多線程(生產者和消費者)

有一個有限緩衝區(這裏用有名管道實現FIFO式緩衝區)和兩個線程:生產者和消費者。它們不停地把產品放入緩衝區和從緩衝區中拿走產品。一個生產者在緩衝區滿的時候必須等待,一個消費者在緩衝區空的時候也必須等待。

信號量的考慮:這裏使用3個信號量avail和full分別用於解決生產者和消費者線程之間的同步問題,mutex是用於這兩個線程之間的互斥問題。其中avail表示有界緩衝區中的空單元數初始值爲N,full表示有界緩衝區非空單元數,初始值爲0;mutex是互斥信號量,初始值爲1。

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<pthread.h>
#include<errno.h>
#include<semaphore.h>
#include<sys/ipc.h>

#define MYFIFO "myfifo"  //緩衝區有名管道
#define BUFFER_SIZE 3    //緩衝區單元數
#define UNIT_SIZE 5      //每個單元的大小
#define RUN_TIME 30     //運行時間
#define DELAY_TIME_LEVELS 5.0     //週期的最大值

int fd;
time_t end_time;
sem_t mutex,full,avail;    //3個信號量

void *producer(void *arg)   //生產者線程
{
 int real_write;
 int delay_time = 0;
 
 while(time(NULL)<end_time)
 {
  delay_time = (int)(rand() * DELAY_TIME_LEVELS/(RAND_MAX)/2.0)+1;
  sleep(delay_time);
  sem_wait(&avail);    //p操作信號量avail和mutex
  sem_wait(&mutex);
  printf("/nProducer:delay = %d/n",delay_time);
  if((real_write = write(fd,"hello",UNIT_SIZE)) == -1)   //生產者寫入數據
  {
   if(errno == EAGAIN)
   {
    printf("The FIFO has not been read yet.Please try later/n");
   }
  }
  else
  {
   printf("Write %d to the FIFO/n",real_write);
  }
  sem_post(&full);   //v操作信號量full和mutex
  sem_post(&mutex);
 }
 pthread_exit(NULL);
}

void *customer(void *arg)   //消費者線程
{
 unsigned char read_buffer[UNIT_SIZE];
 int real_read;
 int delay_time;

 while(time(NULL)<end_time)
 {
  delay_time = (int)(rand()*DELAY_TIME_LEVELS/(RAND_MAX))+1;
  sleep(delay_time);
  sem_wait(&full);    //p操作信號量full和mutex
  sem_wait(&mutex);
  memset(read_buffer,0,UNIT_SIZE);
  printf("/nCustomer:delay = %d/n",delay_time);
  if((real_read = read(fd,read_buffer,UNIT_SIZE)) == -1)
  {
   if(errno == EAGAIN)
   {
    printf("No data yet/n");
   }
  }
  printf("Read %s from FIFO/n",read_buffer);
  sem_post(&avail);    //v操作信號量avail和mutex
  sem_post(&mutex);
 }
 pthread_exit(NULL);
}

int main()
{
 pthread_t thrd_prd_id,thrd_cst_id;
 pthread_t mon_th_id;
 int ret;
 srand(time(NULL));
 end_time = time(NULL) + RUN_TIME;
 if((mkfifo(MYFIFO,O_CREAT|O_EXCL)<0)&&(errno!=EEXIST))   //創建有名管道
 {
  printf("Cannot create fifo/n");
  return errno;
 }
 
 fd = open(MYFIFO,O_RDWR);   //打開管道
 if(fd == -1)
 {
  printf("Open fifo error/n");
  return fd;
 }
 
 ret = sem_init(&mutex,0,1);    //初始化互斥信號量爲1
 ret += sem_init(&avail,0,BUFFER_SIZE);   //初始化avail信號量爲n
 ret += sem_init(&full,0,0);   //初始化full信號量爲0

 if(ret != 0)
 {
  printf("Create producer thread error/n");
  return ret;
 }
 
 ret = pthread_create(&thrd_prd_id,NULL,producer,NULL);   //創建兩個線程
 if(ret!=0)
 {
  printf("Create producer thread error/n");
  return ret;
 }
 ret = pthread_create(&thrd_cst_id,NULL,customer,NULL);   
 if(ret!=0)
 {
  printf("Create customer thread error/n");
  return ret;
 }
 pthread_join(thrd_prd_id,NULL);
 pthread_join(thrd_cst_id,NULL);
 close(fd);
 unlink(MYFIFO);
 return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章