Linux programming-process introduction, linux Programming process

Source: Internet
Author: User

Linux programming-process introduction, linux Programming process
Process

The C-language code we usually write is compiled by the compiler, and eventually it becomes an executable program. When the executable program runs (before it ends ), it becomes a process.



A program is an executable file stored on the storage medium, and a process is a program execution process.The program is static and the process is dynamic..


In Linux, the operating system completes tasks one by one through processes, and processes are the basic unit for managing transactions. The process has its ownProcessing Environment(For example, which environment variables are used, where the program runs, and who is running the program) andSystem Resources(Such as CPU usage, memory, I/O devices, data, and programs ). We can understand that a company is equivalent to an operating system, a department is equivalent to a process, and a company is managed by a Department (the system is managed by a process). Each department has its own resources for each department, such as personnel, computer equipment, printers, etc.


Process status

Our current computers are basically multitasking. When we talk about QQ, we can watch the video at the same time. This is equivalent to running both QQ and video programs (two processes) at the same time ). In the early days, the computer's CPU was single-core (a single core theoretically only runs one task). How did it implement multiple tasks? This involvesProcess Scheduling Policy. For example, there are three processes A, B, and C. When we run A single CPU, there is only one process running at every moment. If A finishes running, run B, run B, run C, run C, run A, and run A. The computing speed of CPU is fast enough, and the interval between Running A and A is short enough, from A macro perspective, we can see that A, B, and C are running at the same time. This is the core principle for running multiple tasks on A single CPU,Multi-task implementation through time slice round robin scheduling policy (for more details, see Linux Process Scheduling Analysis).


From the above example, we can know that for process A, sometimes it is running, sometimes it is not running, and the two statuses are different. Therefore, the process is stateful, and at the same time, the status can be converted from the execution status to the non-execution status. Here, we can simply divide the entire life cycle of a process into three States (not actually the three States): readiness, execution, and waiting.

Readiness:

The process already hasAll the execution conditions are waiting for the CPU processing time to be allocated.

Execution status:

The process is running with CPU usage.

Waiting state:

Because the process does not haveStatus in which execution is temporarily unavailable due to certain execution conditions.


It should be noted that both the ready state and the waiting state do not execute, but they are different. The ready state means that the conditions are met, the time is not reached, and the waiting state is not met.


Similarly, the three States of a process can be converted to one another:



In order to better convert these three states, we will give you an example of how to buy a train ticket.


Mike hurried to the train station to buy a train ticket. It was too anxious to arrive at the ticket office and found that he forgot his ID card. At this time, even if Mike waited in line, it was useless because Mike did not have the conditions for buying a ticket (without his ID card ), at this time, Mike belongsWait state.


Mike called the object and asked her to bring her ID card. When the meeting was over, his ID card was sent. At this time, Mike could go to the queue to buy a ticket. It was just time, so Mike could buy a ticket, at this time, Mike belongsReadiness. And this process isByWait state to ready state.


After waiting for 10 minutes, we finally arrived at Mike. Mike started to buy tickets. At this time, Mike belongsExecution status. And this process isFrom ready state to execution state.


During the course of buying a ticket, Mike's object called him and asked Mike to help her buy a train ticket. However, Mike didn't have the ID card of her object. Then, mike continues to wait for his object to send his ID card. At this time, MikeFrom execution to waiting.


In this case, Mike bought a train ticket for his colleagues (more than 100 tickets are required ), there are still many people waiting in the queue, and those waiting in the queue will be upset. Then the conductor said, if you haven't finished the process in 20 minutes, please wait in the queue. As a result, it took Mike 20 minutes to finish the process, so he went to the back to requeue. At this time, MikeFrom execution state to ready state.


Process Control Block

For the operating system, it needs to control many processes. At the same time, each process has A different state. How does the system know that A is executed to B instead of C? How does the system coordinate the control process?


When we run a program to make it a process, the system will open up a piece of memory space to store the data information related to the process, and the data information is through the struct (task_struct, open/usr/include/linux/sched. h can be found in the definition of task_struct) to store. We call the struct that stores process-related data as a process control block. The operating system uses this process control block to control processes. For more details, see Linux Process Management.


Process control blocks are the most important record-type data structures in the operating system. The process control block records all information required to describe the progress of a process and control its operation. It is the only identifier of a process. The process control block contains a lot of information,The most important part is the process number.For other information, we will not discuss it in detail here.


Process number

Each process is identified by a process number.Its type is pid_t (unsigned integer), and the process number range is: 0 ~ 32767. The process number is always unique, but it can be reused. After a process is terminated, its process number can be used again.



The system allows a process to create a new process. A new process is a sub-process. A sub-process can also create a new sub-process to form a process tree structure model. All processes in the Linux system are also in a tree structure. The root is automatically constructed by the system, that isThe number 0 process executed in the kernel state. It is the ancestor of all processes.. A process whose process number is 0 is usually a scheduling process and is often called a swapper process ). Process 1 (kernel state) is created by process 0. Process 1 is responsible for Kernel initialization and system configuration, create several kernel threads for high-speed cache and virtual master memory management.Subsequently, process 1 calls execve () to run the executable program init, and changes to the user State process 1, that is, the init process.


Therefore, all processes in Linux are directly or indirectly created by the init process.


Next, we will introduce three different process numbers.


Process ID (PID ):

A non-negative integer that identifies a process.

Parent process ID (PPID ):

Any process (except the init process) is created by another process. This process is called the parent process of the created process, and the corresponding process is called the parent process number (PPID ). For example, process A creates process B, and the process Number of process A is the parent process Number of process B.

Process Group number (PGID ):

A process group is a collection of one or more processes. They are associated with each other. A process group can receive various signals from the same terminal. The associated process has a process group number (PGID ). This process is a bit similar to a QQ group. The group is equivalent to a QQ group. Each process is equivalent to each friend. Every friend is pulled into this QQ Group for convenient management, especially when something is notified, as long as you scream in the group, everyone will receive it, which is simple and rude. However, the process group number is a little different from the QQ group number,By default, the current process number is treated as the current process group number..


The Linux operating system provides the getpid (), getppid (), and getpgid () functions for obtaining process numbers ().


Required header file:

# Include <sys/types. h>

# Include <unistd. h>


Pid_t getpid (void );

Function:

Obtain the current process ID (PID)

Parameters:

None

Return Value:

Current process number



Pid_t getppid (void );

Function:

Obtain the parent process ID (PPID) of the process that calls this function)

Parameters:

None

Return Value:

The parent process ID (PPID) of the process that calls this function)


Pid_t getpgid (pid_t pid );

Function:

Obtain the process group number (PGID)

Parameters:

Pid: process ID

Return Value:

If the parameter is 0, the current process group number is returned. Otherwise, the process group number specified by the parameter is returned.


The sample code is as follows:

#include <stdio.h>#include <sys/types.h>#include <unistd.h>int main(int argc, char *argv[]){pid_t pid, ppid, pgid;pid = getpid();printf("pid = %d\n", pid);ppid = getppid();printf("ppid = %d\n", ppid);pgid = getpgid(pid);printf("pgid = %d\n", pgid);return 0;}

The running result is as follows:



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.