现在的位置: 首页 > 综合 > 正文

子父进程

2014年02月13日 ⁄ 综合 ⁄ 共 432字 ⁄ 字号 评论关闭

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
int main(void)
{
pid_t pid;
char *message;
int n;
pid=fork();
if(pid<0)
{
   perror("fork failed!");
   exit(1);
}
if(pid==0)
{
   message="This is the child!/n";
   n=6;
   printf("%d/n",getppid());
}
else
{
   message="This is the parent!/n";
   n=3;
}
for(;n>0;n--)
{
   printf(message);
   sleep(1);
   if(pid==0)                             //当父进程结束后子进程的父进程变成init
    printf("%d/n",getppid());
}
return 0;
}

抱歉!评论已关闭.