Talk together (80th: C language instance-process mutex)
Hello, everyone. In the previous review, we talked about examples of parent and child processes. The example is as follows:Process mutex. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!
We introduced the examples of parent and child processes in the last time. This is a mutual exclusion between processes.Mutual exclusion refers to the use of critical resources by two processes in a certain order.For example, a keyboard is a critical resource. If the parent process is in use, the child process needs to wait until it is used up. Next, we will explain it through an actual example:
int main(){ pid_t pid; pid_t pid_res; int count = 0; int stat_value; pid = fork(); if(pid > 0) { printf("PID: %d -> Father Process is running \n",getpid()); count++; printf("count is %d in Father Process \n",count); } else if(pid == 0) { printf("PID: %d -> Son Process is running \n",getpid()); count++; printf("count is %d in Son Process \n",count); } else { printf("Create process failed \n"); return 1; } return 0;}
After compiling and running the above code, we can get the following results:
. /S // run the compiled program PID: 3030-> Father Process is running count is 1 in Father Process // The parent Process uses the critical resource PID such as count: 3031-> Son Process is running count is 1 in Son Process // The Subprocess uses the count critical resource.
In the above Code, we use count to simulate critical resources. From the running results of the program, we can see that the parent process and the child process have no connection to the use of count, they almost use this critical resource at the same time, and there is no certain order. (Of course, count is not really a critical resource in the program. We are just imitating it ).
If you want processes to use critical resources in a certain order, you can use the wait function. It is usually used in the parent process. After the wait function is used in the parent process, it waits until the child process is running, it starts to run the content in the parent process. The following is the code after we use wait:
Int main () {pid_t pid; pid_t pid_res; int count = 0; int stat_value; pid = fork (); if (pid> 0) {printf ("PID: % d-> Father Process is running \ n ", getpid (); pid_res = wait (& stat_value); // use the wait function to implement Process mutex if (pid_res> 0) // wait until the child process ends and then use the critical resource {printf ("Son process finished: PID = % d \ n", pid_res); count ++; printf ("count is % d in Father Process \ n", count) ;}} else if (pid = 0) {printf ("PID: % d-> Son Process is running \ n ", getpid (); count ++; printf (" count is % d in Son Process \ n ", count );} else {printf ("Create process failed \ n"); return 1;} return 0 ;}
The following is the running result of the program. Please compare it with the running result process that does not use the wait function.
. /S // run the compiled program PID: 3073-> Father Process is running // The parent Process does not use the count critical resource PID: 3074-> Son Process is running count is 1 in Son Process // The Subprocess uses the count critical resource Son process finished: PID = 3074 // sub-Process end count is 1 in Father Process // The parent Process is using the count critical resource
The readers will not write the code in the body, and the detailed code will be put into my resources. You can click here to download and use it.
Let's talk about examples of process mutex. I want to know what examples will be provided later, and I will try again.