信号量
**信号量的实现 **
二值信号量
一般信号量
#include
#include
#include
sem_t empty;
sem_t full;
void* producerThd(void* arg){
for(int i=0; i<10; i++){
printf("**Producing one item.**\n");
sem_wait(&empty);
printf("**PUTTING item to warehouse.**\n");
sem_post(&full);
}
pthread_exit(0);
}
void* consumerThd(void* arg){
for(int i=0; i<10; i++){
sem_wait(&full);
printf("##GETTING item from warehouse.##\n");
sem_post(&empty);
printf("##Consuming the item.##\n");
}
pthread_exit(0);
}
int main(int argc, char *argv[]) {
pthread_t producer_tid, consumer_tid;
sem_init(&empty, 0, 1);
sem_init(&full, 0, 0);
pthread_create(&producer_tid, NULL, producerThd, NULL);
pthread_create(&consumer_tid, NULL, consumerThd, NULL);
pthread_join(producer_tid, NULL);
pthread_join(consumer_tid, NULL);
sem_destroy(&empty);
sem_destroy(&full);
}
使用下面的命令进行编译生成可执行文件,然后运行程序。
gccproducerAndConsumer.c-oproducerAndConsumer-lpthread
#include
#include
#include
#include
void printBank();
sem_t empty;
sem_t full;
int Bank[10]={0};
int in=0,out=0;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
void* producerThd(void* arg){
for(int i=0; i<20; i++){
sem_wait(&empty);
pthread_mutex_lock(&lock); //临界区开始
Bank[in] = 1;
in = (in+1)%10;
printBank();
sleep(0.1);
pthread_mutex_unlock(&lock);//临界区结束
sem_post(&full);
}
pthread_exit(0);
}
void* consumerThd(void* arg){
for(int i=0; i<20; i++){
sem_wait(&full);
pthread_mutex_lock(&lock); //临界区开始
Bank[out] = 0;
out = (out+1)%10;
printBank();
sleep(1);
pthread_mutex_unlock(&lock);//临界区结束
sem_post(&empty);
}
pthread_exit(0);
}
/*该函数用以输出缓冲区的全部数值*/
void printBank(){
printf("Bank:");
for(int i=0; i<10; i++){
printf("[%d]",Bank[i]);
if(i==9) putchar('\n');
}
}
int main(int argc, char *argv[]) {
pthread_t producer_tid, consumer_tid;
sem_init(&empty, 0, 10);
sem_init(&full, 0, 0);
pthread_create(&producer_tid, NULL, producerThd, NULL);
pthread_create(&consumer_tid, NULL, consumerThd, NULL);
pthread_join(producer_tid, NULL);
pthread_join(consumer_tid, NULL);
sem_destroy(&empty);
sem_destroy(&full);
}
在该步实验中,我们用sleep()来调节生产和消费的速度,我们故意让生产比消费快一点,这样我们可以更好地观察运行结果。