Linux守护进程和线程

2015-04-10  本文已影响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
    }
}

线程

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

不共享:

创建线程

#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,否则返回错误编号。

线程的终止:

 #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得到的终止状态是不同的:

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

#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
上一篇 下一篇

猜你喜欢

热点阅读