生产者消费者的实验布置一周了,还不知怎么写,主要是些信息量的操作wait()signal()函数不会用。在网上找了一个源程序,包括windows下和linux下的具体实现。
把人家的东西帖过来,保存住。我只看了下linux下的程序,有些函数及其参数还是看不太 懂。一会把这个程序涉及的一些问题的相关知识贴在后几篇文章,学习。。。。
他的程序实现了
有一个生产者进程,有两个消费者进程。生产者产生1-100的100个数。两个消费者从共享内存中取数。
以下程序转载于:(操作系统中关于信号量操作的代码示例(Linux + windows)
)author:zhangwei
=======================================
windows下的程序:
#include "stdafx.h"
//作为仓库存放数据,最多可以放五个数据
int array[5];
//记录生成数据的个数
int pointer;
//记录取得的数据的位置
int pointerget;
//用来保存数据和
int sum;
//临界区对象
CRITICAL_SECTION csArray;
//句柄,保存Full信号量
HANDLE hFull;
//句柄,保存Empty信号量
HANDLE hEmpty;
//生产者函数
DWORD WINAPI Producer(LPVOID lpParam)
{
int i = 0;
pointer = 0;
while( i < 100 )
{
WaitForSingleObject(hEmpty,INFINITE);
EnterCriticalSection( &csArray);
array[(pointer++)%5] = i + 1;
LeaveCriticalSection( &csArray);
ReleaseSemaphore(hFull,1,NULL);
i++;
}
return 0;
}
//消费者函数A
DWORD WINAPI ConsumerA(LPVOID lpParam)
{
while(1)
{
WaitForSingleObject(hFull,INFINITE);
EnterCriticalSection( &csArray);
sum += array[(pointerget ++ )%5];
printf("ConsumerA get %d \n",array[(pointerget -1)%5]);
if(pointerget == 100)
printf("The sum is %d",sum);
LeaveCriticalSection( &csArray);
ReleaseSemaphore(hEmpty,1,NULL);
}
return 0;
}
//消费者函数B
DWORD WINAPI ConsumerB(LPVOID lpParam)
{
while(1)
{
WaitForSingleObject(hFull,INFINITE);
EnterCriticalSection( &csArray);
sum += array[(pointerget ++ )%5];
printf("ConsumerB get %d \n",array[(pointerget -1 )%5]);
if(pointerget == 100)
printf("The sum is %d",sum);
LeaveCriticalSection( &csArray);
ReleaseSemaphore(hEmpty,1,NULL);
}
return 0;
}
//主函数
void main()
{
HANDLE hThreadProducer,hThreadConsumerA,hThreadComsumerB;
sum = 0;
pointerget = 0;
InitializeCriticalSection( &csArray);
hFull = CreateSemaphore(NULL,0,5,NULL);
hEmpty = CreateSemaphore(NULL,5,5,NULL);
hThreadProducer = CreateThread(NULL,0,
Producer,NULL,0,NULL);
hThreadConsumerA = CreateThread(NULL,0,
ConsumerA,NULL,0,NULL);
hThreadComsumerB = CreateThread(NULL,0,
ConsumerB,NULL,0,NULL);
_getch();
}
=======================================
linux下的程序:
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXSEM 5
//声明三个信号灯ID
int fullid;
int emptyid;
int mutxid;
int main()
{
struct sembuf P,V;;
union semun arg;
//声明共享内存
int *array;
int *sum;
int *set;
int *get;
//映射共享内存
array = (int *)mmap(NULL , sizeof( int )*5,PROT_READ|PROT_WRITE,MAP_SHARED|MAP_ANONYMOUS,-1,0);
sum = (int *)mmap(NULL , sizeof( int ),PROT_READ|PROT_WRITE,MAP_SHARED|MAP_ANONYMOUS,-1,0);
get = (int *)mmap(NULL , sizeof( int ),PROT_READ|PROT_WRITE,MAP_SHARED|MAP_ANONYMOUS,-1,0);
set = (int *)mmap(NULL , sizeof( int ),PROT_READ|PROT_WRITE,MAP_SHARED|MAP_ANONYMOUS,-1,0);
*sum = 0;
*get = 0;
*set = 0;
//生成信号灯
fullid= semget(IPC_PRIVATE,1,IPC_CREAT|00666);
emptyid=semget(IPC_PRIVATE,1,IPC_CREAT|00666);
mutxid=semget(IPC_PRIVATE,1,IPC_CREAT|00666);
//为信号灯赋值
arg.val = 0;
if(semctl(fullid , 0 , SETVAL , arg) == -1) perror("semctl setval error");
arg.val = MAXSEM;
if(semctl(emptyid , 0 ,SETVAL , arg) == -1) perror("semctl setval error");
arg.val = 1;
if(semctl(mutxid , 0 ,SETVAL , arg) == -1) perror("setctl setval error");
//初始化P,V操作
V.sem_num=0;
V.sem_op =1;
V.sem_flg=SEM_UNDO;
P.sem_num=0;
P.sem_op =-1;
P.sem_flg=SEM_UNDO;
//生产者进程
if(fork() == 0 ) {
int i = 0;
while( i < 100)
{
semop(emptyid , &P ,1 );
semop(mutxid , &P , 1);
array[*(set)%MAXSEM] = i + 1;
printf("Producer %d\n", array[(*set)%MAXSEM]);
(*set)++;
semop(mutxid , &V , 1);
semop(fullid , &V , 1);
i++;
}
sleep(10);
printf("Producer is over");
exit(0);
}else {
//ConsumerA 进程
if(fork()==0) {
while(1){
semop(fullid , &P , 1);
semop(mutxid , &P , 1);
if(*get == 100)
break;
*sum += array[(*get)%MAXSEM];
printf("The ComsumerA Get Number %d\n", array[(*get)%MAXSEM] );
(*get)++;
if( *get ==100)
printf("The sum is %d \n ", *sum);
semop(mutxid , &V , 1);
semop(emptyid , &V ,1 );
sleep(1);
}
printf("ConsumerA is over");
exit(0);
}else {
//Consumer B进程
if(fork()==0) {
while(1){
semop(fullid , &P , 1);
semop(mutxid , &P , 1);
if(*get == 100)
break;
*sum += array[(*get)%MAXSEM];
printf("The ComsumerB Get Number %d\n", array[(*get)%MAXSEM] );
(*get)++;
if( *get ==100)
printf("The sum is %d \n ", *sum);
semop(mutxid , &V , 1);
semop(emptyid , &V ,1 );
sleep(1);
}
printf("ConsumerB is over");
exit(0);
}
}
}
// sleep(20);
return 0;
}
张伟的mail:dawei_zhang81@163.com