data_deal.c

来自「一个编程中常用的PV操作(生产者—消费者)的c语言的demo」· C语言 代码 · 共 33 行

C
33
字号
#include "outshm.h"

int main(void)
{
	struct exchange *shm;
	int producer, consumer, i;
	
	//creat and init consumer
	consumer = open_semaphore_set(ftok("consumer",0),1);
	init_a_semaphore(consumer,0,1);
	
	//creat and init producer
	producer = open_semaphore_set(ftok("producer",0),1);
	init_a_semaphore(producer,0,1);
	
	//get and connect the shm named"shared"
	shm = (struct exchange *)shminit(ftok("shared",0));
	
	//read data from the shm written by the assistant process,output it
	for(i=0; ; i++)
	{
		semaphore_V(consumer);
		semaphore_P(producer);
		
		//deal with the data,end loop with "end"
		printf("data received:%s,sequence:%d\n",shm->buf,shm->seq);
		if (strcmp(shm->buf,"end",3) == 0)
			break;
	}
	rm_semaphore(producer);
	rm_semaphore(consumer);
	exit(0);
}

⌨️ 快捷键说明

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