Linux Process Collaboration-a small example

Source: Internet
Author: User

Dad fed fruit to his daughter and son. Dad selects oranges or apples randomly, peel oranges or peel apples on a plate. The peeling speed is faster and the peeling time is slower. My daughter only eats oranges, and my son only eats apples (of course we assume that my daughter and son will never eat enough ). A plate can contain only three fruits. My son eats fast and her daughter eats slowly.

Program to simulate the process:

Simple Analysis

Semaphores:

Int accessplate = 1; // indicates the semaphore for accessing the plate

Int Apple = 0; // Number of apples

Int Orange = 0; // orange

Int emptyplates = 3; // empty plate

Father:

P (emptyplates );

Produce a fruit

If (is apple)

Then

P (accessplate)

Put apple in

V (Apple)

V (accessplate)

Else

P (accessplate)

Put orange in

V (orange)

V (accessplate)

BOY:

P (Apple)

P (accessplate)

Get an apple

Eat an apple

V (emptyplates)

V (accessplate)

 

Girl:

P (orange)

P (accessplate)

Get an orange

Eat an orange

V (emptyplates)

V (accessplate)

 

Code:

 

# 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 ;}

 

Run:

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.