#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#define MAX 5 //缓冲区长度
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; //互斥信号量
sem_t full; //资源信号量,满缓冲区的个数
sem_t empty; //资源信号量,空缓冲区的个数
int in = 0; //队尾
int out = 0; //队头
void* produce(void* arg)
{
int i;
for ( i = 0; i < MAX*2; i++) //
{
printf("producer is preparing data\n");
sem_wait(&empty); //若空缓冲区个数低于0阻塞
pthread_mutex_lock(&mutex); //对缓冲区关锁
in = (in+1) % MAX;
printf("now in is %d\n", in);
pthread_mutex_unlock(&mutex);//对缓冲区开锁
sem_post(&full); // 通知消费者进程,数据个数增加了一个
}
return (void*)1; //void *为“无类型指针”,void *可以指向任何类型的数据。
}
void* consume(void* arg)
{
int i;
for ( i = 0; i < MAX*2; i++)
{
printf("consumer is preparing consume data\n");
sem_wait(&full); //若满缓冲区个数低于0阻塞
pthread_mutex_lock(&mutex); //对缓冲区关锁
out = (out +1) % MAX;
printf("now out is %d\n", out );
pthread_mutex_unlock(&mutex); //对缓冲区开锁
sem_post(&empty); //通知生产者进程,空缓冲区个数增加一个
}
return (void*)2;
}
int main(int argc, char *argv[])
{
pthread_t thid1;
pthread_t thid2;
pthread_t thid3;
pthread_t thid4;
int ret1;
int ret2;
int ret3;
int ret4;
sem_init(&full, 0, 0);
sem_init(&empty, 0, MAX);
pthread_create(&thid1, NULL, produce, NULL);
pthread_create(&thid2, NULL, consume, NULL);
pthread_create(&thid3, NULL, produce, NULL);
pthread_create(&thid4, NULL, consume, NULL);
pthread_join(thid1, (void**)&ret1);
pthread_join(thid2, (void**)&ret2);
pthread_join(thid3, (void**)&ret3);
pthread_join(thid4, (void**)&ret4);
return 0;
}
实验四
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <pthread.h>
#include <errno.h>
#include <math.h>
pthread_mutex_t chopstick[5]; //筷子作为互斥资源,设为mutex
void* eat_think(void* arg)
{
char phi=*(char*)arg;
int left,right;//左右筷子的编号
switch(phi){
case'A':
left =4;
right = 0;
break;
case 'B':
left = 0;
right = 1;
break;
case 'C':
left = 1;
right = 2;
break;
case 'D':
left = 2;
right = 3;
break;
case 'E':
left = 3;
right = 4;
break;
}
int i;
for(;;){
usleep(30);
pthread_mutex_lock(&chopstick[left]); //拿起左手的筷子
printf("Philosopher %c fetches chopstick %d\n",phi,left);
if (pthread_mutex_trylock(&chopstick[right]) == EBUSY){
//尝试拿起右手的筷子,如果右边筷子被拿走放下左手的筷子
pthread_mutex_unlock(&chopstick[left]);
printf("Philosopher %c release chopstick %d\n", phi, left);
continue;
}
pthread_mutex_lock(&chopstick[right]); //拿起右手的筷子,如观察死锁,保留这一语句,把if语句作为注释
printf("Philosopher %c fetches chopstick %d\n", phi, right);
printf("Philosopher %c is eating.\n",phi);
usleep(30); //吃饭
pthread_mutex_unlock(&chopstick[left]); //放下左手的筷子
printf("Philosopher %c release chopstick %d\n", phi, left);
pthread_mutex_unlock(&chopstick[right]); //放下右手的筷子
printf("Philosopher %c release chopstick %d\n", phi, right);
}
}
int main(){
pthread_t A,B,C,D,E; //定义5个哲学家线程
int i;
for (i = 0; i < 5; i++)
pthread_mutex_init(&chopstick[i],NULL);
pthread_create(&A,NULL, eat_think, "A");
pthread_create(&B,NULL, eat_think, "B");
pthread_create(&C,NULL, eat_think, "C");
pthread_create(&D,NULL, eat_think, "D");
pthread_create(&E,NULL, eat_think, "E");
pthread_join(A,NULL);
pthread_join(B,NULL);
pthread_join(C,NULL);
pthread_join(D,NULL);
pthread_join(E,NULL);
return 0;
}