Process definition
The program does not run alone, and the program is assigned resources to run only if it is loaded into memory, and this execution is called a process. The difference between a program and a process is that a program is a set of instructions, which is a static descriptive text of the process, and a process is an execution activity of a program, which belongs to the dynamic concept.
In multi-channel programming, we allow multiple programs to be loaded into memory at the same time, which can be implemented concurrently under the dispatch of the operating system. This is the design that greatly improves the CPU utilization. The advent of the process makes it possible for each user to feel the CPU alone, so the process is proposed for multi-channel programming on the CPU.
Inter-process communication
#管道 (pipe): A pipe is a half-duplex mode of communication in which data can only flow in one direction and can only be used between processes that have affinity. A process's affinity usually refers to a parent-child process relationship.
#有名管道 (named pipe): A well-known pipe is also a half-duplex mode of communication, but it allows communication between unrelated processes.
# semaphore (Semophore): Semaphore is a counter that can be used to control access to shared resources by multiple processes. It is often used as a locking mechanism to prevent a process from accessing the shared resource while other processes are accessing the resource. Therefore, it is primarily used as a means of synchronization between processes and between different threads within the same process.
# message Queue: Message Queuing is a linked list of messages, stored in the kernel and identified by message queue identifiers. Message Queuing overcomes the disadvantages of less signal transmission information, only unformatted byte stream, and limited buffer size.
# signal (sinal): A signal is a more sophisticated means of communication that notifies the receiving process that an event has occurred.
# Shared Memory: Shared memory is the mapping of memory that can be accessed by other processes, which is created by a process, but can be accessed by multiple processes. Shared memory is the fastest IPC approach and is specifically designed for low-efficiency operation of other interprocess communication modes. It is often used with other communication mechanisms, such as signal two, to achieve synchronization and communication between processes.
# Socket: Socket is also an inter-process communication mechanism, unlike other communication mechanisms, it can be used for different and inter-process communication.
Enhancement
Semaphore mechanism is a convenient and efficient process synchronization mechanism, but each access to understand the resources of the process must bring their own synchronous operation, not only to the system management trouble, but also easily due to improper synchronization of the system deadlock caused. Such a new process synchronization tool appears-enhancement.
Defined
A resource management program that represents the data structure of a system's shared resources and a set of processes that implement operations on that shared data structure, collectively constituting an operating system's resource management module, which we call a "pipe process."
Enhancement is called by the process of the requested resource, which consists of four parts: the name of the ① tube, a description of the data structure ② local to the tube, ③ a set of procedures for manipulating the data structure, and ④ a statement that sets the initial value for shared data that is local to the tube;
The process is equivalent to the wall, the shared data structure is all circled, leaving only the entrance and exit, and have their own message queue, each time the processes to access the shared resources, must pass through the pipe to enter, and the pipe each time only one process to allow access to the pipe, so as to achieve mutual exclusion.
Features of the enhancement:
- Modular. Enhancement is a basic program unit that can be compiled independently.
- Abstract data type. There is not only data in the tube, but also the operation of the data.
- Information masking. The data in the process must be accessed only by Cheng Lai, which is also defined within the pipe for process calls outside the pipe, and the data structure in the pipe and the concrete implementation outside are invisible.
The difference between a tube and a process:
1, although both define the data structure, but the process of the structure of the PCB is the process of private, pipe data structure is a public data structure, such as Message Queuing;
2, both exist to the respective data structure operation, but the process is by the sequential program execution related operation, but the pipe process is the synchronization operation and the resource initialization operation;
3, the purpose of setting up the process is to achieve the concurrency of the system, and enhancement is to solve the problem of synchronization of resource use;
4, the process by invoking the process in the pipe to manipulate the shared data structure, the process is called like a subroutine, so the process is active, and enhancement is passive.
5, the process can be concurrent, but the pipe is not with its caller concurrency
6, the process is dynamic, and enhancement is a resource management module in the operating system for the process to call.
Thread
The process has many advantages, it provides multi-channel programming, let us feel each of us have their own CPU and other resources, can improve the utilization of the computer. Many people do not understand, since the process is so good, why do threads? In fact, careful observation will find that the process is still a lot of defects, mainly reflected in two points:
A process can only do one thing at a time, and if you want to do two or more things at once, the process is powerless.
If a process is blocked during execution, such as waiting for input, the entire process hangs, even if some work in the process does not depend on the input data, it cannot be executed.
If these two shortcomings are difficult to understand, give a realistic example, perhaps you are clear: if we take the course of the process as a process, then we have to do is ear to listen to the teacher lectures, hand also take notes, the brain still have to think about the problem, so as to effectively complete the task of the lectures. And if only to provide the process of this mechanism, the above three things will not be executed at the same time, can only do one thing, listen to the time can not take notes, and can not use the brain to think, this is one; if the teacher wrote the calculation process on the blackboard, we began to take notes, and the teacher suddenly had a step down, blocked He was thinking over there, and we couldn't do anything else, even if you wanted to think about a problem you didn't understand just now, which is second.
Now you should understand the defects of the process, and the solution is very simple, we can let listen, write, think three independent process, parallel together, so it is obvious to improve the efficiency of lectures. The same mechanism, the thread, is also introduced in the actual operating system.
Advantages of threading
Because of concurrency, we invented the process and further invented the thread. Only the concurrency level of the process and thread is different: The process is an abstraction provided at the processor level, and the thread is a layer of concurrent abstraction at the level of the process. If we enter the computer architecture, we will find that the pipeline provides a kind of concurrency, but instruction-level concurrency. In this way, pipelines, threads, and processes from low to high on three levels to provide the concurrency we desperately need!
In addition to increasing the concurrency of the process, the thread has the advantage of being able to effectively utilize multiprocessor and multicore computers. There is a tendency for processors to evolve toward multicore, and multicore does not allow a process to execute faster without threads, due to all the two-point limits above. However, if a process is broken down into several threads, it is possible to have different threads running on different cores, thus increasing the execution speed of the process.
For example: We often use Microsoft Word for text typesetting, and actually open multiple threads. One of these threads is responsible for displaying, one accepting keyboard input, one for saving, and so on. These threads run together, allowing us to feel that our input and screen display occur simultaneously, rather than entering some characters, which can be seen over a period of time. When we inadvertently, we also carried out automatic disk operation. This is the convenience that the thread brings to us.
The difference between a process and a thread
A process is a program with a certain independent function about a single run activity on a data set, a process that is an independent unit of the system's resource allocation and scheduling.
A thread is an entity of a process that is the basic unit of CPU dispatch and dispatch, which is a smaller unit that can run independently than a process. The thread itself basically does not own the system resources, only has a point in the operation of the necessary resources (such as program counters, a set of registers and stacks), However, it can share all of the resources owned by the process with other threads that belong to one process.
A program has at least one process, and a process has at least one thread. One thread can create and revoke another thread, which can be executed concurrently between multiple threads in the same process, and the thread shares the process resources in addition to the stack. Similar to the process's control table PCB, the thread also has its own control table TCB, but the TCB holds a much smaller state of the thread than the PCB table.
The main difference between processes and threads is that they are different ways to manage operating system resources. The process has a separate address space, and after a process crashes, it does not affect other processes in protected mode, and the thread is just a different execution path in a process. Thread has its own stack and local variables, but there is no separate address space between the threads, a thread dead is equal to the entire process dead, so the multi-process program is more robust than multithreaded programs, but in the process of switching, the cost of large resources, efficiency is worse. But for some concurrent operations that require simultaneous and shared variables, only threads can be used, and processes cannot be used.
Cause of deadlock
- Resource competition
- Inter-process propulsion
The necessary condition for generating a deadlock
- Mutually exclusive
- Request and hold
- Do not deprive
- Loop wait
Prevent deadlocks
In the necessary condition of generating deadlock, plus discard (except for mutually exclusive conditions, because the rejection of mutual exclusion, there is no guarantee of concurrency correctness)
- Discard requests and keep
The system stipulates that all processes must have all the resources required for a one-time application before they begin execution.
- Abandon Loop wait
The order in which all processes request resources must be presented in the order in which the resource numbers are incremented.
- Abandon the non-deprivation
When a process already has resources and is not immediately satisfied when it requests new resources, it must immediately release the resources it has in its possession, and if such resources are needed in the future, it can be re-applied.
Avoid deadlocks
The banker algorithm is used to avoid deadlocks.
Deadlock detection
Save request and assignment information about a resource
Provides an algorithm to use this information to detect if a system has entered a deadlock state
Unlock deadlock
Deprivation of resources
Undo Process
Process Line Cheng