我正在编写我的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[] = "Shuting 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 2
perror("pipe");
exit(-1);
}
if(pipe(fd3)==-1 || pipe(fd31)==-1){ //Creating Pipes for son 3
perror("pipe");
exit(-1);
}
if((child1 = fork()) == -1){
perror("fork");
exit(-1);
}
/*Creating child 2 and 3*/
if((child2 = fork()) == -1){
perror("fork");
exit(-1);
}
if((child3 = fork()) == -1){
perror("fork");
exit(-1);
}
}
if(child1==0){ // Child 1
close(fd2[0]);close(fd21[0]);close(fd3[0]);close(fd31[0]);
close(fd2[1]);close(fd21[1]);close(fd3[1]);close(fd31[1]);
close(fd1[1]); /* Son process closes up output side of pipe */
close(fd11[0]); /* Son process closes up input side of pipe */
/* Read in a string from the pipe */
read(fd1[0], readbuffer, sizeof(readbuffer));
printf("I'm SON1 -->Received string: %s\n", readbuffer);
printf("LOADING...\n");
sleep(2);
write(fd11[1], "SON1-->Shuting Down\n", (strlen("SON1-->Shuting Down\n")+1));
printf("I'm SON1 -->send string & shutdown\n");
exit(0);
}else if(child2==0){ // Child 2
close(fd1[0]);close(fd11[0]);close(fd3[0]);close(fd31[0]);
close(fd1[1]);close(fd11[1]);close(fd3[1]);close(fd31[1]);
close(fd2[1]); /* Son process closes up output side of pipe */
close(fd21[0]); /* Son process closes up input side of pipe */
/* Read in a string from the pipe */
read(fd2[0], readbuffer, sizeof(readbuffer));
printf("I'm SON2 -->Received string: %s\n", readbuffer);
printf("LOADING...\n");
sleep(2);
write(fd21[1], "SON2-->Shuting Down\n", (strlen("SON2-->Shuting Down\n")+1));
printf("I'm SON2 -->send string & shutdown\n");
exit(0);
}else if(child3==0){ // Child 3
close(fd2[0]);close(fd21[0]);close(fd1[0]);close(fd11[0]);
close(fd2[1]);close(fd21[1]);close(fd1[1]);close(fd11[1]);
close(fd3[1]); /* Son process closes up output side of pipe */
close(fd31[0]); /* Son process closes up input side of pipe */
/* Read in a string from the pipe */
read(fd3[0], readbuffer, sizeof(readbuffer));
printf("I'm SON3 -->Received string: %s\n", readbuffer);
printf("LOADING...\n");
sleep(2);
write(fd31[1], "SON3-->Shuting Down\n", (strlen("SON3-->Shuting Down\n")+1));
printf("I'm SON3 -->send string & shutdown\n");
exit(0);
}else{ // DAD Code
close(fd1[0]);close(fd2[0]);close(fd3[0]); /* Father process closes up input side of pipe */
close(fd11[1]);close(fd21[1]);close(fd31[1]); /* Father process closes up output side of pipe */
/* Send "string" through the output side of pipe */
write(fd1[1], string, (strlen(string)+1));
printf("I'm the father -->send string to SON1\n");
write(fd2[1], string, (strlen(string)+1));
printf("I'm the father -->send string to SON2\n");
write(fd3[1], string, (strlen(string)+1));
printf("I'm the father -->send string to SON3\n");
read(fd11[0], readbuffer, sizeof(readbuffer));
printf("I'm the father -->SON1 Received string: %s", readbuffer);
read(fd21[0], readbuffer, sizeof(readbuffer));
printf("I'm the father -->SON2 Received string: %s", readbuffer);
read(fd31[0], readbuffer, sizeof(readbuffer));
printf("I'm the father -->SON3 Received string: %s", readbuffer);
sleep(2);
printf("Father shuting down\n");
exit(0);
}
return(0);
}
我的问题是,当我执行代码时,它会出现类似的内容:
I'm the father -->send string to SON1
I'm the father -->send string to SON2
I'm the father -->send string to SON3
I'm SON3 -->Received string: Hello, world!
LOADING...
I'm SON2 -->Received string: Hello, world!
LOADING...
I'm SON1 -->Received string: Hello, world!
LOADING...
I'm SON3 -->send string & shutdown
I'm SON2 -->send string & shutdown
I'm SON1 -->send string & shutdown
I'm the father -->SON1 Received string: SON1-->Shuting Down
I'm the father -->SON2 Received string: SON2-->Shuting Down
I'm the father -->SON3 Received string: SON3-->Shuting Down
Father shuting down
V:~$ I'm SON2 -->Received string:
LOADING...
I'm SON1 -->Received string:
LOADING...
I'm SON1 -->Received string:
LOADING...
I'm SON1 -->Received string:
LOADING...
我的问题是:¿对儿子的四次召唤来自哪里?我错过了什么?
任何帮助将不胜感激!
提前谢谢你,对不起我的拼写错误
答案 0 :(得分:6)
好的,我现在明白了。你的错误是你以下列方式分叉:
if ((child1 = fork() == -1) {
// handle error
}
if ((child2 = fork()) == -1)
..
..
..
fork后,将创建一个新的流程副本。它将继续与父母相同的位置。它还将执行下一个分叉 第一次分叉后,将创建一个新子项。然后在第二个fork之后你将有4个进程来执行第3个fork。结束8个进程。假设fork sys调用没有失败。
如果你只想创建3个进程,那么它应该是这样的:
Child1 = fork();
if (Child1 == -1) { // handle error }
if (Child1 == 0) { // child process }
if (Child1 > 0)
{
Child2 = fork();
if (Child2 == -1) ...
if (child2 == 0)...
if (child2 > 0)
{
Child3 = fork();....
}
.
.
答案 1 :(得分:5)
让我们看一下这段代码:
if((child1 = fork()) == -1){
perror("fork");
exit(-1);
}
/*Creating child 2 and 3*/
if((child2 = fork()) == -1){
perror("fork");
exit(-1);
}
if((child3 = fork()) == -1){
perror("fork");
exit(-1);
}
假设没有任何分支失败,在第一个分支之后,你将有两个进程(父进程和子进程),这两个进程从该点开始继续。所以两个都将执行第二个fork,给你4个进程(父进程,2个子进程和孙进程),所有进程都进入第3个分支,之后你有8个进程......