* * Producer/consumer model * given warehouse volume (N) * Generator generated product warehousing * Consumers take out product consumption from warehouse * When the warehouse is full, the producer cannot continue to generate * The customer cannot continue to consume when the warehouse is empty * The access to the warehouse is exclusive * * Sembin Control exclusive access * Semmax Control warehouse Full * semmin Control warehouse Empty * * Producer consumer * p (Semmax) p (semmin) * p (Sembin) p (sembin) * (*product) + + (* Product)--Output Information * V (sembin) v (sembin) * V (semmin) v (semmax) * * * * * #include <stdio.h> #include <stdl ib.h> #include <sys/types.h> #include <unistd.h> #include <fcntl.h> #include <string.h> # Include <sys/mman.h> #include <semaphore.h> #include <assert.h> #include <signal.h> #define PC_ Shm_name "/PCSHM" #define Pc_sem_name_bin "/pcsembin" #define PC_SEM_NAME_MAX "/pcsemmax" #define PC_SEM_NAME_MIN "/p Csemmin "#define SHM_LENGTH 512//Shared memory length #define PRODUCT_MAX 30//Warehouse volume #ifndef FALSE #define FALSE 0 #endif #ifndef T RUE #define TRUE 1 #endif int exitflag = 0;
Exit Flag * * CTRL + C signal/void Ctrlchandle (int signum) {exitflag = 1;} int main (void) {int * prodUct Number of products, pointing to shared memory int shmfd; Shared memory handle sem_t * SEMBIN; Binary semaphore, control of warehouse uniqueness Access sem_t * SEMMAX; Count semaphore, control product upper Limit sem_t * semmin; Count semaphore, control product lower bound _bool Isfirst = FALSE;
First run.
Signal (SIGINT, ctrlchandle); Preparing shared memory Shmfd = Shm_open (pc_shm_name, O_RDWR | O_creat |
O_EXCL, 0666);
if (shmfd >= 0) {//create Isfirst = TRUE for the first time;
Ftruncate (SHMFD, shm_length);
}else {//re-open SHMFD = Shm_open (Pc_shm_name, O_RDWR, 0666);
if (SHMFD < 0) {perror ("Shm_open");
Exit (Exit_failure); }//Memory mapped product = (int *) mmap (NULL, Shm_length, prot_read|
Prot_write, map_shared, SHMFD, 0);
if (Product = = map_failed) {perror ("mmap");
Shm_unlink (Pc_shm_name);
Exit (Exit_failure);
} if (Isfirst) {///first create shared memory, initialize *product = 0;
//Open/Create semaphore if (isfirst) {sembin = Sem_open (Pc_sem_name_bin, O_creat, 0666, 1);
Semmax = Sem_open (Pc_sem_name_max, O_creat, 0666, Product_max); Semmin = Sem_open (Pc_sem_name_min, O_cReat, 0666, 0);
}else {sembin = Sem_open (pc_sem_name_bin, 0);
Semmax = Sem_open (Pc_sem_name_max, 0);
Semmin = Sem_open (pc_sem_name_min, 0);
} if ((Sembin = = sem_failed) | |
(Semmax = = sem_failed) | |
(Semmin = = sem_failed))
{perror ("Sem_open");
Munmap (product, shm_length);
Shm_unlink (Pc_shm_name);
Exit (Exit_failure);
}//main loop while (!exitflag) {#ifdef produce//producer//p (MAX) if (sem_wait (Semmax)!= 0) continue;
P (BIN) if (sem_wait (Sembin)!= 0) {sem_post (Semmax);
Continue
///Production (*product) + +;
printf ("produce:%d\n", *product);
V (BIN) assert (Sem_post (sembin) = = 0);
V (MIN) assert (Sem_post (semmin) = = 0);
#else//Consumer//p (MIN) if (sem_wait (semmin)!= 0) continue;
P (BIN) if (sem_wait (Sembin)!= 0) {sem_post (semmin);
Continue
//Consumption (*product)--;
printf ("consume:%d\n", *product);
V (BIN) assert (Sem_post (sembin) = = 0);
V (MAX) assert (Sem_post (semmax) = = 0); #endif UsleEP (100000);
} munmap (product, shm_length);
Shm_unlink (Pc_shm_name);
Sem_close (Sembin);
Sem_close (semmin);
Sem_close (Semmax);
Sem_unlink (Pc_sem_name_bin);
Sem_unlink (pc_sem_name_min);
Sem_unlink (Pc_sem_name_max);
return 0;
}