# Include <sys/Mman. h> # include <sys/types. h> # include <fcntl. h> # include <unistd. h> # include <stdio. h> # include <stdlib. h> # include <errno. h> # include <time. h> # include <sys/IPC. h> # include <sys/SEM. h>/* overall Varible */INT numofemptyplate_sem_id = 0; // number of empty plates.int numofapple_sem_id = 0; // Number of appleint numoforange_sem_id = 0; // Number of orangeint getaccesstoplate = 0; // get plateint * Plate = NULL; // 3 platesstruct sembuf P, V; // P, V opration void printplates (const char * Arg) {int I = 0; printf ("% s \ nfruits in plates:", ARG); for (I = 0; I <3; I ++) {printf ("% d ", plate [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 ora Nge, sleep 1 seconds semop (getaccesstoplate, & P, 1); // P (plate) printf ("father get access to the plates. \ n "); printplates (" FATHER: "); for (I = 0; I <3; I ++) {If (0 = plate [I]) // find an empty plate, and put the furit in {printf ("% d", plate [I]); 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 (P Late) printf ("father put the plates back. \ n ");} else // get an apple {sleep (2); // get an apple, sleep 2 seconds semop (getaccesstoplate, & P, 1 ); // P (plate) printf ("father get access to the plates. \ n "); printplates (" FATHER: "); for (I = 0; I <3; I ++) {If (0 = plate [I]) // find an empty plate, and put the furit in {printf ("% d", plate [I]); 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) printf (" father put the plates back. \ n ") ;}} 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) printf ("gilr get access to the plates. \ n "); printplates (" Girl: "); for (I = 0; I <3; I + +) {If (2 = plate [I]) {printf ("% d", plate [I]); plate [I] = 0; // eat an orange break;} semop (numofemptyplate_sem_id, & V, 1); // v (empty plates) printf ("Girl eat an orange. \ n "); semop (getaccesstoplate, & V, 1); // v (plate) printf (" girl put the plates back. \ n ");} 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) printf ("boy get access to the plates. \ n "); printplates (" Boy: "); for (I = 0; I <3; I ++) {if (1 = plate [I]) {printf ("% d", plate [I]); plate [I] = 0; // eat an apple break ;}} semop (numofemptyplate_sem_id, & V, 1 ); // v (empty plates) printf ("boy eat an apple. \ n "); semop (getaccesstoplate, & V, 1); // v (plate) printf (" boy put the plates back. \ n ");} int main () {/* P Rocess ID */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_c Reat | 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 )) {perro R ("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 ;} |