美文网首页
Linux守护进程和线程

Linux守护进程和线程

作者: 捉虫大师 | 来源:发表于2015-04-10 10:31 被阅读597次

守护进程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

相关文章

  • Linux守护进程和线程

    守护进程daemon 守护进程概念就不赘述了,挺好理解的,从名字都能看出来。查看 其中TPGID为-1的代表dae...

  • 进程管理

    进程管理进程和线程图形简单解释进程和线程管理VIM编辑Linux 系统资源信息的获取获取 Linux CPU 信息...

  • 开题

    linux 内核 线程和进程管理

  • Java-守护线程

    前言:Java中有两类线程:用户线程和守护线程 其实以前老早就听过守护进程,很少有人提起守护线程; 为什么会出现守...

  • Android中进程和线程

    1,Linux中的进程管理1)进程和线程进程是资源分配的最小单位。线程是操作系统调度执行的最小单位。进程和线程是程...

  • 线程

    线程的概念 线程:light weight process,轻量级的进程,Linux环境下本质上仍是进程。和进程的...

  • java——多线程讲解三

    java多线程的补充:守护线程(后台线程)守护线程:在一个进程中如果只剩下了守护线程,那么守护线程也会死亡。任何一...

  • 进程守护进程

    什么是守护进程?Linux 的大多数服务器就是用守护进程实现的,使用ps -axj可以查看守护进程: 守护进程基本...

  • Python入门开发学习笔记之了解守护线程的概念

    本节重点 了解守护线程的概念了解守护线程与守护进程的区别本节时长需控制在15分钟内 一 守护线程 无论是进程还是线...

  • 守护进程和守护线程详解

    守护进程 什么是守护进程? 守护进程会在主进程代码运行结束的情况下,立即挂掉。 守护进程本身就是一个子进程。 主进...

网友评论

      本文标题:Linux守护进程和线程

      本文链接:https://www.haomeiwen.com/subject/orttfttx.html