#include
#include
#include
#include
#include
#include
#include
#include
#define MAXSEM 5
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 semval 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");
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<30){
semop(emptyid,&P,1);
semop(mutxid,&P,1);
array[*(set)%MAXSEM]=i+1;
printf("Producer %dn",array[(*set)%MAXSEM]);
(*set)++;
semop(mutxid,&V,1);
semop(fullid,&V,1);
i++;
}
sleep(10);
printf("Producer is over");
exit(0);
}else{
if(fork()==0){
while(1){
semop(fullid,&P,1);
semop(mutxid,&P,1);
if(*get==30)
break;
*sum+=array[(*get)%MAXSEM];
printf("The ConsumerA Get Number %dn",array[(*get)%MAXSEM]);
(*get)++;
semop(mutxid,&V,1);
semop(emptyid,&V,1);
sleep(1);
}
printf("ConsumerA is over");
exit(0);
}else{
if(fork()==0){
while(1){
semop(fullid,&P,1);
semop(mutxid,&P,1);
if(*get==30)
break;
*sum+=array[(*get)%MAXSEM];
printf("The ConsumerB Get Number %dn",array[(*get)%MAXSEM]);
(*get)++;
semop(mutxid,&V,1);
semop(emptyid,&V,1);
sleep(1);
}
printf("ConsumerB is over");
exit(0);
}
}
}
return 0;
}
第二题
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int numOfEmptyPlate_sem_id = 0; //number of empty plates.
int numOfApple_sem_id = 0; //number of apple
int numOfOrange_sem_id = 0; //number of orange
int getAccesstoPlate = 0; //get plate
int *plate = NULL; //3 PLATES
struct sembuf P, V; // p,v opration
void printPlates(const char *arg)
{
int i = 0;
for(i=0; i<3; i++)
{
}
printf("n");
}
void father_do() // PUT FRUITS in
{// 1 == apple, 2 == range
int i = 0;
semop( numOfEmptyPlate_sem_id, &P, 1); // P(empty plates)
if( rand()%100+1 > 50) //get an orange
{
sleep(1); //get an orange,sleep 1 seconds
semop( getAccesstoPlate, &P, 1); //P(plate)
printPlates("father:");
for(i=0; i<3; i++)
{
if( 0 == plate[i]) //find an empty plate, and put the furit in
{
plate[i] = 2;
break;
}
}
semop( numOfOrange_sem_id, &V, 1); //V(oranges)
printf("Father put an orange in plate.n");
semop( getAccesstoPlate, &V, 1); //V(plate)
}
else // get an apple
{
sleep(2); //get an apple,sleep 2 seconds
semop( getAccesstoPlate, &P, 1); //P(plate)
printPlates("father:");
for(i=0; i<3; i++)
{
if( 0 == plate[i]) //find an empty plate, and put the furit in
{
plate[i] = 1;
break;
}
}
semop( numOfApple_sem_id, &V, 1); //V(apples)
printf("Father put an apple in plate.n");
semop( getAccesstoPlate, &V, 1); //V(plate)
}
}
void girl_do() //EAT ORAGERS
{
int i = 0;
semop( numOfOrange_sem_id, &P, 1); //P(oranges)
sleep(2); //girl eat slowly,sleep 2 seconds
semop( getAccesstoPlate, &P, 1); //P(plate)
printPlates("girl:");
for( i=0; i<3; i++)
{
if( 2 == plate[i] )
{
plate[i] = 0; // eat an orange
break;
}
}
semop( numOfEmptyPlate_sem_id, &V, 1); // V(empty plates)
printf("Girl ate an orange.n");
semop( getAccesstoPlate, &V, 1); //V(plate)
}
void boy_do() // EAT APPLES
{
int i = 0;
semop( numOfApple_sem_id, &P, 1); //P(apple)
sleep(1); //boy eat fast,sleep 1 seconds
semop( getAccesstoPlate, &P, 1); //P(plate)
printPlates("boy:");
for( i=0; i<3; i++)
{
if( 1 == plate[i] )
{
plate[i] = 0; // eat an apple
break;
}
}
semop( numOfEmptyPlate_sem_id, &V, 1); // V(empty plates)
printf("The son ate an orange.n");
semop( getAccesstoPlate, &V, 1); //V(plate)
}
int main()
{
pid_t father_id;
pid_t girl_id;
pid_t boy_id;
char op =' ';
srand( time(0)); // set rand seed
plate=(int *)mmap(NULL, sizeof(int)*3, PROT_READ|PROT_WRITE,
MAP_SHARED|MAP_ANONYMOUS,-1,0); //GET SHARE MEMORY
plate[0] = 0;
plate[1] = 0;
plate[2] = 0; //all plates are empty.
numOfApple_sem_id = semget( IPC_PRIVATE, 1, IPC_CREAT|00666);
numOfEmptyPlate_sem_id = semget( IPC_PRIVATE, 1, IPC_CREAT|00666);
numOfOrange_sem_id = semget( IPC_PRIVATE, 1, IPC_CREAT|00666);
getAccesstoPlate = semget( IPC_PRIVATE, 1, IPC_CREAT|00666);
//Set signal measurement value(3 apples, 0 oranges)
if( -1 == semctl(numOfApple_sem_id, 0, SETVAL, 0) ||
-1 == semctl(numOfEmptyPlate_sem_id, 0, SETVAL, 3) ||
-1 == semctl(numOfOrange_sem_id, 0, SETVAL, 0) ||
-1 == semctl(getAccesstoPlate, 0, SETVAL, 1) )
{
perror("Semctl serval Error!n");
}
//init P,V opration
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;
father_id = fork();
if( father_id < 0 )//Error occures
{
fprintf(stderr, "Fork father Fail.n");
}
else if( 0 == father_id ) //father do
{
while(1)
father_do();
}
else
{
girl_id = fork();
if( girl_id <0 )//Error occures
{
fprintf(stderr, "Fork gril Fail.n");
}
else if( 0 == girl_id ) //girl do
{
while(1)
girl_do();
}
else
{
boy_id = fork();
if( boy_id < 0) //Error Occures
{
fprintf(stderr, "Fork boy Fail.n");
}
else if ( 0 == boy_id) //boy do
{
while(1)
boy_do();
}
else //main process
{
do{
op= getchar();
}while( op!='q');
exit(0);
}
}
}
return 0;
}



