关于setpgid函数的小知识点

42 篇文章 0 订阅

我想在主进程里面调用setpgid()来将第一个子进程的进程组ID设置为第二个进程的进程ID,想知道这样会不会自动创建一个新的进程组,第二个子进程作为进程组组长。
代码如下
 

#include <stdio.h>

#include <stdlib.h>
#include <zconf.h>

int main() {
    pid_t pid1,pid2,pid3;
    pid1 = fork();
    if(pid1 == 0){
        printf("I'm the first child process,\n");
        printf("before set pgrpID,\n");
        printf("My processID is %d,my parent processID is %d,my process groupID is %d\n\n",getpid(),getppid(),getpgrp());
        sleep(5);

        printf("[1]after set pgrpID,\n");
        printf("[1]My processID is %d,my parent processID is %d,my process groupID is %d\n\n",getpid(),getppid(),getpgrp());

        //child 1
    }
    else{
        pid2 = fork();
        if(pid2 == 0){
            sleep(1);
            printf("I'm the second child process,\n");
            printf("My processID is %d,my parent processID is %d,my process groupID is %d\n\n",getpid(),getppid(),getpgrp());
            //child2
        }
        else{
            pid3 = fork();
            if(pid3 == 0){
                sleep(2);
                printf("I'm the third child process,\n");
                printf("My processID is %d,my parent processID is %d,my process groupID is %d\n\n",getpid(),getppid(),getpgrp());
                //child3
            }
            else{
                sleep(3);
                printf("I'm the main process.\n");
                printf("My processID is %d\n",getpid());
                printf("pid1: %d,pid2: %d\n",pid1,pid2);
                if(setpgid(pid1,pid2))
                    printf("set failed.\n");
                sleep(8);
                printf("The main process going to shut down.\n\n");
            }

        }
    }
    return 0;
}

输出

/home/gilbert/CLionProjects/C_project/mutex/mutex_lesson1/cmake-build-debug/mutex_lesson1
I'm the first child process,
before set pgrpID,
My processID is 8146,my parent processID is 8143,my process groupID is 8143

I'm the second child process,
My processID is 8147,my parent processID is 8143,my process groupID is 8143

I'm the third child process,
My processID is 8148,my parent processID is 8143,my process groupID is 8143

I'm the main process.
My processID is 8143
pid1: 8146,pid2: 8147
set failed.
[1]after set pgrpID,
[1]My processID is 8146,my parent processID is 8143,my process groupID is 8143

The main process going to shut down.


Process finished with exit code 0

 

该函数的原型为

#include<unistd.h>

int setpgid(pid_t pid,pid_t pgid);

返回值,若成功,返回0,若失败,返回-1;

上面的调用失败,原因是进程组组ID等于第二个子进程之进程ID的进程组不存在。

若是修改一下代码

#include <stdio.h>

#include <stdlib.h>
#include <zconf.h>

int main() {
    pid_t pid1,pid2,pid3;
    pid1 = fork();
    if(pid1 == 0){
        printf("I'm the first child process,\n");
        printf("before set pgrpID,\n");
        printf("My processID is %d,my parent processID is %d,my process groupID is %d\n\n",getpid(),getppid(),getpgrp());
        sleep(5);

        printf("[1]after set pgrpID,\n");
        printf("[1]My processID is %d,my parent processID is %d,my process groupID is %d\n\n",getpid(),getppid(),getpgrp());

        //child 1
    }
    else{
        pid2 = fork();
        if(pid2 == 0){
            sleep(1);
            printf("I'm the second child process,\n");
            printf("My processID is %d,my parent processID is %d,my process groupID is %d\n\n",getpid(),getppid(),getpgrp());
            setpgid(0,getpid());
            printf("[2]after set pgrpID,\n");
            printf("[2]My processID is %d,my parent processID is %d,my process groupID is %d\n\n",getpid(),getppid(),getpgrp());

            //child2
        }
        else{
            pid3 = fork();
            if(pid3 == 0){
                sleep(2);
                printf("I'm the third child process,\n");
                printf("My processID is %d,my parent processID is %d,my process groupID is %d\n\n",getpid(),getppid(),getpgrp());
                //child3
            }
            else{
                sleep(3);
                printf("I'm the main process.\n");
                printf("My processID is %d\n",getpid());
                printf("pid1: %d,pid2: %d\n",pid1,pid2);
                if(setpgid(pid1,pid2))
                    printf("set failed.\n");
                sleep(8);
                printf("The main process going to shut down.\n\n");
            }

        }
    }

    return 0;
}


就可以设置了。

输出:

/home/gilbert/CLionProjects/C_project/mutex/mutex_lesson1/cmake-build-debug/mutex_lesson1
I'm the first child process,
before set pgrpID,
My processID is 10183,my parent processID is 10182,my process groupID is 10182

I'm the second child process,
My processID is 10184,my parent processID is 10182,my process groupID is 10182

[2]after set pgrpID,
[2]My processID is 10184,my parent processID is 10182,my process groupID is 10184

I'm the third child process,
My processID is 10185,my parent processID is 10182,my process groupID is 10182

I'm the main process.
My processID is 10182
pid1: 10183,pid2: 10184
[1]after set pgrpID,
[1]My processID is 10183,my parent processID is 10182,my process groupID is 10184

The main process going to shut down.


Process finished with exit code 0
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值