[Operating system] processes and threads

Source: Internet
Author: User

First, the process

The introduction of the process in the implementation of multiple programs, the need to share system resources, resulting in the process of implementation of the mutual restrictive relationship, the execution of the program is characterized by intermittent.
These characteristics occur during the execution of the program, is a dynamic process, and the traditional program itself is a set of instructions, is a static concept, unable to describe the implementation of the program in-memory, that is, we can not see from the program's literal when it executes, when it pauses, and can not see its relationship with other execution programs, Therefore, the static concept of the program can not accurately reflect the characteristics of the program concurrent execution process. In order to describe the nature of program dynamic execution process, people introduce the concept of "process".

A process is a run-time activity of a program with independent functionality about a data collection. It can apply and own system resources, is a dynamic concept, is an active entity. It is not just the code of the program, it also includes the current activity, which is represented by the value of the program counter and the contents of the processing register.
Process is the most basic and important concept in the operating system. is a multi-task program system, in order to describe the internal dynamics of the system, describing the system of internal procedures of the movement of the rules of the introduction of a concept, all the multi-channel program design operating systems are based on the process.

The composition of the process
    • Process ID, process group ID, user ID, group ID
    • Environment
    • Working directory
    • Program Instructions
    • Register
    • Stacks (Stack)
    • Heaps (heap)
    • File descriptor
    • Signal operation
    • Shared libraries
    • interprocess communication tools: Message Queuing, pipelines, semaphores, shared memory

Dynamic: The essence of the process is the process of execution of a program, the process is dynamic generation, the dynamic extinction.
Concurrency: Any process can execute concurrently with other processes
Independence: The process is a basic unit that can operate independently, and it is also an independent unit for allocating resources and dispatching.
Asynchrony: Because of the inter-process constraints, the process has a discontinuity of execution, that is, the process at its own independent, unpredictable speed ahead
Structure characteristic: The process consists of three parts, the program, the data and the process Control block.

Processes and Procedures
    • A program is an ordered set of instructions that itself does not have any meaning to run, and is a static concept. The process is a process of execution of the program on the processing machine, it is a dynamic concept.
    • The program can be used as a software data for a long time, and the process has a certain life period. The program is permanent and the process is temporary.
    • A process can describe concurrency more realistically than a program can, and a process consists of two parts: program and data.
    • The process has the ability to create other processes, and the program does not.
    • In a traditional operating system, the program is not able to run independently, as a resource allocation and independent operation of the basic unit is a process.
The relationship between the basic state of a process and its state

Status: Running, blocking, pending blocking, ready, pending
Transitions between states:

    • The ready process is executed by the CPU and becomes the running state;
    • A running process, making an I/O request or not getting the requested resource, into a blocking state;
    • Running process, process execution (or time slice has arrived), become ready state;
    • The blocked process hangs, becomes a suspended blocking state, when the I/O operation that causes the process to block is completed before the user restarts the process (called Wake-up), the suspend blocking state becomes the suspend-ready state, and when the user restarts the process before the I/O operation is completed, the suspend blocking state becomes the blocking state;
    • Suspends a process in readiness (or running) into a pending ready state, and when the process resumes, the pending ready state becomes the ready state;

Inter-process Communication mode
    1. Pipe and well-known pipe (named pipe): Pipelines can be used for communication between parent-child processes that have affinity, and in addition to having the functionality of a pipeline, a well-known pipeline allows for inter-process communication without affinity.
    2. Signal (signal): The signal is a simulation of the interrupt mechanism at the software level, it is a more complex mode of communication, used to notify the process that an event occurred, a process received a signal and the processor received an interrupt request effect can be said to be consistent.
    3. Message Queuing: Message Queuing is a linked table of messages that overcomes the drawbacks of limited semaphores in the last two modes of communication, with write permission to add new information to the message queue according to certain rules; a process that has Read permission on a message queue can read information from the message queue.
    4. Shared memory: This can be said to be the most useful way to communicate between processes. It allows multiple processes to access the same piece of memory space, and different processes can see in time that the data in the shared memory is updated in the other process. This approach relies on some kind of synchronous operation, such as mutual exclusion and semaphore.
    5. Semaphore (semaphore): primarily as a synchronous and mutually exclusive means between processes and between different threads of the same process.
    6. Socket (SOCKET): This is a more general inter-process communication mechanism, it can be used for inter-process communication between different machines in the network, the application is very extensive.
Second, the thread

A thread is a sequence of code executed in the context of a process, an entity in a process, a unit of independent Dispatch and dispatch by the system, and also known as a lightweight process (light weight processes). In a system that supports multithreading, a process becomes an entity for resource allocation and protection, and a thread is a basic unit that is scheduled to execute.
A thread has only a few resources that are necessary to run, but it can share all of the resources owned by the process with other threads of the same process.
One thread can create and revoke another thread, which can be executed concurrently between multiple threads in the same process. Because of the mutual constraints between threads, the thread is running in a discontinuous.
A thread is a single sequential control flow in a program. Running multiple threads at the same time in a single program accomplishes different tasks, called multithreading.

Threads within a process
    • A thread modifies the shared system resources, and other threads within the process can see this change.
    • For the same data, there may be two pointers with the same value pointing to the data.
    • Multiple threads within a process can read and write to the same memory unit, so an explicit synchronization mechanism must be taken.
    • In the same process's address space, the communication between threads consumes less.

    • The program exists as a process in the operating system, acquires resources, and runs.
    • Within a process, threads can create other threads. Each thread has its own stack.
    • All threads in a process share code snippets and data segments.
The life cycle of a thread
    • Identity of the thread: Usually an integer is used to identify a thread
    • Thread creation: automatically creates the main thread starting from the main function and invokes the function library interface to create a new threading (Pthread_create)
    • Thread termination: Execution is complete, or pthread_exit is called, the main thread exits causing the entire process to terminate
The state of the thread
    • Ready: The thread waits for the available processor.
    • Run (running): thread is being executed.
    • Blocking (blocked): A thread is waiting for an event to occur (such as the completion of an I/O, attempting to lock a locked mutex).
    • Terminate (terminated): The thread returns from the start function or calls Pthread_exit.
Advantages of multithreading
    • Create a line turndown the cost of creating a process is small
    • Thread switching is less expensive than switching between processes
    • Take advantage of multi-processor
    • Data sharing
    • Data sharing makes communication between threads more efficient than interprocess communication
    • Fast response characteristics
    • In the event of a busy system, the process responds to the user's input in a timely manner through a separate thread
Synchronization and mutual exclusion of threads

When there are multiple threads, it is often necessary to synchronize these threads to access the same data or resources. For example, suppose you have a program where one thread is used to read a file to memory, while another thread is used to count the number of characters in the file. Of course, it is meaningless to count the entire file before it is transferred into memory. However, because each operation has its own thread, the operating system executes each of the two threads as an unrelated task, which may count the number of words when the entire file is not loaded into memory. To work around this problem, you must synchronize two threads.
The so-called synchronization, refers to the walk in a number of different processes between the pieces of the program, their operation must be in strict accordance with the specified sequence of operation, this order depends on the specific task to be completed. If defined with access to resources, synchronization refers to the mutual exclusion (in most cases), through other mechanisms to achieve the visitor's orderly access to resources. In most cases, synchronization has been mutually exclusive, especially if all writes to the resource must be mutually exclusive. In rare cases, multiple visitors can be allowed to access resources at the same time.
The so-called mutex refers to a number of program fragments scattered between different processes, when a process runs one of the program fragments, other processes will not be able to run any of their program fragments, can only wait until the process runs out of this program fragment to run. If defined with access to a resource, it is unique and exclusive for a resource to allow only one visitor to access it at the same time. However, mutual exclusion cannot limit the order in which visitors access resources, that is, access is unordered.

Because threads share memory space for the same process, multiple threads may need to access the same data at the same time. Without proper protection, access to shared data can result in inconsistencies and errors in data.
Common synchronization mechanisms:

    • Critical area (critical section)
    • Signal Volume (Simphore)
    • Mutex (mutex)
    • Enhancement (monitor)

signal Volume (Simphore)
The semaphore is defined as an integer variable, with two atoms operating wait and signal to manipulate the semaphore.
Wait: When you do wait, if the semaphore value is greater than the 0,wait operation, the value of the semaphore is reduced by 1 and returned, and wait waits if the current semaphore has a non-positive value.
The signal:signal operation adds 1 to the semaphore value and wakes up which waiting processes

Mutex (mutex)
Only one thread can lock the mutex at the same time, and the thread mutually exclusive access to the shared variable counter with the mutex:

pthread_mutex_t  count_mutex = PTHREAD_MUTEX_INITIALIZER;pthread_mutex_lock(  &count_mutex ) ;    counter++ ;pthread_mutex_unlock( &count_mutex );
Three, the difference between process and thread and contact
    • Child processes and parent processes have different code and data spaces, while multiple threads share data spaces, and each thread has its own execution stack and program counter for its execution context.
    • Multithreading is mainly to save CPU time, play use, depending on the situation. A thread needs to use the computer's memory resources and CPU to run

relationship of process and thread:
(1) A thread can belong to only one process, while a process may have multiple threads, but at least one thread.
(2) A resource is allocated to a process, and all the threads of the same process share all the resources of that process.
(3) The processor is assigned to a thread, that is, a thread that is actually running on the processing machine.
(4) Threads need to be synchronized during execution. Synchronization is achieved between threads of different processes using the means of message communication. A thread is an execution unit within a process and a scheduler within a process.

the difference between a process and a thread:
(1) Dispatch: The thread acts as the basic unit of dispatch and allocation, and the process as the basic unit of resources
(2) Concurrency: Not only can concurrent execution between processes, but also concurrent execution between multiple threads of the same process
(3) Owning a resource: a process is an independent unit that owns resources, and threads do not own system resources, but they can access resources that belong to the process.
(4) Overhead: When you create or revoke a process, the overhead of the system is significantly greater than the cost of creating or revoking a thread, because the system allocates and reclaims resources for it.

[Operating system] processes and threads

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.