p11-3.c

来自「SUN Solaris8平台下进程间通信」· C语言 代码 · 共 32 行

C
32
字号
#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include "err_exit.h"int main (void){    pid_t pid;    int n, mypipe[2];    char buffer[BUFSIZ+1];    /* 创建管道 */    if (pipe (mypipe))         err_exit("Pipe failed.\n");    /* 派生子进程 */     pid = fork ();      if (pid < (pid_t) 0)           err_exit ("Fork failed.\n");       /* fork 失败 */    else if (pid == (pid_t) 0) {           /* 这是子进程 */        sprintf(buffer, "%d",mypipe[0]);    /* 转换描述字为字符 */        execl("p11-4", "p11-4", buffer, (char *)0);          exit (EXIT_SUCCESS);    }    else {   /* 这是父进程 */        close(mypipe[0]);        n = write(mypipe[1], "Hello world!\n", strlen("Hello world!\n"));        printf("%d: write %d bytes\n", getpid(),n);        n = write(mypipe[1], "Greeting from father",                                         strlen("Greeting from father"));        printf("%d: write %d bytes\n", getpid(),n);        exit(EXIT_SUCCESS);    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?