Linux learning process, threads and programs

  

1: program and process differences

The emergence of the process is originally under UNIX, used to represent multi-user, multi-tasking operating system environment, the application The concept of a basic execution unit in a memory environment. A process is the most basic concept of a UNIX operating system environment and is the smallest unit of system resource allocation. The user management and resource allocation under the UNIX operating system are almost all realized by the operating system through the control of the application process!

When a source program written in a language such as c c++ java is compiled into an executable file by the corresponding compiler, it is submitted to the computer processor for running, and the running state is called a process. The process appears to the user as the execution process of the application. From the perspective of the operating system, the process is the memory allocated by the operating system, the basic unit of resources such as cpu time slices, and the running environment provided for the running program. The difference between a process and an application is that the application is stored as a static file in a storage space such as a computer's hard disk, and the process is a system resource management entity that is maintained by the operating system under dynamic conditions.

The biggest difference between the concept of a process and the concept of a program is:

1: The process is dynamic, and the program is static

2: The process has a certain life Period, and the program is a collection of instructions, which itself does not have the meaning of "sports". Programs that are not built into the process cannot be recognized as an independent operating system.

3: A process can only correspond to one program, but a program can correspond to multiple processes.

2: Processes in the Linux environment

Linux process operations mainly generate processes, terminate processes, and there are data and control interactions between processes, that is, interprocess communication and synchronization.

Process generation process:


There are many ways to generate a process, but the basic process is the same.

(1) First copy the environment configuration of its parent process.

(2) Establish a process structure in the kernel.

(3) Insert the structure into the process list for easy maintenance.

(4) Allocate resources to this process.

(5) Copy the memory mapping information of the parent process.

(6) Manage file descriptors and link points.

(7) Notify the parent process.

How the process terminates:

There are 5 ways to terminate the process:

1: Return from the main function.

2: Call the exit function.

3: Call the _exit function.

4: Call abort.

5: Terminated by a signal.

When the process terminates, the system will release all resources of the process, such as memory, file characters, kernel structure, and so on.

Note: The difference between exit and _exit is that the exit function checks the opening of the file before the system calls exit, and writes the contents of the file buffer back to the file.

Inter-process communication:

There are several ways to communicate between processes, with pipes, shared memory, and message queues being the most common methods.

1: Pipeline is the oldest way of process communication in the UNIX family. It uses the kernel to establish a channel between two processes. It is similar to the operation of a file, only read-only on the end of the pipe. The other end only writes. Use read and write to pass data between processes.

2: Shared memory is a piece of memory that is shared between multiple processes. Multiple processes use the address of the shared memory obtained to operate directly on the memory.

3: The message is to create a linked list in the kernel. The sender sends the data to the kernel according to a certain identifier. After the kernel puts it into the scale, it waits for the request from the receiver. After the receiver sends the request, the kernel removes the message from the kernel and sends it to the receiver according to the identifier of the message. A message is a complete asynchronous operation.

Synchronization between processes:

The need to write completion tasks between multiple processes is that there is often a dependency between services, which leads to synchronization problems. The process synchronization mode under linux mainly includes messages and semaphores.

A semaphore is a shared representation of a value that is used for the protection of operations between multiple processes or shared resources. It is the primary means of process synchronization.

3: Processes and Threads

Threads and processes are another pair of meaningful concepts, with the following differences and connections:

1: Entering the car is the operating system The basic unit of resource allocation, the process has a complete virtual space. When system resource allocation is performed, in addition to CPU resources, independent resources are not allocated to threads, and resources required by threads need to be shared.

2: A thread is part of a process. If there is no thread allocation for display, the process can be considered to be single-threaded; if a thread is established in the process, the system can be considered multi-threaded.

3: Multi-threading and multi-process are different, although both are parallel completion functions, but resources such as memory and variables between multiple threads can be shared by multiple methods in a simple way. Multi-process is different, and the sharing method between processes is limited.

4: The process has a process control table PCB (this is not a PCB board), the system schedules the process through the PCB; the thread has a thread control table TCB. But the state represented by the TCB is much less than the PCB.

In summary, the above relationship can be seen that the thread is part of the process, and the process is part of the program.

I have seen the most straightforward process. Thread and program comparison, you can refer to this link to deepen understanding http://blog.jobbole.com/38696/#jtss-tsina

Copyright © Windows knowledge All Rights Reserved