Linux Process 1

Source: Internet
Author: User

Basic knowledge and implementation of Linux Processes

I learned about Linux Process programming in the last week, and I will summarize it as follows.

In the first part, we first introduce the basic concepts of processes and how to implement real processes in Linux.

Tiger-John note:

Many people focus only on programming, but forget to pay attention to the principle rather than the basic principle. In fact, the operating system works in the same way as Jin Yong's martial arts novels. All specific implementations, such as the Linux operating system and the uC/OS Operating System, are just martial arts. If our internal skills are good, then we can learn the specific implementation process quickly. It will also have a more essential understanding of its knowledge.

I. Basic concepts of processes:
1. Why do processes need to be introduced to computer operating systems:
The purpose of introducing processes in the operating system is to allow concurrent execution of multiple programs to improve resource utilization and system throughput.
2. Process concept:
A process is an execution of a program, and a process is the smallest unit and scheduling unit with resources. (In the thread-introduced operating system, the thread is the smallest scheduling unit)
3. What is the composition of a process?
A process consists of three parts: Process Control Blocks (PCB), data, and programs. PCB is the soul of the process.
4. Process status:
The three most basic statuses of a process are: running, readying, and block)
5. Differences between processes and programs:
The main difference between a process and a program is that the process is dynamic and the program is static. Programs running in a process are executable code stored on the hard disk.
6. Advantages and Disadvantages of processes

(Everything has two sides. We should pay attention to its advantages and disadvantages when learning. In the process of discovering the disadvantages of things, people constantly improve things and introduce new things. During the learning process of the operating system, we will find many such examples. In the process of constantly pursuing perfection, people constantly introduce new knowledge points-the appearance of processes and threads can fully explain all this)
Advantage: Concurrent execution of multiple programs
Disadvantage: the concurrent execution of a program consumes a huge amount of time and space, and each process carries an excessive "burden" during the switchover, resulting in reduced system efficiency.
To solve this problem, people think that the process does not own resources when it is not running. This introduces the thread concept: the thread itself does not own resources or has very few resources, A process is only the basic unit of resources and a thread is the basic unit of scheduling.
7. Thread introduction:
In the operating system, threads are introduced to reduce the time-space overhead of concurrent program execution, so that the operating system has better concurrency.

Ii. How to Implement processes and threads in Linux
1. in Linux, The task_struct struct is used to describe the process PCB. We can see the definition of the Process task_struct and the description of the Process status in include/Linux/sched. h.
1> Process status in Linux
A. Running status: the process is running or waiting for running in the running queue.
B. The process is waiting for an event to complete (for example, waiting for data to arrive ). A signal or timer can wake up while waiting.
C. Non-disruptive Wait Status: the process is waiting for an event to be completed and cannot be awakened by a signal or timer. It must wait until the event occurs.
D. Stiff state: the process has been terminated, but the process descriptor still exists until the parent process calls the wait () function and releases it.
E. Stop status: the process stops running after receiving the sinstop, sigstp, sigtin, and sgiou signals, or the process is being tracked.
Tiger-John note:

1. In include/Linux/sched. H, we can see the specific implementation of the Process status in linxu:
# Define task_running 0
# Define task_interruptible 1
# Define task_uninterruptible 2
# Define task_zombie 4
# Define task_stopped 8
Where:
Task_running is in the ready state, and the process currently only waits for CPU resources.
Task_interruptible and task_uninterruptible are both blocking states. The process is currently waiting for other system resources except the CPU. The former can be awakened by signals, and the latter cannot.
Task_zombie is a zombie, and the process has ended running, but the process control block has not been deregistered.
Task_stopped is a pending state and is mainly used for debugging purposes. After receiving the sigstop signal, the process enters this status and resumes running after receiving the sigcont.

2. You can run the PS or pstree command on the terminal to view the processes in the current system.
Run the ps command to view the current status of the process. The running status is R, the stoppedwait status is S, the stoppedwait status is d, the dead status is Z, and the stoppedstatus is T.
Instance:
Think @ Ubuntu :~ $ PS-eo pid, stat
PID stat
1 ss
2 S
3 S
37 Sn
364 SS
371 S <
442 S <s
1060 SL
1081 SSL
1085 SSL
1203 SS +
3782 SS
3803 R +

Tiger-John note:

There are some suffix characters in the running results, which are <(high-priority process), n (low-priority process), L (Memory Lock page, that is, the page cannot be swapped out of memory), s (the process is the first process of the session), L (multi-threaded process), and + (the process is located in the frontend process group ).
For example, SSL indicates that the process is in the stoppedwait state, and the process is the first process of the session, and is a multi-threaded process.
2. What are the methods for inter-process communication in Linux?
1> pipeline (PIPE): A half-duplex communication method. data can only flow in one direction and can only be used between unrelated processes. The kinship of a process usually refers to the parent-child process relationship.
2> named pipe: A famous pipe is also a half-duplex communication method, but it allows communication between unrelated processes.
3> semophore: a semaphore is a counter that can be used to control access to shared resources by multiple processes. It is often used as a lock mechanism to prevent other processes from accessing a shared resource. Therefore, it is mainly used for synchronization between processes and between different threads in the same process.
4> Message Queue: a message queue is a linked list of messages stored in the kernel and identified by the Message Queue identifier. The message queue has overcome the disadvantages of few signal transmission information, the pipeline can only carry unformatted byte streams, and the limited buffer size.
5> signal (Sinal): a signal is a complex communication method used to notify the receiving process that an event has occurred.
6> shared memory (shared memory): The Shared Memory maps a piece of memory that can be accessed by other processes. The shared memory is created by one process, but can be accessed by multiple processes. Shared memory is the fastest IPC method. It is specially designed for the low efficiency of communication between other processes. It is often used with other communication mechanisms, such as signal two, to achieve synchronization and communication between processes.
7> socket: socket is also a communication mechanism between processes. Different from other communication mechanisms, socket can be used for process communication between different processes.
3. Process Control
1> Linux Process Control includes process creation, execution, process exit, and process priority change.
In Linux, system calls used to control processes include:
A. fork: used to create a new process.
B. Exit: Used to terminate a process.
C.exe C: used to execute an application
D. Wait: suspends the parent process and waits for the child process to terminate.
E. getpid: obtains the ID of the current process.
F. Nice: the priority of the process to be changed
4. process ID
1> in Linux, each process is identified by a unique process ID. The process ID is a non-negative number. In addition to the process ID, each process has other information that can be obtained through the corresponding function.

2> main functions:

Pid_t getpid (void): Obtain the process ID
Pid_t getppid (void): obtains the ID of the parent process of the process.
Pid_t getuid (void): obtains the actual user ID of the process.
Pid_t geteuid (void): Obtain the valid user ID of the process.
Pid_t getgid (void): obtains the actual group ID of the process.
Pid_t getegid (void) to obtain the valid group ID of the process

Tiger-johen description:
The declaration of these functions is in the SYS/types. h and unistd. h header files.
2> concepts related to user IDs and group IDs
A. Actual user ID (UID): identifies the user who runs the process
B. valid user ID (EUID): identify the user identity to run the process.
For example, a common user a runs a program, which is run as root and has the root permission when running the program. In this case, the actual user ID is the ID of user A, and the valid user ID is the root user ID.
3> function instances:
Header file: # include <unistd. h>

# Include <sys/types. h>
Function Definition: pid_t getpid (void)
Function Description: getpid () is used to obtain the process identifier of the current process. Many programs use this value to create a temporary file to avoid problems caused by the same temporary file.
Returned value: the process identifier of the current process.
Function example:

# Include <stdio. h>

# Include <sys/types. h>
# Include <unistd. h>
Main ()
{
Printf ("pid = % d/N", getpid ());
}
3. process memory image
1. Converting a program into a process in Linux
A. in Linux, the generation of C Programs is divided into four stages:

Pre-compile

Compile

Assembly

Link

Tiger-johen description:

The compiler GCC has been precompiled, compiled, and compiled to convert the source program file to the target file.
B. When the program is executed, the operating system copies the executable program to the memory. To convert a program to a process, follow these steps:
The kernel reads the program into the memory and allocates memory space for the program.
The kernel assigns the process identifier (PID) and other resources to the process.
The kernel saves the PID and corresponding status information for the process, and puts the process in the running queue for execution. After the program is converted to a process, it can be executed by the scheduling program of the operating system.
2. memory image of the process
A. The memory image of a process refers to how the kernel stores executable program files in the memory. During the process of converting a program into a process, the operating system copies the executable program from the hard disk to the memory.
B. The general layout of program images in Linux is as follows: (from low address to high address)
1> code segment: the code segment is read-only and can be shared by multiple processes.
2> Data Segment: stores initialized variables, including global variables and initialized static variables.
3> uninitialized data segment: stores uninitialized static variables, also known as BSS segments.
4> heap: used to store variables dynamically allocated during program running
5> Stack: Call a function, save the return address of the function, function parameters, and local variables defined in the function.
Tiger-johen description:

Differences between executable programs and memory images:
A. the executable program is located in the disk and the memory image is located in the memory;
B. the executable program does not have a stack because the stack is allocated only when the program is loaded into the memory;
C. Although the executable program has uninitialized data segments, it is not stored in the executable file on the hard disk;
D. the executable program is static and unchanged, while the memory image changes dynamically as the program is executed.



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.