把人家的东西帖过来,保存住。我只看了下linux下的程序,有些函数及其参数还是看不太 懂。一会把这个程序涉及的一些问题的相关知识贴在后几篇文章,学习。。。。
他的程序实现了有一个生产者进程,有两个消费者进程。生产者产生1-100的100个数。两个消费者从共享内存中取数。
以下程序转载于:[url]http://www.5inet.net/System/Linux/025896.html[/url](操作系统中关于信号量操作的代码示例(Linux + windows))author:zhangwei
=======================================
windows下的程序:
int array[5];
int pointer;
int pointerget;
int sum;
CRITICAL_SECTION csArray;
HANDLE hFull;
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;
}
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;
hFull = CreateSemaphore(NULL,0,5,NULL);
hEmpty = CreateSemaphore(NULL,5,5,NULL);
Producer,NULL,0,NULL);
hThreadConsumerA = CreateThread(NULL,0,
ConsumerA,NULL,0,NULL);
hThreadComsumerB = CreateThread(NULL,0,
ConsumerB,NULL,0,NULL);
_getch();
}
#include <sys/types.h>
#include <linux/sem.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <time.h>
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");
if(semctl(emptyid , 0 ,SETVAL , arg) == -1) perror("semctl setval error");
if(semctl(mutxid , 0 ,SETVAL , arg) == -1) perror("setctl setval error");
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 ) {
while( i < 100)
{
semop(mutxid , &P , 1);
array[*(set)%MAXSEM] = i + 1;
printf("Producer %d\n", array[(*set)%MAXSEM]);
(*set)++;
semop(fullid , &V , 1);
i++;
}
sleep(10);
printf("Producer is over");
exit(0);
}else {
//ConsumerA 进程
if(fork()==0) {
while(1){
semop(mutxid , &P , 1);
break;
*sum += array[(*get)%MAXSEM];
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);
//Consumer B进程
if(fork()==0) {
while(1){
semop(mutxid , &P , 1);
break;
(*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;
}