p11-1.c
来自「SUN Solaris8平台下进程间通信」· C语言 代码 · 共 30 行
C
30 行
#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]; char some_data[] = "Hello, world!"; /* 创建管道 */ 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) { /* 这是子进程 */ close(mypipe[1]); n = read(mypipe[0],buffer,BUFSIZ); printf("child %d: read %d bytes: %s\n",getpid(),n,buffer); } else { /* 这是父进程 */ close(mypipe[0]); n = write(mypipe[1], some_data, strlen(some_data)); printf("parent %d: write %d bytes\n", getpid(),n); } exit (EXIT_SUCCESS);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?