您的位置:首页 > 编程语言

UNIX环境编程----编程实例---创建一个子进程并建立一个新的会话

2015-11-23 20:30 330 查看
创建一个子进程并建立一个新的会话:

源代码:

#include<unistd.h>

#include<stdio.h>

#include<fcntl.h>

#include<errno.h>

#include<sys/types.h>

int main()

{

int gpid,npid,spid;

npid=fork();

if(npid==-1)

{

perror("fork error");

_exit(0);

}else if(npid>0)

{

printf("the parent process do nothing \n");

_exit(0);

}else

{

//创建一个新会话

gpid=setsid();

if(gpid==-1)

{

perror("setsid error ");

_exit(-1);

}else

{

printf("the new session create success \n");

//判断该子进程的是不是新的组长进程

//取得子进程的进程ID

spid=getpid();

if(spid==gpid)

printf("the sub process is the group process leader \n");

}

}

return 0;

}

运行结果:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: