⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 producer & consumer.txt

📁 使用POSIX线程(或进程)和SYSTEM V信号量实现生产者消费者问题。要求: 1. 有界缓冲区内设有5个存储单元
💻 TXT
字号:
#include<stdio.h>
#include<pthread.h>
#include<sys/types.h>
#include<linux/sem.h>
#include<unistd.h>

void P(int semid,int index)
{
   struct sembuf sem;
   sem.sem_num=index;
   sem.sem_op=-1;
   sem.sem_flg=0;
   semop(semid,&sem,1);
   return;
}
void V(int semid,int index)
{ 
   struct sembuf sem;
   sem.sem_num=index;
   sem.sem_op=1;
   sem.sem_flg=0;
   semop(semid,&sem,1);
   return;
}
#define N 5
int a[N];
int semid;
int in,out
void *producer(void *arg)
{
   int i,j;
   i=5;
   while(i--)
   {
      P(semid,1);
      P(semid,0);
      a[in]=i+1;
      printf("producer %d produce the %dth data %d     ",pthread_self(),in,a[in]);
      for(j=0;j<N;j++)
      printf("%d     ",a[j]);
      printf("\n\n");
      in=(in+1)%N;
      V(semid,0);
      V(semid,2);
      sleep(3);
   }
}
void *consumer(void *arg)
{
   int i,j,s;
   i=5;
   while(i--)
   {
      P(semid,2);
      P(semid,0);
      s=a[out];
      printf("consumer %d consume the %dth data %d     ",pthread_self(),out,a[out]);
      a[out]=0;
      for(j=0;j<N;j++)
      printf("%d     ",a[j]);
      printf("\n\n");
      out=(out+1)%N;
      V(semid,0);
      V(semid,1);
      sleep(3);
   }
}
int main(void)
{
   union semun semopts;
   int res,i;
   pthread_t t1[2],t2[2];
   semid=semget(300,3,IPC_CREAT|0666);
   if(semid<0)
   {  printf("error");
      return;
   }
   semopts.val=1;
   res=semctl(semid,0,SETVAL,semopts);
   if(res<0) return;
   semopts.val=N;
   res=semctl(semid,1,SETVAL,semopts);
   if(res<0) return;
   semopts.val=0;
   res=semctl(semid,2,SETVAL,semopts);
   if(res<0) return;
   for(i=0;i<2;i++)
   {  if(pthread_create(&t1[i],NULL,producer,NULL))
      printf("error creating thread");
   }
   for(i=0;i<2;i++)
   {  if(pthread_create(&t2[i],NULL,consumer,NULL))
      printf("error creating thread");
   }
   semctl(semid,0,IPC_RMID,0);
   semctl(semid,1,IPC_RMID,0);
   semctl(semid,2,IPC_RMID,0);
   exit(0);
}

输出结果:
producer 1082318016 produce the 0th data 5 5 0 0 0 0
producer 1090706496 produce the 1th data 5 5 5 0 0 0
consumer 1099094976 consume the 0th data 5 0 5 0 0 0
consumer 1116941120 consume the 1th data 5 0 0 0 0 0
producer 1082318016 produce the 2th data 4 0 0 4 0 0
producer 1090706496 produce the 3th data 4 0 0 4 4 0
consumer 1099094976 consume the 2th data 4 0 0 0 4 0
consumer 1116941120 consume the 3th data 4 0 0 0 0 0
producer 1082318016 produce the 4th data 3 0 0 0 0 3
producer 1090706496 produce the 0th data 3 3 0 0 0 3
consumer 1099094976 consume the 4th data 3 3 0 0 0 0 
consumer 1116941120 consume the 0th data 3 0 0 0 0 0
producer 1082318016 produce the 1th data 0 2 2 0 0 0
producer 1090706496 produce the 2th data 0 2 2 2 0 0
consumer 1099094976 consume the 1th data 0 0 2 2 0 0
consumer 1116941120 consume the 2th data 2 0 0 0 0 0
producer 1082318016 produce the 3th data 0 0 0 1 1 0
producer 1090706496 produce the 4th data 0 0 0 1 1 1
consumer 1099094976 consume the 3th data 0 0 0 0 1 1
consumer 1116941120 consume the 4th data 1 0 0 0 0 0

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -