Talk C chestnuts together (102nd back: C language instance -- use semaphores to synchronize and mutually exclusive processes)
Hello, everyone. In the previous session, we were talking about the use of semaphores for inter-process synchronization and mutex. In this session, we will continue to talk about this example. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!
In the previous session, we introduced functions related to semaphores and their usage. In this session, we use specific examples to illustrate how to use these functions to operate semaphores.
Below isProcedure:
1. use the semget function to create a semaphore. 2. use the semctl function to initialize the semaphore. 3. use the semop function to perform P operations on semaphores so that processes can enter the critical zone to operate on critical resources. 4. use the semop function to perform the V Operation on the semaphore so that other processes can be awakened when the process leaves the critical section; 5 .. use the semctl function to delete semaphores;
We can still remember the pseudocode we mentioned in the previous chapter. We will convert these pseudocodes into the actual C language code:
Nocritical code // non-critical code P (sem); // execute the P operation to enter the critical section and execute the code {critical code; // do something} V (sem); // execute the V operation to exit the nocritical code in the critical section. // code in the non-critical section
The readers will not write the code in the text, and the detailed code will be put into my resources. You can download and use it.
In addition, let me talk about the content in the Code (you can download the code and compare it): At the beginning of the code, the code defines the Union semun, otherwise there will be a compilation error, as detailed below:
union semun { int val; /* Value for SETVAL */ struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */ unsigned short *array; /* Array for GETALL, SETALL */ struct seminfo *__buf; /* Buffer for IPC_INFO */ };
The Code encapsulates semaphores. For example, the semctl function is encapsulated into a function for initializing and deleting semaphores, And the semop function is encapsulated into a semaphores P/V operation. In addition, we differentiate the use of parameters and the absence of parameters in the code, and perform latency operations on the program when using parameters, this is to help you observe the program running results.
The following is the running result of the program. For details, refer:
(When running the program, use the-s parameter to initialize and delete the semaphore. If no parameter is used, only the P/V operation is performed .)
$. /A-s & // run the compiled program a in the background. The parameter-s indicates that the semaphore is initialized or deleted. [1] 2713 // The program runs in the background, the background process is started here, PID: 2713 $ [pid: 2713] init semaphore [pid: 2713] running nocritical code $. /a // run the program on the foreground without any parameters. The foreground process is started here. PID: 2714 [pid: 2714] running nocritical code [pid: 2713] enter critical area [pid: 2713] running critical code // the code of the background process for executing the critical area [pid: 2713] leave critical area [pid: 2714] enter critical area [pid: 2714] running critical code // code of the front-end process execution critical section [pid: 2714] leave critical area [pid: 2714] running nocritical code $ [pid: 2713] delete semaphore [pid: 2713] running nocritical code // You need to press the Enter key to switch to [1] + Done in the terminal window. /a-s // The background process is running.
You can see from the preceding running results that the background process executes the code in the critical section, and then the foreground process executes the code in the critical section. That is to say, only one process can operate the code in the critical section within a period of time. This is the synchronization and mutex between processes.
For more information, see the examples of using semaphores for inter-process synchronization and mutex. I want to know what examples will be provided later, and I will try again.