linux系統生產者-消費者,讀者-寫者,哲學家就餐 C語言實現
阿新 • • 發佈:2018-12-27
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define NUM 15
int queue[NUM];
sem_t blank_number;
sem_t product_number;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
static int p = 0;
static int c = 0;
//生產者執行緒關聯函式
void *produce1(void *arg)
{
while(1)
{
sem_wait(&blank_number);
pthread_mutex_lock(&lock);
queue[p] = rand()%1000;
printf("produce1 %d\n",queue[p]);
p = (p+1)%NUM;
pthread_mutex_unlock(&lock);
sem_post(&product_number);
//sleep(rand()%5);
}
pthread_exit(NULL);
}
void *produce2(void *arg)
{
while(1)
{
sem_wait(&blank_number);
pthread_mutex_lock(&lock);
queue[p] = rand()%1000;
printf("produce2 %d\n",queue[p]);
p = (p+1)%NUM;
pthread_mutex_unlock(&lock);
sem_post(&product_number);
//sleep(rand()%5);
}
pthread_exit(NULL);
}
void *consume1(void *arg)
{
while(1)
{
sem_wait(&product_number);
pthread_mutex_lock(&lock);
printf("consume1 %d\n",queue[c]);
c = (c+1)%NUM;
pthread_mutex_unlock(&lock);
sem_post(&blank_number);
sleep(rand()%5);
}
pthread_exit(NULL);
}
void *consume2(void *arg)
{
while(1)
{
sem_wait(&product_number);
pthread_mutex_lock(&lock);
printf("consum2 %d\n",queue[c]);
c = (c+1)%NUM;
pthread_mutex_unlock(&lock);
sem_post(&blank_number);
sleep(rand()%5);
}
pthread_exit(NULL);
}
int main()
{
pthread_t pid1;
pthread_t pid2;
pthread_t cid1;
pthread_t cid2;
int ret = 0;
//訊號量初始化
sem_init(&blank_number,0,NUM);
sem_init(&product_number,0,0);
ret = pthread_create(&pid1,NULL,produce1,NULL);
if(ret < 0)
{
printf("pthread_create error\n");
exit(0);
}
pthread_create(&pid2,NULL,produce2,NULL);
pthread_create(&cid1,NULL,consume1,NULL);
pthread_create(&cid2,NULL,consume2,NULL);
pthread_join(pid1,NULL);
pthread_join(pid2,NULL);
pthread_join(cid1,NULL);
pthread_join(cid2,NULL);
//釋放訊號量
sem_destroy(&blank_number);
sem_destroy(&product_number);
return 0;
}
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define NUM 15
int queue[NUM];
sem_t blank_number;
sem_t product_number;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
static int p = 0;
static int c = 0;
//生產者執行緒關聯函式
void *produce1(void *arg)
{
while(1)
{
sem_wait(&blank_number);
pthread_mutex_lock(&lock);
queue[p] = rand()%1000;
printf("produce1 %d\n",queue[p]);
p = (p+1)%NUM;
pthread_mutex_unlock(&lock);
sem_post(&product_number);
//sleep(rand()%5);
}
pthread_exit(NULL);
}
void *produce2(void *arg)
{
while(1)
{
sem_wait(&blank_number);
pthread_mutex_lock(&lock);
queue[p] = rand()%1000;
printf("produce2 %d\n",queue[p]);
p = (p+1)%NUM;
pthread_mutex_unlock(&lock);
sem_post(&product_number);
//sleep(rand()%5);
}
pthread_exit(NULL);
}
void *consume1(void *arg)
{
while(1)
{
sem_wait(&product_number);
pthread_mutex_lock(&lock);
printf("consume1 %d\n",queue[c]);
c = (c+1)%NUM;
pthread_mutex_unlock(&lock);
sem_post(&blank_number);
sleep(rand()%5);
}
pthread_exit(NULL);
}
void *consume2(void *arg)
{
while(1)
{
sem_wait(&product_number);
pthread_mutex_lock(&lock);
printf("consum2 %d\n",queue[c]);
c = (c+1)%NUM;
pthread_mutex_unlock(&lock);
sem_post(&blank_number);
sleep(rand()%5);
}
pthread_exit(NULL);
}
int main()
{
pthread_t pid1;
pthread_t pid2;
pthread_t cid1;
pthread_t cid2;
int ret = 0;
//訊號量初始化
sem_init(&blank_number,0,NUM);
sem_init(&product_number,0,0);
ret = pthread_create(&pid1,NULL,produce1,NULL);
if(ret < 0)
{
printf("pthread_create error\n");
exit(0);
}
pthread_create(&pid2,NULL,produce2,NULL);
pthread_create(&cid1,NULL,consume1,NULL);
pthread_create(&cid2,NULL,consume2,NULL);
pthread_join(pid1,NULL);
pthread_join(pid2,NULL);
pthread_join(cid1,NULL);
pthread_join(cid2,NULL);
//釋放訊號量
sem_destroy(&blank_number);
sem_destroy(&product_number);
return 0;
}