Talk C together (93rd back: C language instance -- critical resources for inter-process communication)
Hello, everyone. In the previous chapter, we are talking about the example of using signals and pipelines for inter-process communication. This example is as follows:Critical resources for inter-process communication. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!
We will first introduce what is a critical resource.A critical resource is a scarce resource that can be used by multiple processes, but can only be used by one process within a certain period of time. Therefore, we also call critical resources as shared resources.. For example, a printer is a critical resource. It can be used by multiple processes, but only one process can use the printer in a certain period of time. Otherwise, the printed content becomes messy.
Why do we introduce critical resources? What is the relationship between critical resources and processes?
Do you still remember what we mentioned in the previous chapter? There are three problems to solve regarding inter-process communication: (See 83rd)
1. How to communicate between processes; 2. How to Use critical resources between processes; 3. How to execute a task in an orderly manner between processes;
In the previous chapter, we introduced signals and pipelines and described in detail how to use signals and pipelines to send messages and data between processes. In fact, this is the communication between processes using signals and pipelines. We will introduce the content in the previous chapter to solve this problem.The first question of communication between processes: how to communicate between processes.
Now, this problem has been solved and we need to solve it.The second problem of inter-process communication: How to Use critical resources between processes. This is why I want to introduce critical resources.
For example, we edit the test. c file in vim and write a simple program in the file. The specific content is as follows:
int main(){ printf("hello \n"); return 0;}
If we use gedit to open the test. c file at the same time and write the following content:
int main(){ int a; printf("please input a value for a. \n"); scanf("%d",&a); return 0;}
We regard the test. c file as a critical resource, and use vim and gedit to edit the test. c file at the same time. vim and gedit will start related processes. Therefore, we can see that two processes are using critical resources. So the two processes use the critical resource at the same time. What exactly is the content in the file?
In fact, the vim and gedit processes use special methods to access the critical resource. This ensures that the content in the critical resource is consistent between the two processes. So what methods do they use to access critical resources? In the following chapter, we will introduce two methods to access critical resources:Shared Memory and message queue.
Let's talk about the example of critical resources for inter-process communication. I want to know what examples will be provided later, and I will try again.