c语言实例,linux线程同步的信号量方式 谢谢

c语言实例,linux线程同步的信号量方式 谢谢,第1张

这么高的悬赏,实例放后面。信号量(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  

    }

和用于分配、释放共享内存的 shmget 和 shmctl 类似,系统调用 semget 和 semctl 负责分配、释放信号量。调用 semget 函数并传递如下参数:一个用于标识信号量组的键值,该组中包含的信号量数量和与 shmget 所需的相同的权限位标识。该函数返回的是信号量组的标识符。您可以通过指定正确的键值来获取一个已经存在的信号量的标识符;这种情况下,传递的信号量组的容量可以为0。

信号量会一直保存在系统中,甚至所有使用它们的进程都退出后也不会自动被销毁。最后一个使用信号量的进程必须明确地删除所使用的信号量组,来确保系统中不会有太多闲置的信号量组,从而导致无法创建新的信号量组。可以通过调用semctl来删除信号量组。调用时的四个参数分别为信号量组的标识符, *** 作的信号量在组中的编号、常量IPC_RMID 和一个 union semun 类型的任意值(被忽略)。调用进程的有效用户 id 必须与分配这个信号量组的用户 id 相同(或者调用进程为 root 权限亦可)。与共享内存不同,删除一个信号量组会导致 Linux 立即释放资源。

代码 5.2 展示了用于分配和释放一个二元信号量的函数。

代码 5.2 (sem_all_deall.c)分配和释放二元信号量

#include <sys/ipc.h>

#include <sys/sem.h>

#include <sys/types.h>/* 我们必须自己定义 semun 联合类型。 */

union semun { int valstruct semid_ds *bufunsigned short int *arraystruct seminfo *__buf}

/* 获取一个二元信号量的标识符。如果需要则创建这个信号量 */

int binary_semaphore_allocation (key_t key, int sem_flags)

{

return semget (key, 1, sem_flags)

} /* 释放二元信号量。所有用户必须已经结束使用这个信号量。如果失败,返回 -1 */

int binary_semaphore_deallocate (int semid)

{

union semun ignored_argumentreturn semctl (semid, 1, IPC_RMID, ignored_argument)

}


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

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

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

发表评论

登录后才能评论

评论列表(0条)

    保存