Linux 的多线程编程中,如何给线程发信号

Linux 的多线程编程中,如何给线程发信号,第1张

不管是在进程还是线程,很多时候我们都会使用一些定时器之类的功能,这里就定时器在多线程的使用说一下。首先在linux编程中定时器函数有alarm()和setitimer(),alarm()可以提供一个基于秒的定时功能,而setitimer可以提供一个基于微妙的定时功能。

alarm()原型:

#include <unistd.h>

unsigned int alarm(unsigned int seconds)

这个函数在使用上很简单,第一次调用这个函数的时候是设置定时器的初值,下一次调用是重新设置这个值,并会返回上一次定时的剩余时间。

setitimer()原型:

#include <sys/time.h>

int setitimer(int which, const struct itimerval *value,struct itimerval *ovalue)

这个函数使用起来稍微有点说法,首先是第一个参数which的值,这个参数设置timer的计时策略,which有三种状态分别是:

ITIMER_REAL:使用系统时间来计数,时间为0时发出SIGALRM信号,这种定时能够得到一个精准的定时,当然这个定时是相对的,因为到了微秒级别我们的处理器本身就不够精确。

ITIMER_VIRTUAL:使用进程时间也就是进程分配到的时间片的时间来计数,时间为0是发出SIGVTALRM信号,这种定时显然不够准确,因为系统给进程分配时间片不由我们控制。

ITIMER_PROF:上面两种情况都能够触发

第二个参数参数value涉及到两个结构体:

struct itimerval {

struct timeval it_interval /* next value */

struct timeval it_value /* current value */

}

struct timeval {

long tv_sec /* seconds */

long tv_usec /* microseconds */

}

在结构体itimerval中it_value是定时器当前的值,it_interval是当it_value的为0后重新填充的值。而timeval结构体中的两个变量就简单了一个是秒一个是微秒。

上面是这两个定时函数的说明,这个函数使用本不是很难,可以说是很简单,但是碰到具体的应用的时候可能就遇到问题了,在多进程编程中使用一般不会碰到什么问题,这里说的这些问题主要体现在多线程编程中。比如下面这个程序:

#include <pthread.h>

#include <signal.h>

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/time.h>

void sig_handler(int signo)

{

alarm(2)

printf("alarm signal\n")

}

void *pthread_func()

{

alarm(2)

while(1)

{

pause()

}

}

int main(int argc, char **argv)

{

pthread_t tid

int retval

signal(SIGALRM, sig_handler)

if((retval = pthread_create(&tid, NULL, pthread_func, NULL)) <0)

{

perror("pthread_create")

exit(-1)

}

while(1)

{

printf("main thread\n")

sleep(10)

}

return 0

}

这个程序的理想结果是:

main thread

alarm signal

alarm signal

alarm signal

alarm signal

alarm signal

main thread

可事实上并不是这样的,它的结果是:

main pthread

alarm signal

main pthread

alarm signal

main pthread

这么高的悬赏,实例放后面。信号量(sem),如同进程一样,线程也可以通过信号量来实现通信,虽然是轻量级的。信号量函数的名字都以"sem_"打头。线程使用的基本信号量函数有四个。

     信号量初始化。

     int sem_init (sem_t *sem , int pshared, unsigned int value)

    这是对由sem指定的信号量进行初始化,设置好它的共享选项(linux 只支持为0,即表示它是当前进程的局部信号量),然后给它一个初始值VALUE。

    等待信号量。给信号量减1,然后等待直到信号量的值大于0。

    int sem_wait(sem_t *sem)

    释放信号量。信号量值加1。并通知其他等待线程。

    int sem_post(sem_t *sem)

    销毁信号量。我们用完信号量后都它进行清理。归还占有的一切资源。

    int sem_destroy(sem_t *sem) #include <stdlib.h>  

    #include <stdio.h>  

    #include <unistd.h>  

    #include <pthread.h>  

    #include <semaphore.h>  

    #include <errno.h>  

    #define return_if_fail(p) if((p) == 0){printf ("[%s]:func error!/n", __func__)return}  

    typedef struct _PrivInfo  

    {  

        sem_t s1  

        sem_t s2  

        time_t end_time  

    }PrivInfo  

    static void info_init (PrivInfo* thiz)  

    static void info_destroy (PrivInfo* thiz)  

    static void* pthread_func_1 (PrivInfo* thiz)  

    static void* pthread_func_2 (PrivInfo* thiz)  

    int main (int argc, char** argv)  

    {  

        pthread_t pt_1 = 0  

        pthread_t pt_2 = 0  

        int ret = 0  

        PrivInfo* thiz = NULL  

        thiz = (PrivInfo* )malloc (sizeof (PrivInfo))  

        if (thiz == NULL)  

        {  

            printf ("[%s]: Failed to malloc priv./n")  

            return -1  

        }  

        info_init (thiz)  

        ret = pthread_create (&pt_1, NULL, (void*)pthread_func_1, thiz)  

        if (ret != 0)  

        {  

            perror ("pthread_1_create:")  

        }  

        ret = pthread_create (&pt_2, NULL, (void*)pthread_func_2, thiz)  

        if (ret != 0)  

        {  

            perror ("pthread_2_create:")  

        }  

        pthread_join (pt_1, NULL)  

        pthread_join (pt_2, NULL)  

        info_destroy (thiz)  

        return 0  

    }  

    static void info_init (PrivInfo* thiz)  

    {  

        return_if_fail (thiz != NULL)  

        thiz->end_time = time(NULL) + 10  

        sem_init (&thiz->s1, 0, 1)  

        sem_init (&thiz->s2, 0, 0)  

        return  

    }  

    static void info_destroy (PrivInfo* thiz)  

    {  

        return_if_fail (thiz != NULL)  

        sem_destroy (&thiz->s1)  

        sem_destroy (&thiz->s2)  

        free (thiz)  

        thiz = NULL  

        return  

    }  

    static void* pthread_func_1 (PrivInfo* thiz)  

    {  

        return_if_fail(thiz != NULL)  

        while (time(NULL) < thiz->end_time)  

        {  

            sem_wait (&thiz->s2)  

            printf ("pthread1: pthread1 get the lock./n")  

            sem_post (&thiz->s1)  

            printf ("pthread1: pthread1 unlock/n")  

            sleep (1)  

        }  

        return  

    }  

    static void* pthread_func_2 (PrivInfo* thiz)  

    {  

        return_if_fail (thiz != NULL)  

        while (time (NULL) < thiz->end_time)  

        {  

            sem_wait (&thiz->s1)  

            printf ("pthread2: pthread2 get the unlock./n")  

            sem_post (&thiz->s2)  

            printf ("pthread2: pthread2 unlock./n")  

            sleep (1)  

        }  

        return  

    }


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/9010978.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-23
下一篇2023-04-23

发表评论

登录后才能评论

评论列表(0条)

    保存