显示父进程和子进程的C程序只显示子进程,而不显示父进程

显示父进程和子进程的C程序只显示子进程,而不显示父进程,c,operating-system,C,Operating System,但是,我需要一个输出,当按下1时,显示 >Do you want to create a process? Press 1 for Yes 1 >This should print twice This is the child process This should print twice This is the child process 有人能指出我犯了什么逻辑错误吗?您应该检查从fork获得的pid,下面是子/父代码的示例: >This should be print

但是,我需要一个输出,当按下1时,显示

>Do you want to create a process? Press 1 for Yes 1
>This should print twice This is the child process This should print twice This is the child process

有人能指出我犯了什么逻辑错误吗?

您应该检查从fork获得的pid,下面是子/父代码的示例:

>This should be printed twice
>This is child process
>This should be printed twice
>This is parent process

我想您忘了将
fork
的结果分配给
pid
;目前,它还未初始化。您还应该考虑当用户选择不使用fork时会发生什么。(您可能还希望在消息中添加换行符,
\n
,以获得更整洁的输出,或者使用
put
pid=fork()
或其他
pid
只是随机垃圾。投票以简单的打字方式结束。@Lundin谢谢,我明白了。结束主题(如果可以的话)
>This should be printed twice
>This is child process
>This should be printed twice
>This is parent process
int main(){

int  a, b;

pid_t pid;

a = 3;

b = 5;

pid = fork();

if (pid == -1)

{

    printf("ERROR IN FORK MAIN\n");

}

if(pid == 0)

{

   Add( a,b) ;/*a function for child to do*/

}

else
{

    printf("Parent Done\n");

}
          return 0;}