Linux守护进程和线程

守护进程daemon

守护进程概念就不赘述了,挺好理解的,从名字都能看出来。
查看

ps -ajx

其中TPGID为-1的代表daemon进程。

创建daemon进程有两种方法:

方法1:
 #include<unistd.h>
 pid_t setsid(void);

当调用这个函数的时候不允许当前进程为进程组的Leader,所以一般都fork一个子进程。

#include<unistd.h>
#include<fcntl.h>
#include<stdlib.h>
void daemonize()
{
    pid_t pid;
    pid = fork();
    if(pid > 0){
        exit(0);
    }else if(pid == 0){
        setsid();
        //改变工作目录
        chdir("/");
        //下面将关闭终端和标准输入输出
        close(0);
        open("dev/null", O_RDONLY);
        dup2(0, 1);
        dup2(0, 2);
    }
}

int main(void)
{
    daemonize();
    while(1){
        //running
    }
}
damon
方法2:

使用库函数daemon(man 3 daemon)来创建daemon进程。

man 3 daemon看一下

#include <unistd.h>
int daemon(int nochdir, int noclose);

两个参数的特别说明

If nochdir is zero, daemon() changes the process’s current working directory to the root directory ("/"); otherwise,
If noclose is zero, daemon() redirects standard input, standard output and standard error to /dev/null; otherwise, no changes are made to these file descriptors.

代码就很简单了

#include<unistd.h>
#include<fcntl.h>
#include<stdlib.h>

int main(void)
{
    daemon(0, 0);
    while(1){
        //running
    }
}

线程

线程概念也不再赘述了。线程之间共享:

  • 文件描述符
  • 信号的处理方式
  • 当前的工作目录
  • 用户id和组id

不共享:

  • 线程id
  • 上下文,包括各种寄存器的值、程序计数器和栈指针
  • 栈空间
  • errno 变量
  • 信号屏蔽字
  • 调度优先级

创建线程

#include <pthread.h>
int pthread_create(pthread_t *restrict thread, const pthread_attr_t *restrict attr, void *(*start_routine)(void*), void *restrict arg);

注意:

在Linux上线程函数位于libpthread共享库中,因此在编译时要加上-lpthread选项

man 3 pthread_create 看一下

The pthread_create() function starts a new thread in the calling process.The new thread starts execution by invoking start_routine(); arg is passed as the sole argument of start_routine().
RETURN VALUE:On success, pthread_create() returns 0; on error, it returns an error number, and the contents of *thread are undefined.

意思大概是新启动一个线程就会激活第3个参数所指向的函数,最后一个参数是所激活函数的参数。成功时返回0,否则返回错误编号。

线程的终止:

  • 线程函数调用return,会终止当前线程,若是主线程,则等同exit,会结束进程;
  • pthread_cancel 终止同一进程中的其他线程;
  • pthread_exit 终止自己的线程。
 #include<pthread.h>
 void pthread_exit(void *value_ptr);

这个函数的参数是可以通过函数pthread_join获得。

#include<pthread.h>
int pthread_join(pthread_t thread, void **value_ptr);

调用该函数的线程将挂起等待,直到id为thread的线程终止。thread线程以不同的方法终止,通过pthread_join得到的终止状态是不同的:

  • 如果thread线程通过return返回,value_ptr所指向的单元里存放的是thread线程函数的返回值;
  • 如果thread线程被别的线程调用pthread_cancel异常终止掉,value_ptr所指向的单元里存放的是常数PTHREAD_CANCELED;
  • 如果thread线程是自己调用pthread_exit终止的,value_ptr所指向的单元存放的是传给pthread_exit的参数。

后面的线程同步异步的控制就不展开说了,在读书的时候这块是重点,都是算法。
下面写个简单例子验证一下:

#include<stdio.h>
#include<pthread.h>

void *running()
{
    pthread_t pid;
    pid = pthread_self();
    printf("thread,pid: 0x%u ,is running....\n", pid);
}

int main(void)
{
    pthread_t ntid[3];
    void *tret;
    int i;
    for(i=0; i<3; i++)
    {
        pthread_create(&ntid[i], NULL, running, NULL);
        sleep(1);
    }
    pthread_join(ntid[0], &tret);
    printf("thread 1,exit code %d\n", tret);
    pthread_join(ntid[1], &tret);
    printf("thread 2,exit code %d\n", tret);
    pthread_cancel(ntid[2]);
    pthread_join(ntid[2], &tret);
    printf("thread 3,exit code %d\n", tret);
    return 0;
}

附上运行截图:


thread
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Linux-创建进程与线程用到的函数解析 【1】exit: exit函数可以退出程序并将控制权返回给操作系统,而用...
    Yojiaku阅读 8,953评论 0 2
  • 转自:Youtherhttps://www.cnblogs.com/youtherhome/archive/201...
    njukay阅读 5,529评论 0 52
  • 线程 在linux内核那一部分我们知道,线程其实就是一种特殊的进程,只是他们共享进程的文件和内存等资源,无论如何对...
    大雄good阅读 3,882评论 0 2
  • 大学,是所有学生梦寐以求的,青春,到底什么是青春?是初中时期的懵懂,高中时期的暗恋,还是大学时期的甜蜜,又或者是社...
    佳佳_ec70阅读 2,880评论 0 0
  • 每个人对于“垃圾牌”知识的定义不尽相同,我的定义是放错了时间和空间的知识。 这个话题的触发是一早接到朋友的电话,听...
    幸运的老师阅读 2,383评论 0 0