Operating System Basics
What is an operating system?
- A program that manages computer hardware and software resources.
- It is the kernel and cornerstone of computer system.
- Provide a user interface to interact with the system.
- Contains a kernel and a shell.
User mode and kernel Mode
According to the characteristics of process access to resources, we can divide the process running on the system into two types: user state and system state.
To run the program | Memory access range | instruction | |
---|---|---|---|
User mode | The application | Restricted to access only user storage space | Privileged instructions |
The system state | System program | Unrestricted access to any of the computer’s resources, including user and system storage space, hardware and software in the system | Nonprivileged instruction |
So how do these two states relate?
First, most running programs run in user mode. Only a fraction of it will go into the system state. The privilege instructions mentioned in the above table put the program into system state. In JavaGuide terms, all operations related to system-state level resources (such as file management, process control, memory management, etc.) must be performed by the operating system through system calls to the operating system.
These system calls can be roughly divided into the following categories by function:
- Equipment management. Request or release the device, and start the device.
- File management. Read, write, create, and delete files.
- Process control. Complete the process creation, cancellation, blocking, and wake up functions.
- Process communication. Completes functions such as messaging or signaling between processes.
- Memory management. The system allocates and reclaims memory, and obtains the size and address of the memory occupied by a job.
Processes and threads
Process and thread
First, threads are smaller units of execution into which processes are divided, and a process can produce multiple threads during its execution.
The main difference between threads and processes is that processes are independent of each other, not necessarily threads.
The heap of a process shared by multiple threads. But it has its own stack.
Process status
-
Create State (New) : Being created
-
Ready: The system is ready to run. The process now has all the resources it needs except the processor and can run once it gets the processor resources.
-
Running: The process is running on the processor.
-
Waiting: A state in which a process is suspended while waiting for some event, such as an I/O operation, to complete. The process cannot run even if the processor is idle
-
Terminated process is disappearing from the system. It may end normally or be interrupted for other reasons
Communication between processes
I quoted shuai Di’s article on CSDN for this part, he’s really good. The article is well written, progressive and well-founded. It is not simply enumerating the content and characteristics of the object. If you can understand the knowledge based on his ideas, I believe you will have a deeper understanding and a deeper impression on the knowledge. Original link: blog.csdn.net/m0_37907797…
1. Pipes
We used to | using Linux. This is the pipe. The output of the former will be the input of the latter.
In this way, it is clear that the communication is one-way.
Also, the pipe is anonymous and we don’t have a name for it. Named pipes also exist. Here is an example of code that creates a copper sleeve named test:
mkfifo test
Copy the code
Next, an example using the pipe will illustrate the pipe in more detail.
Echo "this is a pipe" > test cat < testCopy the code
Note that the command will remain in the pipe until the data is read out, and the write command will end only when the data is read out.
From this example, you can see that the communication mechanism of a pipe is similar to a cache. It’s like a process putting data in a cache and waiting for another process to read it.
What are his strengths and weaknesses?
- advantages
- It’s so simple, we don’t have to create objects anymore.
- Ensures that data in the pipeline is taken by another process
- disadvantages
- Inefficient, the original process can return only after another process has read the data
What makes it possible to put data into memory and immediately return it without waiting for another process to read it? Message queues can answer this question.
2. Message queues
If process A wants to send A message to process B, it only needs to put the message in the corresponding message queue. Process B reads the message from the corresponding message queue when necessary. This approach is also similar to caching.
Note that a message queue is a linked list of messages, in a specific format, stored in memory. The implementation of linked lists is known to be first-in, first-out, but the correspondence mentioned in the previous paragraph is identified by message queue identifiers, which implement random query of messages.
So what are the disadvantages? Message queues are not suitable if there is a large amount of memory for sending data and communication between two processes is very frequent. The large size of the data means that the process of sending a message takes a long time to read memory.
3. Shared memory
The disadvantage of message queues can be well addressed by shared memory communication. The time spent copying messages is not needed.
But processes are independent of each other, so how can they share memory?
First, the memory the system allocates to the process is not actual physical memory, but virtual memory space. So we can have both processes take a virtual address space and map it to the same physical memory.
This approach relies on some kind of synchronous operation, such as mutex and semaphore. This is arguably the most useful form of interprocess communication.
4. Semaphore
A semaphore is essentially a counter used to achieve mutual exclusion and synchronization between processes.
For example, the initial value is 1, and then when A calls, we set the semaphore to 0. So when B comes to access it, it can’t access it if it’s 0.
5. Socket
Shared memory, pipes, semaphores and message queues are all communication between multiple processes on the same host. What do processes on different hosts use to communicate?
The answer is socket. Socket is the basic operation unit that supports TCP/IP network communication. To put it simply, socket is a convention of communication between two parties. It uses related functions in the socket to complete the communication process.
How threads are synchronized
First, thread synchronization is when two or more threads that share a critical resource execute concurrently, and the threads should be synchronized to avoid critical resource usage conflicts. Operating systems generally have the following three ways of thread synchronization:
-
Mutex: Using the mutex mechanism, only the thread that owns the mutex has access to a common resource. Because there is only one mutex, it is guaranteed that common resources will not be accessed by multiple threads at the same time.
-
Semaphore: It allows multiple threads to access a unified resource at the same time, but controls the maximum number of threads that can access the resource at the same time
-
Events: Wait/notify to synchronize multiple threads and compare priorities of multiple threads
Process scheduling algorithm
Scheduling refers to the order in which processes run. Good scheduling allows maximum CPU utilization. Computer scientists have defined some algorithms:
-
First come, first served (FCFS) scheduling algorithm: Select the first process to enter the ready queue and allocate resources to it. Make it execute immediately and continue to execute until it completes or is blocked by some event and gives up CPU usage.
-
Short Job First (SJF) scheduling algorithm: select a process with the shortest estimated running time from the ready queue and allocate resources to it… As above…
-
Round Robin scheduling algorithm: Each process is assigned a time segment, called its time slice. That is, how long the process is allowed to run.
-
Multi – stage feedback queue scheduling algorithm: it is recognized as a good process scheduling algorithm. Prioritize processes and allocate more time to processes with higher priorities.
-
Priority scheduling algorithm: Assigns a priority to each process, executes the highest priority process first, and so on. When tie is present, FCFS is used. Prioritize based on memory requirements, time requirements, or any other resource requirements.
To be continued…