(1)定义信号量:
sem_t semaphore; //定义一个名为semaphore的信号量;
(2)初始化信号量int sem_init(sem_t *sem, int pshared, unsigned int value);
参数: sem_t* sem:要初始化的信号量; int pshared:pshared = 0; usigned int value:该信号信号量的初值; 返回值: a.返回0,表示初始化成功; b.返回-1,表示操作失败; (3)P操作 int sem_wait(sem_t* sem); //对于信号量sem进行P操作; int sem_trywait(sem_t* sem);
(4)V操作
int sem_post(sem_t* sem); //对于信号量sem进行V操作 生产者-消费者模型: 交易场所:环形队列; 注:使用信号量进行互斥和同步; 单进程单生产者--单消费者代码实现: producter_consumer.c #include<stdio.h> #include<pthread.h> #include<semaphore.h> sem_t blank_count; sem_t data_count; int cycle_queue[20]; void* producter_running(void* arg) { int step = 0; while(1) { sem_wait(&blank_count); //P int data = rand()00; cycle_queue[step] = data; step++; step %= 20; printf("the producter is %d\n",data); sem_post(&data_count); //V } } void* consumer_running(void* arg) { int step =0; while(1) { sleep(1); sem_wait(&data_count); int data = cycle_queue[step]; step++; step %= 20; printf("the consumer is %d\n",data); sem_post(&blank_count); } } //producter_consumer based on single thread int main() { pthread_t producter1; pthread_t consumer1; sem_init(&blank_count,0,20); sem_init(&data_count,0,0); pthread_create(&producter1,NULL,producter_running,NULL); pthread_create(&consumer1,NULL,consumer_running,NULL); pthread_join(producter1,NULL); pthread_join(consumer1,NULL); return 0; } Makefile: producter_consumer:producter_consumer.c gcc -o $@ $^ -lpthread .PHONY:clean clean: rm -f producter_consumer 运行结果:
单进程多生产者多消费者的生产者消费者模型:
producter_consumer.c
#include<stdio.h> #include<pthread.h> #include<semaphore.h> sem_t blank_count; sem_t data_count; int cycle_queue[20]; pthread_mutex_t pro = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t con = PTHREAD_MUTEX_INITIALIZER; void* producter_running(void* arg) { static int step = 0; while(1) { pthread_mutex_lock(&pro); sem_wait(&blank_count); //P int data = rand()00; cycle_queue[step] = data; step++; step %= 20; printf("producter:pthread_id:%u--->data:%d\n",pthread_self(),data); sem_post(&data_count); //V pthread_mutex_unlock(&pro); } return NULL; } void* consumer_running(void* arg) { static int step =0; while(1) { pthread_mutex_lock(&con); sem_wait(&data_count); int data = cycle_queue[step]; step++; step %= 20; printf("consumeer;pthread_id:%u--->data:%d\n",pthread_self(),data); sem_post(&blank_count); pthread_mutex_unlock(&con); sleep(1); } } //producter_consumer based on single thread int main() { pthread_t producter1; pthread_t producter2; pthread_t consumer1; pthread_t consumer2; sem_init(&blank_count,0,20); sem_init(&data_count,0,0); pthread_create(&producter1,NULL,producter_running,NULL); pthread_create(&producter2,NULL,producter_running,NULL); pthread_create(&consumer1,NULL,consumer_running,NULL); pthread_create(&consumer2,NULL,consumer_running,NULL); pthread_join(producter1,NULL); pthread_join(producter2,NULL); pthread_join(consumer1,NULL); pthread_join(consumer2,NULL); sem_destroy(&blank_count); sem_destroy(&data_count); pthread_mutex_destroy(&pro); pthread_mutex_destroy(&con); return 0; } 运行结果:
作者水平有限,若有问题,请留言,谢谢!!!