Linux semaphores (inter-process communication) will use a program to demonstrate the use of semaphores. The program uses PV to control semaphores, to operate the critical section, and P to reduce semaphores by 1, the V operation adds 1 to the Semaphore, and the code between PV operations is the key code in the critical section. Only one process can be accessed at a time. Program...
Linux semaphores (inter-process communication) will use a program to demonstrate the use of semaphores. The program uses PV to control semaphores, to operate the critical section, and P to reduce semaphores by 1, the V operation adds 1 to the Semaphore, and the code between PV operations is the key code in the critical section. Only one process can be accessed at a time. The program creates a sub-process and has a critical code in each of the two processes. all the functions are output sequentially ~ 9 characters. Ensure that the process is synchronized to www.2cto.com [plain] # include # Include # Include # Include # Include # Include Static int init_semvalue (int); static void del_semvalue (int); static int P (int); static int V (int ); // the custom semun struct union semun {int val; struct semid_ds * buf; unsigned short * array; struct seminfo * _ buf;}; int main (int argc, char * argv []) {int var_cri = 0; int sem_id; pid_t pid; www.2cto.com // Create a semaphore sem_id = semget (key_t) 2234, 1, 0666 | IPC_CREAT ); // initialize a Semaphore if (! Init_semvalue (sem_id) {fprintf (stderr, "Failed to initialize semaphore! \ N "); exit (EXIT_FAILURE);} // create a sub-process if (pid = fork () = 0) // sub-process {while (1) {// critical section if (! P (sem_id) // Apply for resource exit (EXIT_FAILURE); int I; for (I = 0; I! = 10; ++ I) {printf ("[child]: % d \ n", I); sleep (1);} if (! V (sem_id) // release resource exit (EXIT_FAILURE);} www.2cto.com} else if (pid> 0) // parent process {sleep (2); while (1) {// critical section if (! P (sem_id) exit (EXIT_FAILURE); int I; for (I = 0; I! = 10; ++ I) {printf ("[parent]: % d \ n", I); sleep (1) ;}if (! V (sem_id) exit (EXIT_FAILURE);} sleep (8); // destroy the Semaphore del_semvalue (sem_id);} exit (EXIT_SUCCESS);} static int init_semvalue (int id) {union semun sem_union; sem_union.val = 1; if (semctl (id, 0, SETVAL, sem_union) =-1) // set the value return (0) of the signal value in the semaphore set ); return (1);} static void del_semvalue (int id) {www.2cto.com union semun sem_union; if (semctl (id, 0, IPC_RMID, sem_union) =-1) // The IPC--RMID removes the semaphore set from memory fprintf (stderr, "Failed to delete semaphore \ n");} int P (int id) {struct sembuf op; op. sem_num = 0; // single signal op. sem_op =-1; // P operation op. sem_flg = 0; if (semop (id, & op, 1) <0) // change the key, which indicates a resource to be used by the process. return 0; return 1 ;} www.2cto.com int V (int id) {struct sembuf op; op. sem_num = 0; op. sem_op = 1; // V operation op. sem_flg = 0; if (semop (id, & op, 1) <0) return 0; return 1 ;}
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.