一步一步炼代码之多线程篇(二)经典PV问题

来源:互联网 发布:软件项目管理期末试卷 编辑:程序博客网 时间:2024/06/03 20:24

一 生产者消费者问题

环境:linux gcc vim

#include<stdio.h>#include<unistd.h>#include<pthread.h>#include<sys/ipc.h>#include<semaphore.h>#include<errno.h>#include<fcntl.h>#define N 5int count = 0;time_t time_end;sem_t mutex,empty,full;void productor(void* args);void customer(void* args);int main(){  int ret;  pthread_t id1,id2;  time_end = time(NULL)+30;  sem_init(&mutex,0,1);  sem_init(&empty,0,N);  sem_init(&full,0,0);  ret = pthread_create(&id1,NULL,(void*)productor,NULL);  if(!ret)    printf("thread of productor is created successfully.\r\n");  ret = pthread_create(&id2,NULL,(void*)customer,NULL);  if(!ret)    printf("thread of customer is created successfully.\r\n");  pthread_join(id1,NULL);  printf("thread productor is return\r\n");  pthread_join(id2,NULL);  printf("thread customer is return\r\n");  return 0;}void productor(void* args){  while(time(NULL)<time_end)  {    sem_wait(&empty);    sem_wait(&mutex);    if(count>=N)      printf("FULL\r\n");    else    {      count++;      printf("productor::count is %d\r\n",count);    }    sem_post(&mutex);    sem_post(&full);    sleep(1);  }  printf("thread productor is over\r\n");}void customer(void* args){  while(time(NULL)<time_end||count>0)  {    sem_wait(&full);    sem_wait(&mutex);    if(count<=0)      printf("EMPTY\r\n");    else    {      count--;      printf("customer::count is %d\r\n",count);    }    sem_post(&mutex);    sem_post(&empty);    sleep(2);  } printf("thread customer is over\r\n");}

编译选项:gcc ProductorCustomer.c -lpthread -o mainApp

运行结果:

productor::count is 1thread of productor is created successfully.customer::count is 0thread of customer is created successfully.productor::count is 1customer::count is 0productor::count is 1productor::count is 2customer::count is 1productor::count is 2productor::count is 3customer::count is 2productor::count is 3productor::count is 4customer::count is 3productor::count is 4productor::count is 5customer::count is 4productor::count is 5customer::count is 4productor::count is 5customer::count is 4productor::count is 5customer::count is 4productor::count is 5customer::count is 4productor::count is 5customer::count is 4productor::count is 5customer::count is 4productor::count is 5customer::count is 4productor::count is 5customer::count is 4productor::count is 5customer::count is 4productor::count is 5thread productor is overthread productor is returncustomer::count is 4customer::count is 3customer::count is 2customer::count is 1customer::count is 0thread customer is overthread customer is return


二 理发师问题

环境:linux gcc vim

#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <pthread.h>#include <errno.h>#include <sys/ipc.h>#include <semaphore.h>#include <fcntl.h>#define  n 5time_t end_time; /*end time*/sem_t mutex,customers,barbers;int count = 0;/*等待理发的顾客数*/void barber(void* arg);void customer(void* arg);int main(int argc, char *argv[]){    pthread_t id1,id2;    int ret = 0;    end_time = time(NULL)+20;    /*初使化信号量的个数*/    sem_init(&mutex,0,1);    sem_init(&customers,0,0);    ret = sem_init(&barbers,0,1);    if(0!=ret)        perror("sem init.");    /*初使化两个线程*/    ret = pthread_create(&id1,NULL,(void *)barber,NULL);    if(0!=ret)        perror("create barbers.");    ret = pthread_create(&id2,NULL,(void *)customer,NULL);    if(0!=ret)        perror("create customers.");    /*让顾客进程先阻塞*/    pthread_join(id2,NULL);    pthread_join(id1,NULL);    exit(0);}/*理发师进程*/void barber(void *arg){  while(time(NULL)<end_time||count>0)  {    sem_wait(&customers);/*p(customers)*/    sem_wait(&mutex);/*p(mutex)*/    count--;    printf("Barber:cut hair,count is:%d.\n",count);    sem_post(&mutex);/*v(mutex);*/    sem_post(&barbers);/*v(barbers) 通知已进来的,在等理发师的顾客进程*/    sleep(3);  }}/*customer*//*顾客进程*/void customer(void *arg){  while(time(NULL)<end_time)  {    sem_wait(&mutex);/*p(mutex)*/    if(count<n)    {      count++;      printf("Customer:add count,count is:%d.\n",count);      sem_post(&mutex);/*v(mutex)*/      sem_post(&customers);/*v(customers)*/      sem_wait(&barbers);/*p(barbers)*/    }    else    {      /*v(mutex)  注意 如果个客户数已满了,则把互斥锁放掉,*/      sem_post(&mutex);    }    sleep(1);  }/*end of     while(time()<end_time)*/}/*customer*/

编译选项:gcc SleepBarber.c -lpthread -o mainApp


运行结果:

Customer:add count,count is:1.Barber:cut hair,count is:0.Customer:add count,count is:1.Customer:add count,count is:2.Barber:cut hair,count is:1.Customer:add count,count is:2.Barber:cut hair,count is:1.Customer:add count,count is:2.Barber:cut hair,count is:1.Customer:add count,count is:2.Barber:cut hair,count is:1.Customer:add count,count is:2.Barber:cut hair,count is:1.Customer:add count,count is:2.Barber:cut hair,count is:1.Customer:add count,count is:2.Barber:cut hair,count is:1.Barber:cut hair,count is:0.




0 0
原创粉丝点击