C语言实现生产者消费者进程同步问题?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言实现生产者消费者进程同步问题?相关的知识,希望对你有一定的参考价值。
问题:有1 个生产者和1 个消费者,至多有10 个商品可以生产,利用信号量对空缓冲区和满缓冲区进行表示。假设生产者和消费者权限相等,只有缓冲区未满,生产者可生成商品放入缓冲池,只要缓冲池未空,消费者则可消费商品。
代码如下:
#include <stdio.h>
#include <windows.h>
#define n 10 //缓冲区容量大小
typedef int semaphore; //信号量
int in=0, out=0;
semaphore mutex=1, empty=n, full=0,buffer[n],front = 0, rear = 0;
void producer();//生产者进程
void consumer();//消费者进程
void wait(semaphore&pass)// P操作
pass--;
void signal(semaphore&release) // V操作
release++;
void produce_item(char* item_ptr) //生产者生产 并标记为T True 代表该处已满
*item_ptr='T';
void put_buffer(int pass) //生产者将物品放入缓冲区
buffer[front]=pass;
printf("produce is %c into buffer[%d]\n",buffer[front],front);
front=(front +1)%n;
void remove_buffer(char* release) //消费者消费缓冲区,并标记为F false 代表该处已空
printf("consumer use is %c from buffer[%d]\n",buffer[rear], rear);
*release = buffer[rear];
buffer[rear] = 'F';
printf("now the buffer[%d] is %c !\n",rear, buffer[rear]);
rear=(rear+1)%n;
void consume_item()
printf("consumer had use the product\n\n");
void proceducer()
char item;
while (1)
Sleep(1000);
produce_item(&item);
wait(empty);
wait(mutex);
put_buffer(item);
signal(mutex);
signal(full);
if (full==n)
consumer();
void consumer()
char get_item;
while (1)
Sleep(1000);
wait(full);
wait(mutex);
remove_buffer(&get_item);
signal(mutex);
signal(empty);
consume_item();
if (empty==n)
producer();
int main()
producer();
return 0;
为什么报错 error: syntax error before '&' token
而且运行不了求大神指点
稍微修改了一点,好像没什么大问题。
参考技术A 你要是C语言是没有semophore &这种语法的,这是C++的语法#include <stdio.h>
#include <windows.h>
#define n 10 //缓冲区容量大小
typedef int semaphore; //信号量
int in = 0, out = 0;
semaphore mutex = 1, empty = n, full = 0, buffer[n], front = 0, rear = 0;
void proceducer();
void consumer();
void wait(semaphore&pass) // P操作
pass--;
void signal(semaphore&release) // V操作
release++;
void produce_item(char* item_ptr) //生产者生产 并标记为T True 代表该处已满
*item_ptr = 'T';
void put_buffer(int pass) //生产者将物品放入缓冲区
buffer[front] = pass;
printf("produce is %c into buffer[%d]\\n", buffer[front], front);
front = (front + 1) % n;
void remove_buffer(char* release) //消费者消费缓冲区,并标记为F false 代表该处已空
printf("consumer use is %c from buffer[%d]\\n", buffer[rear], rear);
*release = buffer[rear];
buffer[rear] = 'F';
printf("now the buffer[%d] is %c !\\n", rear, buffer[rear]);
rear = (rear + 1) % n;
void consume_item()
printf("consumer had use the product\\n\\n");
void proceducer()
char item;
while (1)
Sleep(1000);
produce_item(&item);
wait(empty);
wait(mutex);
put_buffer(item);
signal(mutex);
signal(full);
if (full == n)
consumer();
void consumer()
char get_item;
while (1)
Sleep(1000);
wait(full);
wait(mutex);
remove_buffer(&get_item);
signal(mutex);
signal(empty);
consume_item();
if (empty == n)
proceducer();
int main()
proceducer();
return 0;
求用c语言实现消费者和生产者的问题,简单点,就用一个生产者和一个消费者的两进程同步算法,谢谢啦各位
参考技术A #include<stdio.h>#include<stdlib.h>
int mutex=1,full=0,empty=3,x=0;
int main()
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit");
while(1)
printf("\nEnter your choice:");
scanf("%d",&n);
switch(n)
case 1: if((mutex==1)&&(empty!=0))
producer();
else
printf("Buffer is full!!");
break;
case 2: if((mutex==1)&&(full!=0))
consumer();
else
printf("Buffer is empty!!");
break;
case 3:
exit(0);
break;
return 0;
int wait(int s)
return (--s);
int signal(int s)
return(++s);
void producer()
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nProducer produces the item %d",x);
mutex=signal(mutex);
void consumer()
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\nConsumer consumes item %d",x);
x--;
mutex=signal(mutex);
以上是关于C语言实现生产者消费者进程同步问题?的主要内容,如果未能解决你的问题,请参考以下文章
求用c语言实现消费者和生产者的问题,简单点,就用一个生产者和一个消费者的两进程同步算法,谢谢啦各位