[Basic] computer operating system entry

First, the threads and processes

Basic concepts of threads and processes

Process is the basic unit of resource allocation and protection of the operating system, the operating system allocates resources to the process as the basic unit. The thread is an integral part of the process, which represents a sequential flow of execution.

2. The difference between threads and processes
  • Process operating system is the basic unit of resource allocation, and the thread is the basic unit of task scheduling and execution;
  • The operating system can perform multiple processes, the process can perform multiple threads;
  • Process cost, small thread overhead: switching between each process has its own code and data space, the process will have greater overhead; thread can be seen as lightweight processes, threads share the same set of code and data space each thread has its own independent stack and run a program counter, the overhead of switching between threads small;
  • Independent resource allocation process, thread group shared resources: the system will assign a different memory space for each process at run time; threads share system resources, such as virtual address space, file descriptors , and signal processing , etc., have their own call stack , register environment, thread-local storage;
  • The process is the thread of the container: if there are multiple threads within a process, then it is a multi-threaded execution together to complete. A thread is part of the process, so the thread is also known as light weight process or LWP.

Second, inter-thread communication

  • Unknown pipe PIPE: pipe is half-duplex communication mode, data flows only one way, but can only be used in a process having a genetic relationship between. Kinship process usually refers to the process of parent-child relationship;
  • Named Pipes FIFO: named pipe is half-duplex communication, but allows no communication between the kinship process;
  • Message queue MessageQueue: message queue is a linked list message stored in the message queue by the kernel identifier. Signaling message queue overcome the less information, only the carrier pipe plain byte stream and other shortcomings limited buffer size;
  • Shared storage SharedMemory: Shared memory is memory mapped a period that can be accessed by other processes, this shared memory created by a process, but can be accessed by multiple processes. IPC shared memory is the fastest way, it is for the other inter-process communication running low efficiency specifically designed. It is often associated with other communication mechanisms, such as semaphores, used in conjunction, to achieve synchronization and communication between processes;
  • Semaphore Semaphore: Semaphore is a counter, multiple processes can be used to control access to shared resources. It is often used as a locking mechanism to prevent access to shared resources is a process, other processes can also access the resource. Therefore, the main as well as inter-process synchronization means between the different threads within the same process;
  • Socket Socket: Solutions sleeve port is an inter-process communication mechanism, with various other communication mechanism is that it can be used for different processes and communication between;
  • Signal Sinal: signal is a more sophisticated means of communication, an event used to notify the receiving process has occurred.

Third, the thread deadlock

1. Basic Concepts Deadlock

Deadlock refers to two or more processes in the implementation process, due to the competition for resources or A blocking phenomenon caused due communicate with each other, without external force, they will not be able to promote it. At this time, say the system is in deadlock state or system to produce a deadlock, which is always in the process of waiting for another process called the deadlock.

2. deadlock generation condition
  • Mutually exclusive conditions: refers to the process of resource allocation to be exclusive use of a resource that is occupied by only one process at a time. If at this time there are other resources to process the request, the requestor can only wait until the share of resources with the completion of the process of release;
  • Request and keeping conditions: that the process has to keep at least one resource, but proposed a new resource request, the resource has been occupied by other processes, this time requesting process blocked, but for other resources that they have available to keep hold;
  • Not deprivation: Resource refers to the process have been obtained before is not used, can not be denied, can only be released by their own when used up;
  • Loop wait condition: refers to a deadlock occurs, there is a necessary process - endless chain of resources, i.e., the process set {P0, P1, P2, ···, Pn} of P0 is waiting for a resource occupied P1; P1 P2 is waiting for resources occupied, ......, Pn is waiting for a resource that has been occupied by P0.
3. deadlock causes
  • Processes compete for resources caused by deadlock: When the resource sharing system for a plurality of processes, such as, the number of processes is insufficient to meet the various needs of the printer, and the like public queues, such processes cause competition for resources and a deadlock;
  • Illegal order to promote the process: the process during operation, request and order the release of resources properly, will also lead to a deadlock. For example, concurrent processes P1, P2, respectively, to keep the resources R1, R2, and the application process P1 resources R2, process P2 when applying for resource R1, both of which are occupied because the required resources blocked.
4. Prevention Deadlock

To break one of the four necessary conditions for effective in preventing the occurrence of deadlock,

  • Break mutually exclusive conditions: the transformation of exclusive resources to virtual resources, most of the resources has been unable to reform;
  • Not seize break condition: When a process exclusive possession of a resource and then apply for a exclusive resources can not be met, then exit the possession of the original resources;
  • Possession of break and eligibility criteria: the use of pre-allocated resource strategy, namely to apply all the resources before running the process to meet the run, or to wait, so as not to occupy and application;
  • Wait for conditions to break the cycle: to achieve an orderly distribution of resources strategies to achieve the classification number for all devices, all processes can only be used by increasing numbers form of application resources.

Guess you like

Origin www.cnblogs.com/6970-9192/p/11414604.html