http://blog.csdn.net/life_hunter/article/details/8790553
1.生产者-消费者问题
-
-
-
-
-
-
-
-
- #include
- #include
- #include
- #include
- #include
-
- void *producer_handler(void *ptr);
- void *consumer_handler(void *ptr);
-
- sem_t mutex,blank,fill;
- int *buffer;
- int in=0,out=0,buffer_size=10;
-
- void main()
- {
- if((buffer=(int *)malloc(buffer_size*sizeof(int)))==NULL)
- printf("can't allocate memroy on heap\n");
- sem_init(&mutex,0,1);
- sem_init(&blank,0,buffer_size);
- sem_init(&fill,0,0);
-
- int err;
- pthread_t producer,consumer;
- err=pthread_create(&producer,NULL,producer_handler,NULL);
- if(err!=0)
- err_quit("can't create thread: %s\n",strerror(err));
- err=pthread_create(&consumer,NULL,consumer_handler,NULL);
- if(err!=0)
- err_quit("can't create thread: %s\n",strerror(err));
-
- sleep(10);
-
- }
-
- void *producer_handler(void *ptr)
- {
- while(1){
- static int data=0;
- sem_wait(&blank);
-
-
- buffer[in]=++data;
- printf("%d has been input to the buffer\n",data);
- in=(in+1+buffer_size)%buffer_size;
-
- sem_post(&fill);
- sleep(1);
- }
- return ((void *)0);
- }
-
- void *consumer_handler(void *ptr)
- {
- while(1){
- int fetch;
- sem_wait(&fill);
-
- fetch=buffer[out];
- out=(out+1+buffer_size)%buffer_size;
- printf("%d has been fetched\n",fetch);
-
- sem_post(&blank);
- sleep(2);
- }
- return ((void *)0);
- }
2.读者-写者问题
-
-
-
-
-
-
-
-
-
- #include
- #include
- #include
- #include
- #include
-
- #define READER_NUM 5
- void *reader_handler(void *ptr);
- void *writer_handler(void *ptr);
-
- sem_t write_mutex;
- sem_t mutex;
- int read_count;
-
- void main()
- {
- sem_init(&write_mutex,0,1);
- sem_init(&mutex,0,1);
- read_count=0;
-
- int err,i;
- pthread_t reader[READER_NUM],writer;
- int args[READER_NUM];
- for(i=0;i
- args[i]=i;
- err=pthread_create(&reader[i],NULL,reader_handler,(void*)&args[i]);
- if(err!=0)
- err_quit("can't create thread: %s\n",strerror(err));
- }
- err=pthread_create(&writer,NULL,writer_handler,NULL);
- if(err!=0)
- err_quit("can't create thread: %s\n",strerror(err));
-
- sleep(10);
-
- }
-
- void *reader_handler(void *ptr)
- {
- while(1){
- if(read_count==0){
- sem_wait(&write_mutex);
- }
- sem_wait(&mutex);
- ++read_count;
- sem_post(&mutex);
-
- printf("There are %d readers reading\n",read_count);
- sleep(1);
-
- sem_wait(&mutex);
- --read_count;
- sem_post(&mutex);
-
- if(read_count==0){
- sem_post(&write_mutex);
- }
- sleep(1);
- }
- return ((void *)0);
- }
-
- void *writer_handler(void *ptr)
- {
- while(1){
- if(read_count==0){
- sem_wait(&write_mutex);
- printf("Writer writes\n");
- sem_post(&write_mutex);
- }
- else
- printf("Writer failed\n");
- sleep(1);
- }
- return ((void *)0);
- }
3.哲学家进餐问题
有一点要注意:
就是循环生生成线程时,要给线程处理函数传入计数器参数时,不能直接用计数器的地址——因为计数器会现在主函数中改变。显然也不能用循环中的临时变量来记录。所以只好再有外部数组来记录这些实参值了。
-
-
-
-
-
-
-
-
-
- #include
- #include
- #include
- #include
- #include
-
- #define PHILO_NUM 5
- void *philosopher(void *arg);
-
- sem_t sema[PHILO_NUM];
- sem_t mutex;
- void main()
- {
- int i,err;
- int args[PHILO_NUM];
- for(i=0;i
- sem_init(sema+i,0,1);
- args[i]=i;
- }
- sem_init(&mutex,0,1);
- pthread_t philosophers[PHILO_NUM];
- for(i=0;i
- err=pthread_create(philosophers+i,NULL,philosopher,(void*)&args[i]);
- if(err!=0)
- err_quit("can't create thread: %s\n",strerror(err));
- }
- sleep(5);
- }
-
- void *philosopher(void *arg)
- {
- int num;
- num=*((int *)arg);
-
- while(1){
- sem_wait(&mutex);
- sem_wait(&sema[num]);
- sem_wait(&sema[(num+1)%PHILO_NUM]);
- sem_post(&mutex);
- printf("Philosopher %d eats\n",num);
- sem_post(&sema[num]);
- sem_post(&(sema[(num+1)%PHILO_NUM]));
- sleep(1);
- }
-
- return ((void *)0);
- }
哲学家问题解决:
comments:5个人5副刀叉。4个人吃饭毕竟不向5个人,肯定至少有一个会吃到,然后放下,然后其他的人就不会一直hold,所以让其中之一先sleep一下。即不会死锁。
但是从理论上讲,只是开始的时候,4人同吃,不会出现死锁。
但是过了id=0,sleep之后,又变成5个人同吃。还是可能存在死锁的可能。
#include
#include
#include
#include
#include
#include
#define PHILOS 5
#define DELAY 5000
#define FOOD 50
void *philosopher (void *id);
void grab_chopstick(int ,
int ,
char *);
void down_chopsticks (int ,
int);
int food_on_table();
pthread_mutex_t chopstick[PHILOS];
pthread_t philo[PHILOS];
pthread_mutex_t food_lock;
int sleep_seconds=10;
int main (int argn,char **argv)
{
int i;
if (argn==2)
sleep_seconds=atoi(argv[1]);
pthread_mutex_init(&food_lock,NULL);
for (i=0;i0) {
food--;
}
myfood=food;
pthread_mutex_unlock(&food_lock);
return myfood;
}
void grab_chopstick(int phi1,
int c,