调用setsid函数的进程需要满足以下条件:
1. 不是组长进程
2. 有对应的创建session权限
调用setsid函数后的进程:
1. 成为一个组的组长进程
2. 成为一个会话的会话首进程
3. 放弃了控制终端
#include <stdio.h>#include <unistd.h>#include <stdlib.h>int main(int argc, char *argv[]){ pid_t pid; pid = fork(); if (pid < 0) { perror("fork"); return -1; } else if (pid == 0) { PRintf("before====/n"); printf("pid = %d/n", (int)getpid()); printf("gid = %d/n", (int)getpgrp()); printf("sid = %d/n", (int)getsid(getpid())); setsid(); printf("after====/n"); printf("pid = %d/n", (int)getpid()); printf("gid = %d/n", (int)getpgrp()); printf("sid = %d/n", (int)getsid(getpid())); sleep(5); } else { printf("parent-------/n"); printf("pid = %d/n", getpid()); printf("gid = %d/n", getgid()); printf("sid = %d/n", getsid()); waitpid(pid, NULL, 0); } return 0;}运行结果:parent-------pid = 13227gid = 0sid = 12710before====pid = 13228gid = 13227sid = 12710after====pid = 13228gid = 13228sid = 13228
新闻热点
疑难解答