c++创建一个linux deamon进程

正规的方法, 创建一个deamon进程,需要很多步骤
1. fork()
2. 子进程setsid()
3. 主进程wait()
4. chdir()
5. umask()

非正规方法创建一个deamon进程的步骤:
1. 创建一个子进程fork(), 创建子进程的目的是为了后面的设置进程组ID.
2. 子进程执行setsid(), 执行setsid()有两个目的, 一是创建一个新的会话,二是设置进程组ID.
3. 主进程wait()

每个函数都有他的意义.稍后再研究一下:

创建一个deamon进程

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int main(int argc, char *argv[])
{
        int n = fork();
        pid_t pid;
        fprintf(stdout, "PID:%d, PGID:%d, SID:%d\n", getpid(), getpgrp(), getsid(0));

        int stat = 0;
        if (n > 0) {
                pid_t x = wait(&stat);
                printf("child pid=%d\n", x);
                if(WIFEXITED(stat) == 0) {
                        printf("child normal exited!\n");
                }
        } else if (n == 0) {
                if(setsid() < 0) {
                        fprintf(stderr, "setsid error:%s\n", strerror(errno));
                }

                while(1) {
                        printf("child.\n");
                        sleep(1);
                }
        }

        return 0;
}

输出:
这里写图片描述
子进程退出后, 主进程仍然能正确启动子进程的demo:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int main(int argc, char *argv[])
{
    while(1) {
        int n = fork();
        pid_t pid;
        fprintf(stdout, "PID:%d, PGID:%d, SID:%d\n", getpid(), getpgrp(), getsid(0));

        int stat = 0;
        if (n > 0) {
            pid_t x = wait(&stat);
            printf("child pid=%d\n", x);
            if(WIFEXITED(stat) == 0) {
                printf("child normal exited!\n");
            }
        } else if (n == 0) {
            if(setsid() < 0) {
                fprintf(stderr, "setsid error:%s\n", strerror(errno));
            }

            while(1) {
                printf("child.\n");
                sleep(1);
            }
        }

        sleep(5);
    }

    return 0;
}

输出:
这里写图片描述
僵尸进程:
僵尸进程, 是指子进程先于父进程退出, 且退出时, 父进程没有收到子进程的退出信号.

#include <unistd.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
    int n = fork();
    pid_t pid;
    fprintf(stdout, "PID:%d, PGID:%d, SID:%d\n", getpid(), getpgrp(), getsid(0));

    if (n > 0) {
        while(1) {
            printf("parent.\n");
            sleep(1);
        }
    } else if (n == 0) {
            printf("child.\n");
    }

    return 0;
}

输出:
这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值