我正在编写我的frist fork-pipe代码,以了解它是如何工作的.
关键是我要创造3个与他父亲交流的孩子,每个孩子都会执行不同的任务.
这是我正在研究的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
int main(void)
{
int nbytes;
int fd1[2], fd11[2]; /*fd1-->child1*/
int fd2[2], fd21[2]; /*fd2-->child2*/
int fd3[2], fd31[2]; /*fd3-->child3*/
pid_t child1, child2, child3;
char string[] = "Hello, world!\n";
char string1[] = "Shutting Down\n";
char readbuffer[80];
printf("child1-->%d\n", child1);
if (child1==0 && child2==0 && child3==0){
printf( " HOLA-> pid %d\n", getpid() );
if(pipe(fd1)==-1 || pipe(fd11)==-1){ //Creating Pipes for son 1
perror("pipe");
exit(-1);
}
if(pipe(fd2)==-1 || pipe(fd21)==-1){ //Creating Pipes for son …Run Code Online (Sandbox Code Playgroud)