Using semaphore to realize synchronization problem of driver's conductor process in Linux environment

Source: Internet
Author: User
Tags erro semaphore

Problem Description:

On the bus, the driver and the conductor's activities are:
Driver's activity: Start the vehicle, normal driving, stop.
The conductor's activities: Closing the door, selling tickets, driving the door.

In the continuous arrival, stop and drive of the automobile, the signal volume and p,v operation are used to realize their synchronization.

Problem solving;

We can use two signals to achieve the synchronization between the driver and the conductor, the specific implementation of the following form:

Driver Process:

Drivers Drive;

V (S2);

P (S1);

Car departure station;

Conductor Process:

Conductor ticketing;

P (S2);

The conductor drives the door;

Passengers to and from the car;

The conductor closed the door;

V (S1);

C Code implementation:


#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/ipc.h> #include < sys/sem.h>int semid1,semid2; //defines two semaphores void v1 (INT&NBSP;SEMID1); Void v2 (INT&NBSP;SEMID2) ; Void p1 (INT&NBSP;SEMID1); Void p2 (INT&NBSP;SEMID2); Void conductor (); Void driver ();// 2.1 Definition Semun Common body union semun {                int              val;                struct semid_ds  *buf;               unsigned  short  *array;                struct seminfo  *__buf;            };int main () {        key_t key1,key2; defines the key value of the semaphore          //int semid1,semid2;        //2.2 define semaphore initial value          union semun v1,v2;         int r1,r2;        //struct sembuf op1[1],op [2];&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//1. Creating semaphores          Key1=ftok ("."),         key2=ftok (".", 98);         if (key1==-1)         {                 printf ("key1 get erro:%m\n") ;                 exit (-1);          }        if (Key2==-1)          {                 printf ("key2 get erro:%m\n");                 exit ( -1);        }         //get the Semaphore         semid1=semget (key1,1,ipc_creat| ipc_excl|0666);         semid2=semget (key2,1,ipc_creat| ipc_excl|0666);         //semid1=semget (key1,1,0);         //semid2=semget (key2,1,0);                 if (semid1==-1)                   {                          printf ("semid1 get erro:%m\n ");                          exit ( -1);                  }                 if (semid2==-1)                   {                          printf ("semid2  Get erro:%m\n ");                    &Nbsp;     exit ( -1);                  }        //printf ("semid1:%d\n", SEMID1);         //printf ("semid2:%d\n", Semid2);         //2. Initialize the semaphore to 0        v1.val=0;         v2.val=0;                 r1=semctl (SEMID1,0,SETVAL,V1);                 r2=semctl (SEMID2,0,SETVAL,V2);                 if (r1==-1)                   {                          printf ("R1 get  erro:%m\n ");                          exit ( -1);                  }                 if (r2==-1)                   {                         printf ("R2  get erro:%m\n ");                          exit ( -1);           &nBsp;      }        if (fork () >0)              //Parent Process          {                 Driver ();         }        else                      //Sub-process         {                 conductor ();        &NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//4. Deleting semaphores          semctl (Semid1,0,ipc_rmid);         semctl (Semid2,0,IPC_RMID); return 0;} VOID&NBSP;P1 (INT&NBSP;SEMID1) {        struct sembuf op1[1];         op1[0].sem_num=0;        op1[0]. sem_op=-1;//p-1 Operation         op1[0].sem_flg=0;         semop (semid1,op1,1);} VOID&NBSP;P2 (INT&NBSP;SEMID2) {        struct sembuf op2[1];         op2[0].sem_num=0;         op2[0].sem_op=-1;//p-1 Operation         op2[0].sem_flg=0;          semop (semid2,op2,1);} VOID&NBSP;V1 (INT&NBSP;SEMID1) {        struct sembuf op1[1];         op1[0].sem_num=0;         op1[0].sem_op=1;//v to+1 Operation         op1[0].sem_flg=0;         semop (semid1,op1,1);} Void v2 (INT&NBSP;SEMID2) {        struct sembuf op2[1];         op2[0].sem_num=0;         op2[0].sem_op=1;//v for +1 Operation         op2[0].sem_flg=0;         semop (semid2,op2,1);} Void driver ()//driver operation function {        while (1)          {        printf ("Driver drive \ n");         v2 (SEMID2);         p1 (SEMID1);         printf ("Driver off station \ n");         sleep (1);        }}void conductor ()//conductor operation function {        while (1)         {        printf ("conductor ticket \ n"); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;P2 (SEMID2);         printf ("Conductor drive door \ n");         printf ("Passenger \ \");         printf ("Conductor closed door \ n");         V1 (SEMID1);         sleep (1);         }}

Using semaphore to realize synchronization problem of driver's conductor process in Linux environment

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.