所以我试图在第一个孩子身上创建2个子进程和一个孙子进程。我正在尝试运行第一个子进程,然后是孙子,然后是第二个子进程,父进程在进程完成时调用pid。我完全陷入困境 - 我有许多代码版本的代码,但无法按正确的顺序触发它们。任何帮助,建议或链接将不胜感激。
所需的输出将是
- 我是第一个孩子
----我是孙子
我是父母,第一个孩子有pid 21505
- 我是第一个孩子,孙子有21506 pid
- 再试一次
- 我是第二个孩子
- - >第二个子节点:传输了1317个字节。
我是父母,第二个孩子有21507 pid
父母 - 儿童21507完成
- 再试一次
****应该在这里看到前20行lab4.c文件
父母 - 其他孩子21505已完成
int main(int* argc, char* args[])
{
pid_t child1, child2, gchild; //process ids
child1 = fork(); //Create the two child processes
if (child1 == 0)
{ // parent
child2 = fork();
if (child2 == 0)
{ //parent
printf("P: first child has pid %d\n", child1);
printf("P: second child has pid %d\n", child2);
pid_t wpid; // process id that is finished
int status; //status code returned by process
//Wait for all child processes to complete
while ((wpid = wait(&status)) > 0)
{
if (wpid > 0)
{
printf("P: Child %d is done \n", wpid);
}
};
exit(0); //exit main parent process
}
else
{
//Second process's code
printf("SC: I am the second child.\n");
int totalBytes = 0; //total number of bytes in the file we are going to read in
char buffer[2056]; //file contents size 2056 bytes
ssize_t read_bytes = 0; //Bytes from each chunk get placed here temporarily
///Read in file contents: home/common/lab_sourcefile
int fd_in = open("/home/COIS/3380/lab4_sourcefile", O_RDONLY);//open the file
do
{
//read the file chunk by chunk to the buffer
read_bytes = read(fd_in, buffer, (size_t)2056);
// End of file or error.
if (read_bytes <= 0)
{
break; //done reading file
}
//Save the total number of bytes
totalBytes = totalBytes + read_bytes;
} while (1); //infinite loop until we are done reading the file
//Write out contents to: lab4_file_copy
char filepath[1024]; //temp path
getcwd(filepath, sizeof(filepath)); //Get the current working directory
strcat(filepath, "/lab4_file_copy"); //Tack on the filename
int fd = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IRGRP | S_IROTH); //open the file. O_RDWR = read and write flag, O_CREAT = create if doesnt exist, S_* flags are permission flags from fstat
write(fd, buffer, strlen(buffer)); //write to the file
close(fd_in);//close file we were reading
close(fd); //close copy file
// print out the number of bytes the file
printf("SC: --> Second child: %d bytes transferred.\n", totalBytes);
exit(0); //done kill second child process
}
}
else
{
//First child process
printf("FC: I'm the first child.\n");
//Create the granchild process
gchild = fork();
if (gchild == 0)
{
printf("GC: I am the grandchild.\n");
sleep(3); //sleep for 3 seconds
printf("GC: #### Output start ####\n");
execlp("head", "head", "-n", "20", "l.c", NULL); //this should exit automatically
printf("ERROR: execlp command failed\n");//This will only run if the execlp process fails
exit(1); //FAILED!
}
else
{
//Output the granchild pid
printf("FC: I am the first child, grandchild has pid %d\n", gchild);
int grandchildStatus;
//Wait for grandchild process to be done. Polling...
while ((waitpid(gchild, &grandchildStatus, WNOHANG)) != gchild)
{
printf("FC: Try again\n");
sleep(1);
};
printf("GC: #### Output end ####\n");
//End of first child. Grandchild must have completed.
exit(0); //Done. kill first child process
}
}
}
答案 0 :(得分:0)
你的程序基本上有两个错误:
最后else
部分:
您致电fork
,以便fork
功能之后的所有内容都会在两个流程中执行。
两者父母和第二个孩子将打印:&#34; 我是父母,第一个孩子有pid ...... &#34;
要解决此问题,您必须添加if(child2 == 0)
(在else
区块内)...
指示顺序:
当您启动子流程时,子流程真正与父流程同时运行:
如果一个进程打印出&#34; Hello world。&#34;另一个打印出来&#34; 这是一个测试。&#34;您可能会在屏幕上看到以下文字:&#34; 此 Hello 是 世界。 测试强>&#34; (或类似的)因为两个进程同时写入屏幕。
您可以使用子进程父进程中的waitpid
函数等待子进程完成,以确保子进程在父进程继续之前完成(打印出文本)。
如果您希望首先在父进程中打印文本,则必须找到一种方法来停止子进程并等待父进程完成打印。
例如,您可以使用raise(SIGSTOP);
来停止当前正在运行的进程。在另一个流程中,使用kill(pid, SIGCONT);
继续正在等待的其他流程pid
:
child = fork();
if(child == 0)
{
raise(SIGSTOP); /* Wait for the parent */
/* Do something */
}
else
{
/* Do something */
kill(child, SIGCONT); /* Tell the child to continue */
}
...但是:正如PSkocik刚刚提到的那样,即使在这种情况下你的程序也可能会挂起 - 所以这并不容易!