- 论坛徽章:
- 0
|
有兄弟说在if以后的代码是父进程和子进程都会执行的,但是我试验的时候,却没有呢????
头文件省略
int Print(char * out)
{
if(NULL == out)
return -1;
while(1)
{
printf("out is %s and pid is %d\n",out , getpid());
sleep(1);
}
}
int main(void)
{
pid_t newpid;
printf("Hello\n");
newpid = fork();
if(newpid==0)
{
printf("Son is run!\n");
Print("son");
}
else if(newpid>0){
printf("my son is %d\n",newpid);
}else if(newpid <0){
printf("fork fail!\n");
exit(-1);
}
while(1)
{
printf("The thread is running!pid is %d\n",getpid());
sleep(1);
}
}
执行的是这样的:
Hello
my son is 7123
The thread is running!pid is 7122
Son is run!
out is son and pid is 7123
The thread is running!pid is 7122
out is son and pid is 7123
The thread is running!pid is 7122
...................
这里为什么总是父进程在执行“The thread is。。。”,而子进程不执行呢? |
|