linux進程協作-一個小例子

來源:互聯網
上載者:User

       爸爸給女兒和兒子喂水果。爸爸隨機挑選橘子或者蘋果,將橘子剝皮或者將蘋果削皮放在盤子中,剝皮的速度比較快,而削皮的時間比較慢。女兒只吃橘子,兒子只吃蘋果(當然我們假設女兒和兒子永遠也吃不飽)。盤子只能裝下3個水果。兒子吃得比較快,女兒吃得比較慢。

編程類比該過程:

簡單分析

訊號量:

int accessplate = 1;     //表示訪問盤子的訊號量

int apple = 0;                    //蘋果個數

int orange = 0;                  //橘子

int emptyplates = 3;          //空盤子

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)

 

代碼:

 

#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 orange,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(plate)        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(){    /*Process 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_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;}

 

運行:

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.