昨天开始学LINUX,编了一个关于进程的c程序,虽然结果对了,但过程却不明白。请教!
要求创建两个子进程,分别返回b和c,父进程返回a
下面的程序运行结果为
child1 process is printing:b
child2 process is printing:c
parent process is printing:a
有一点不明白,我用的是if—if else语句,为什么会打印出三个语句呢?
我想是进程的创建我还不明白,希望有人指教一下,谢谢!
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int main(void)
{
pid_t pid1;
pid_t pid2;
if( (pid1 = fork() ) < 0 )
printf("fork child1 error!\n");
else if( pid1 == 0 )
printf("child1 process is printing:%c\n",'b');
else if( (pid2 = fork() ) < 0 )
printf("fork child2 error!\n");
else if( pid2 == 0 )
printf("child2 process is printing:%c\n",'c');
else
printf("parent process is printing:%c\n",'a');
exit (0);
}