昨天开始学LINUX,编了一个关于进程的c程序,虽然结果对了,但过程却不明白。请教!

wayne92 2005-05-10 08:26:37
要求创建两个子进程,分别返回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);
}


...全文
300 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
nicelcp 2005-05-12
  • 打赏
  • 举报
回复
调用了fork之后生成1个子进程,此时子进程与父进程共享代码区,子进程的地址空间指向父进程的地址空间

由于fork的返回值不同,导致父子进程在执行if语句时,走的路不同,打印的结果自然不同

创建了子进程之后,父子进程共享代码区,也就是上段程序代码。只是这段代码作为若干个副本在分别执行而已。各自的执行结果保存在不同的进程上下文中。
zhoufanking 2005-05-12
  • 打赏
  • 举报
回复
两次fork,创建两个子进程,再加上父进程,刚好是3个!
前两个print语句是创建了子进程,子进程运行的结果,最后一个print是回到父进程后
执行的结果。
建议好好看看fork的原理。
nichotilikai 2005-05-12
  • 打赏
  • 举报
回复
fork函数的关键就在于一次调用,二次返回。分别在父子进程中返回。
昨天开始学Linux,今天就到了fork函数,速度还够快的。
踏岸寻柳 2005-05-12
  • 打赏
  • 举报
回复
楼主编译、执行一下这个程序,就知道是怎么回事了:
$ cat fork.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>

#define FORK_COUNT 5 // 试一试修改一下这个值,再看看执行结果^_^

int main( void )
{
pid_t pid;

for(int i=0; i<FORK_COUNT; i++)
{
if ( (pid=fork())<0 )
{
perror( "fork error" );
exit(-1);
}
else if (pid==0)
{
pid = getpid();
printf( "index:[%d] child process execute : %d\n", i, pid );
}
}

return 0;
}
happykevinxing 2005-05-12
  • 打赏
  • 举报
回复
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");
你虽然用的是if .....else 语句,对于单进程程序来说只有一个条件满足,但是用了fork后就没那么简单了,fork 是创建了两个一模一样的进程,在子进程和父进程里面都有上面的代码,但是,子,父进程里pid1的值却不一样,子进程里pid1==0,父进程里pid1==1;所以子父进程运行了不同条件下的代码
lemonmisshll 2005-05-11
  • 打赏
  • 举报
回复
理解fork语句的实际意义很重要!!!
hundlom 2005-05-11
  • 打赏
  • 举报
回复
父进程是程序本身,就是起了3个进程。
anhy 2005-05-11
  • 打赏
  • 举报
回复
进程是在运行的程序
pid1=fork()中的父进程是你的程序
liuchb 2005-05-11
  • 打赏
  • 举报
回复
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>

int main(void)
{
pid_t pid1;
pid_t pid2;
if( (pid1 = fork() ) < 0 ) //pid1<0
printf("fork child1 error!\n");
else if( pid1 == 0 ) //pid1==o
printf("child1 process is printing:%c\n",'b');
else if( (pid2 = fork() ) < 0 )//pid1>0 说明回到了父进程 并开始创建子进程pid2
printf("fork child2 error!\n");
else if( pid2 == 0 )
printf("child2 process is printing:%c\n",'c');
else //此处else的隐含条件是pid2>0 又回到了父进程
printf("parent process is printing:%c\n",'a');
exit (0);
}


这样解释可以吗?我也刚接触,我们一起学习

dataat 2005-05-11
  • 打赏
  • 举报
回复
这里关键是fork调用。理解了fork就应该理解了。
fork调用以后从父进程里面创建了其的一个子进程,两者都从fork处开始往下执行。
通过fork的返回值来区分父进程和子进程,返回0则为子进程,非零(正整数)则为父进程。由此可以看懂上面的语句。
好好理解一下,祝你好运。
gaoxianfeng 2005-05-11
  • 打赏
  • 举报
回复
变成三个进程了
sharkhuang 2005-05-10
  • 打赏
  • 举报
回复
2个子进程一个父进程!刚好3
wayne92 2005-05-10
  • 打赏
  • 举报
回复
还是不明白,楼上的可以讲的明白一点吗?
我不明白的是为什么会有三个语句打印出来?
还有,pid1 = fork()中,父进程是什么?是这个程序本身吗?
anhy 2005-05-10
  • 打赏
  • 举报
回复
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>

int main(void)
{
pid_t pid1;
pid_t pid2;
if( (pid1 = fork() ) < 0 )//如果pid1<0表示fork失败
printf("fork child1 error!\n");
else if( pid1 == 0 )//pid1==0表示下面的内容是子进程的部分
printf("child1 process is printing:%c\n",'b');
else if( (pid2 = fork() ) < 0 )//pid1>0表示下面的内容是父进程的内容,同理pid2
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);
}
blankman 2005-05-10
  • 打赏
  • 举报
回复
貌似执行了两个 fork 操作,也就是说总共应该有 3 个进程存在

如果是两个进程的话好像不是这么写的,程序里面应该只有一个 fork


道行太浅,只能看到这个程度,高人指点一下

23,118

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧