linux 信号量是什么怎么用

linux 信号量是什么怎么用,第1张

Linux信号量(semaphore)是一种互斥机制。即对某个互斥资源的访问会收到信号量的保护,在访问之前需要获得信号量。

在 *** 作完共享资源后,需释放信号量,以便另外的进程来获得资源。获得和释放应该成对出现。

获得信号量集,需要注意的是,获得的是一个集合,而不是一个单一的信号量。

#include

#include

#include

1: int semget(key_t key,int nsems,int semflg)

key:系统根据这个值来获取信号量集。

nsems:此信号集包括几个信号量。

semflg:创建此信号量的属性。 (IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR)

成功则返回该信号量集的ID。

注:

既指定IPC_CREAT又指定IPC_EXCL时,如果系统中该信号量集已经存在,则马上返回。

如果需要获得存在的信号量,则将此参数置0.

2: int semctl(int semid,int senum,int cmd....)

semid:信号量ID。

senum:对信号量集中的第几个信号量进行控制。(从0开始)

cmd:需要进行的 *** 作。(SETVAL是其中的一个)。

根据cmd的不同可能存在第四个参数,cmd=SETVAL时,表示同时信号量可以被获得几次,如第四个参数

num=1表示只能被获得一次,既被信号量保护的资源只能同时被一个程序使用。

该系统调用,是在对信号量初始化时用的。

-3: “3”前面加了"-"表示当需要使用互斥资源时应该做这步。

int semop(int semid,struct sembuf *sem,int num_elements)

struct sembuf {

unsigned short sem_num //该信号量集中的第几个信号量。

int sem_op//需要获得还是释放信号量

int sem_flg//相关动作

}

num_elements:需要对该信号量集中的多少个信号量进行处理。

获得信号量时,将sembuf结构提初始化为:

sem_num = 0//该信号量集中的首个信号量

sem_op = -1//获得信号量

sem_flag = IPC_NOWAIT //如果不能获得信号量,马上返回。

semop(semid,_sem,1)

同理释放信号量时,将sem_op设为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  

    }

#include<stdio.h>

#include<pthread.h>

#include<unistd.h>

#include<fcntl.h>

#include<sys/stat.h>

#include<sys/types.h>

#include<semaphore.h>

#include<stdlib.h>

#define N 3

pthread_mutex_t mutex_w,mutex_r// 定义读写互斥锁

sem_t sem_w,sem_r//定义读写信号量

int data[N]

int pos=0

void *function_w(void *arg)

{

int w = *(int *)arg

pos = w

while(1)

{

usleep(100000)

sem_wait(&sem_w)//等待可写的资源

pthread_mutex_lock(&mutex_w)//禁止别的线程写此资源

data[pos] = w

w++

w++

w++

pos++

pos=pos%N

pthread_mutex_unlock(&mutex_w)//别的线程可写此资源

sem_post(&sem_r)// 释放一个读资源

}

return (void *)0

}

void *function_r(void *arg)

{

while(1)

{

sem_wait(&sem_r)//等待可读的资源

pthread_mutex_lock(&mutex_r)//禁止别的线程读此资源

printf("%d\n",data[(pos+N-1)%N])

pthread_mutex_unlock(&mutex_r)//别的线程可读此资源

sem_post(&sem_w)// 释放一个写资源

}

return (void *)0

}

int main(int argc, char **argv)

{

pthread_t thread[2*N]

int i

pthread_mutex_init(&mutex_w,NULL)

pthread_mutex_init(&mutex_r,NULL)

sem_init(&sem_w,0,N)

sem_init(&sem_r,0,0)

for(i=0i<Ni++)

{

if ( pthread_create(&thread[i],NULL,function_w,(void *)&i) <0)//创建写线程

{

perror("pthread_create")

exit(-1)

}

}

for(i=Ni<2*Ni++)

{

if ( pthread_create(&thread[i],NULL,function_r,NULL) <0)//创建读线程

{

perror("pthread_create")

exit(-1)

}

}

sleep(1)

return(0)

}


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

原文地址: https://outofmemory.cn/yw/7148270.html

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

发表评论

登录后才能评论

评论列表(0条)

保存