Introduction to Linux process programming (1)

Source: Internet
Author: User
Tags sleep function
Article title: Introduction to Linux process programming (1 ). Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Abstract: This section describes the process definition. As the basic cell of the system, a process is not only an entity that runs independently inside the system, but also a basic entity for independent competition of resources. Understanding the nature of a process is of great significance for understanding, describing, and designing an operating system. Understanding the activities and status of processes is also conducive to the compilation of complex programs.
  
1. basic concepts of processes
  
First, let's take a look at the definition of a process. a process is a program with independent functions. a running activity that can be executed concurrently in a data set is an active computer program. As the basic cell of the system, a process is not only an entity that runs independently inside the system, but also a basic entity for independent competition of resources. Understanding the nature of a process is of great significance for understanding, describing, and designing an operating system. Understanding the activities and status of processes is also conducive to the compilation of complex programs.
  
1.1 Process status and status transition
  
Now let's take a look at the status and status transition of processes in the life cycle. The following describes the various statuses of the process state model in LINUX:
  
User status: the status of a process running in the user status.
Kernel status: the status of the process running in the kernel status.
In-memory ready: The process is not executed, but is in the ready state. it can be executed as long as the kernel schedules it.
Sleep in memory: the process is sleeping and the process is stored in the memory and is not switched to the SWAP device.
Ready and out: The process is in the ready state, but it must be swapped into the memory before the kernel can schedule it again for running.
Sleep and swap out: The process is sleeping and the memory is swapped out.
Preemptive: when a process returns the user state from the kernel state, the kernel takes precedence over it, performs context switching, and schedules another process. The previous process was in the preemptive status.
Creation status: the process has just been created. This process exists, but it is neither ready nor sleep. This state is the initial state of all processes except process 0.
Zombie: The process stops calling exit and no longer exists. However, there is still a record in the progress table. this record can be collected by the parent process.
Now we can see the process status transition from process creation to exit. It must be noted that a process does not have to go through all states in its lifecycle.
  
First, the parent process creates a sub-process by calling fork. when fork is called, the sub-process is first in the creation state. after the fork call configures the kernel data structure and the private data structure of the sub-process for the sub-process, the sub-process enters the ready state 3 or 5, that is, the sub-process is ready in the memory, or the SWAP device is ready because the memory is insufficient.
  
Assuming that the process is in the memory, the sub-process can be scheduled by the kernel scheduler to run the CPU. During kernel scheduling, the process enters the kernel state, and then the kernel state returns to the user state for execution. After a certain period of time, the process is scheduled by the scheduling program and enters the kernel state, and then enters the ready state. Sometimes, when a process is running in the user state, it also enters the kernel state by using the system call because the kernel service is required. after the service is completed, the kernel state is changed back to the user state. It should be noted that the process may be preemptible when it returns from the kernel status to the user status and enters the status 7. this is because the CPU is urgently needed for processes with higher priorities, you cannot wait until the next scheduling time, causing preemption.
  
The process also enters the sleep state because the requested resource cannot be satisfied until the requested resource is released. If the process is sleeping in the memory, the memory is insufficient. when the sleep time of the process reaches a threshold value, the process will be out of the memory by SWAP, making the process sleep on the SWAP device. This situation may also happen to the ready process.
  
When a process calls the exit system call, the process enters the kernel state, executes the exit call, and ends when it enters the frozen state. The above is a brief description of process status transition.
  
The context of a process is composed of user-level context, register context, and system-level context. The main content is the content of the user space of the process, the Register content, and the kernel data structure related to the process. When the system receives an interrupt, executes a system call, or performs context switching by the kernel, the context of the process is saved. A process runs in its context. to schedule a process, context switching is required. The kernel allows context switching in four situations:
  
When the process goes to sleep;
After a process is executed, the system call will return the user status, but the process is not the most qualified process;
When the kernel returns the user status after the interrupt processing is completed, but finds that the process is not the most qualified process;
When the process exits (after the execution system calls exit.
Sometimes the kernel requires that the current execution be terminated and executed immediately from the previously saved context. This can be implemented by setjmp and longjmp. setjmp stores the saved context in the data space (u zone) of the process and continues to be executed in the current context. once longjmp is encountered, the kernel extracts the previously saved context from the u area of the process and restores the context of the process to the original saved context. At this time, the kernel will execute the process from setjmp and return 1 to setjmp.
  
For a process waiting for resources or other reasons, it enters the sleep state through the kernel sleep algorithm. This algorithm is similar to the sleep function mentioned later in this chapter. The algorithm sleep records the original processor priority of the process, sets the process to sleep state, puts the process into the sleep queue, records the cause of sleep, and performs context switching for the process. The kernel uses the wake-up algorithm to wake up a process. if a resource is released, it will wake up all the processes that are waiting for the resource to sleep. If a process sleeps at a level that can receive a soft interrupt signal (signal), the process's sleep can be awakened by the arrival of the soft interrupt signal.
  
1.2 Process control
  
Now let's talk about the process control, mainly about the processing process of fork, exec, wait, and exit by the kernel, laying a conceptual foundation for the next section to learn about these calls, it also introduces the process of system startup (boot) and the role of process init.
  
In Linux, the only way to create a process is to use the system to call fork. The kernel takes the first step to complete the system call fork. it allocates a table item to the new process in the progress table. The system has a limit on the number of processes that a user can run at the same time. this limit does not apply to super users, but cannot exceed the maximum number of table items in the table. Step 2: give the sub-process a unique process ID (PID ). The process ID number is actually the index number of the table item in the progress table. Step 3: Copy the table entry copy of a parent process to the child process. When the kernel initializes the table entry of the child process, the child process is copied from the parent process. Therefore, sub-processes have the same uid, euid, gid, nice value used for priority calculation, current directory, current root, user file descriptor table, and so on as the parent process. Step 4: Add 1 to the reference numbers of the file table and index node table connected to the parent process. These files are automatically connected to the child process. Step 5: the kernel creates a user-level context for the sub-process. The kernel allocates memory for the u zone and the secondary page table of the child process, and copies the content of the parent process zone. This generates the static part of the process. Step 6: generate the dynamic part of the process. The Kernel copies the first layer of the context of the parent process, that is, the Register context and kernel stack. the kernel then virtual a context layer for the child process, this is to enable the sub-process to "restore" its context. At this time, the call will return the child process pid to the parent process, and the child process will return 0.
  
In Linux, exit is called when a process terminates execution. When a process sends this call, the kernel releases the resources occupied by the process, releases the memory space occupied by the process context, and retains the progress table, set the process state field recorded in the progress table item to the dead state. When a process receives an uncaptured signal, the kernel calls exit from the kernel to exit the process. The parent process obtains the timing data recorded in the progress table of its child process through wait, and releases the progress table. Finally, the kernel enables process 1 (init process) to receive all sub-processes of processes terminated. If a child process freezes, a SIGCHLD soft interrupt signal is sent to the init process.
  
A process synchronizes with its child process by calling wait. If the called process does not have a child process, an error is returned, if a dead child process is found, the PID of the child process and the parameters provided to the parent process upon exit are obtained. If there are sub-processes, but there are no dead sub-processes, the process that sends the call will sleep at an interruption level until it receives a signal or other signal from the sub-process.
  
Another main content of process control is to reference other programs. This function is implemented by the system calling exec. this call reads an executable program file in place of the process that sends out the call. The kernel reads the body of the program file, clears the data area of the original process, and clears the address of the original user soft interrupt signal processing function. When exec calls return, the process executes the new body.
  
A system startup process is also called a self-lifting process. This process varies with machines. However, this process serves the same purpose for all machines: to load the operating system into memory and start execution. The computer first reads the content of the boot block to the memory and executes it. the UDF module loads the kernel into the memory from the file system, transfers the control to the kernel entry, and the kernel starts to run. The kernel first initializes its data structure and installs the root file system to the root "/" to form an execution environment for process 0. After the environment of process 0 is set, the kernel starts execution as process 0 and calls the system to call fork. Because process 0 runs in the kernel state, the new process also runs in the kernel state. The new process (process 1) creates its own user-level context, sets and saves the user register context. In this case, process 1 returns the user status from the kernel status to execute the code (exec) copied from the kernel, and calls exec to execute the/sbin/init Program. Process 1 is usually called an initialization process, which initializes a new process.
  
Process init not only generates new processes, but also processes that allow users to register on the system. For example, process init generally generates some getty sub-processes to monitor the terminal. If a terminal is opened, the getty sub-process requires that a registration process be executed on the terminal. after successful registration, a shell program is executed to allow users to interact with the system. At the same time, process init executes the system to call wait to monitor the death of sub-processes, as well as the handover of orphan processes resulting from the exit of the parent process. The above is a rough model of system startup and process init.
  
1.3 concept of process scheduling
  
Linux is a time-sharing system. The Kernel divides each process into a time slice. when the time slice of the process is used up, another process is scheduled to run. The scheduling program on LINUX system is a multi-level feedback loop scheduling. The scheduling method is to split a process into a time slice to take the lead of a process that exceeds the time slice and feed the process back to a queue in several priority queues. Before the process is executed, it must go through such multiple feedback cycles.
  
Process Scheduling is divided into two parts: one is the scheduling time, that is, when scheduling; the other is the scheduling algorithm, that is, how to schedule and which process to schedule. Let's take a look at the scheduling algorithm. assuming that the current kernel requires scheduling, the scheduler selects a process with the highest priority from the processes in the "ready in memory" and "preemptive" states, if there are several priorities
Related Article

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.