Linux 信号量
信号量
信号量是Dijkstra在1965年提出的一种方法,它使用一个整型变量来累计唤醒次数,供以后使用。在他的建议中引入了一个新的变量类型,称作信号量(semaphore)。一个信号量的取值可以为0(表示没有保存下来的唤醒操作)或者正值(表示有一个或多个唤醒操作)。
Dijkstra建议设立两种操作:down和up(分别为一般化后的sleep和wakeup)。对一个信号量执行down操作,则是检查其值是否大于0。若该值大于0,则将其减1(即用掉一个保存的唤醒信号)并继续;若该值为0,则进程将睡眠,而且此时down操作并未结束。检查数值、修改变量值以及可能发生的睡眠操作均作为一个单一的、不可分割的原子操作完成。保证一旦一个信号量操作开始,则在该操作完成或阻塞之前,其他进程均不允许访问该信号量。这种原子性对于解决同步问题和避免竞争条件是绝对必要的。所谓原子操作,是指一组相关联的操作要么都不间断地执行,要么不执行。
- 从物理上说明信号量的P、V操作的含义。 P(S)表示申请一个资源,S.value>0表示有资源可用,其值为资源的数目;S.value=0表示无资源可用;S.value<0, 则|S.value|表示S等待队列中的进程个数。V(S)表示释放一个资源,信号量的初值应该大于等于0。P操作相当于“等待一个信号”,而V操作相当于“发送一个信号”,在实现同步过程中,V操作相当于发送一个信号说合作者已经完成了某项任务,在实现互斥过程中,V操作相当于发送一个信号说临界资源可用了。实际上,在实现互斥时,P、V操作相当于申请资源和释放资源。
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <semaphore.h>
#define NUM 5
int queue[NUM]; //公共区资源(队列实现)
sem_t blank_num, product_num;
void err_thread(int ret, char *str)
{
if (ret != 0)
{
fprintf(stderr, "%s: %s\n", str, strerror(ret));
pthread_exit(NULL);
}
}
void *producer(void *arg)
{
int i = 0;
while (1)
{
sem_wait(&blank_num);
queue[i] = rand() % 1000 + 1;
printf("produce: %d\n", queue[i]);
sem_post(&product_num);
i = (i + 1) % NUM;
sleep(rand() % 3);
}
}
void *consumer(void *ard)
{
int i = 0;
while (1)
{
sem_wait(&product_num);
printf("consumer: %d\n", queue[i]);
queue[i] = 0;
sem_post(&blank_num);
i = (i + 1) % NUM;
sleep(rand() % 3);
}
}
int main()
{
pthread_t pid, cid;
int ret;
sem_init(&blank_num, 0, NUM);
sem_init(&product_num, 0, 0);
ret = pthread_create(&pid, NULL, producer, NULL);
err_thread(ret, "pthread_create producer error");
ret = pthread_create(&cid, NULL, consumer, NULL);
err_thread(ret, "pthread_create consumer error");
ret = pthread_join(pid, NULL);
err_thread(ret, "pthread_join producer error");
ret = pthread_join(cid, NULL);
err_thread(ret, "pthread_join consumer error");
sem_destroy(&blank_num);
sem_destroy(&product_num);
return 0;
}