Linux system Programming--Introduction to the process

Source: Internet
Author: User
Tags ticket

Process

We usually write the C language code, compiled by the compiler, eventually it will become an executable program, when the executable program (before the end), it becomes a process.



A program is an executable file that is stored on a storage medium, and the process is the process by which the program executes. The program is static and the process is dynamic .


in a Linux system, the operating system is a process to complete a task, the process is the basic unit of management transactions. The process has its own independent processing environment (such as which environment variables are currently used, where the program is running, which user is currently running the program, etc.) and system resources such as: Processor CPU utilization, memory, I/O devices, data, programs). we can understand that the company is equivalent to the operating system, the department equivalent to the process, the company through the Department to manage (system through process Management), for each department, each department has its own resources, such as personnel, computer equipment, printers and so on.


process Status

Our current computer is basically multi-tasking, we chat QQ time, at the same time can look at the video, where the equivalent of QQ and video two programs running simultaneously (two processes). In the early days, the computer's CPU was single-core (single-core theory only runs a task), how does it do multitasking? This involves the scheduling strategy of the process . Now give you an example, there are a,b,c three processes, in our single CPU, every moment only one process is running, if a run out, b run, b run, C Run, C Run, a run, and the CPU is fast enough, a two run time interval short enough, from the macro On the view we see A,b,c as if running at the same time, this is the realization of single CPU running multiple tasks of the core principle, through the time slice polling scheduling strategy to achieve multi-tasking (more details, see "Linux Process scheduling analysis").


From the above example, we can learn that for A process, sometimes run, sometimes not run, two states are not the same, so, the process is stateful, at the same time, the state can be converted to each other, from the state of execution to the state of non-execution, here, We can simply divide the entire life cycle of the process into three states (in fact not referring to these three states): ready state, execution state, waiting state.

Ready State :

The process already has all the conditions for execution and is waiting to allocate CPU processing time.

Execution State :

The process is consuming CPU running.

Waiting State :

The status of a process that is temporarily unable to continue because it does not have certain execution conditions.


It is important to note that both the ready state and the waiting state are not executed, but they are different , and the ready state means that the condition is satisfied, the time is not reached, and the waiting state is not satisfied.


In the same way, these three states of the process can be converted to each other:



In order to give you a better conversion of these three states, give us an example of buying a train ticket.


Mike hurried to the railway station to buy train tickets, too anxious, to the ticket office only to find that forget to bring ID card, this time, even if Mike line up is useless, because Mike does not have the condition of ticket (without ID), this time Mike is waiting state .


Mike gave it to the caller, asked her to bring her ID card, and so on, the ID was sent, and then Mike could go in line to buy a ticket, just when Mike could buy a ticket, when Mike was ready . The process is switched from the waiting state to the ready state .


After waiting for 10 minutes, Mike finally arrived, and Mike started to buy the ticket, and Mike was in the execution state . The process is transformed from the ready state to the execution state .


In the course of the ticket, Mike's object called him and asked Mike to buy a train ticket for her, but Mike had no ID for her object, and Mike continued to wait for his object to send the ID, when Mike was switched from execution to waiting .


If this is the case, Mike buy fire tickets for the company's colleagues bought (need to buy more than 100 tickets), in the process of buying tickets (execution state), there are many people in the queue, behind the queue of people certainly uncomfortable, then the conductor said, 20 minutes later, if you have not finished, please go to the back line. As a result, Mike spent 20 minutes or did not finish, so obediently to the back of the queue, this time, Mike from the execution state to the ready state .


Process Control block

For the operating system, it needs to control many processes, and each process has a different state, how does the system know that A executes to B instead of C? How does the system coordinate the control process?


when we run a program to make it into a process, The system will open up a memory space to hold the data associated with this process, and this data information is stored through the structure (task_struct, open/usr/include/linux/sched.h can find task_struct definition), We refer to this structure, which holds the process-related data information, as the process control block. the operating system uses this process control block to manipulate the control process . For more information, see "Linux Process Management".


The process control block is the most important record-type data structure in the operating system. The process Control block records all the information needed to describe the progress of the process and to control the process's operation, which is the only sign of the process's existence. There is a lot of information in the process control block, the more important one is the process number , and the other information we don't discuss in detail here.


Process Number

each process is identified by a process number whose type is pid_t (unsigned integer) and the range of the process number: 0~32767. Process numbers are always unique, but process numbers can be reused. When a process terminates, its process number can be used again.



The system allows a process to create a new process, the new process is a child process, and the child process can also create a new child process, forming a process tree structure model. All processes of the entire Linux system are also a tree-shaped structure. The roots are automatically constructed by the system, which is the NO. 0 process that executes in the kernel state, which is the ancestor of all processes . A process with a process number of 0 is usually a dispatch process, often referred to as a Swap process (swapper). Created by process number No. 0 (kernel state), number 1th is responsible for performing some kernel initialization and system configuration, and creating several kernel threads for cache and virtual main memory management. the 1th process then calls Execve () to run the executable init, which becomes the user-state process 1th, the init process .


Therefore, all processes under Linux are created directly or indirectly by the INIT process.


Next, we will introduce you to three different process numbers.


process Number (PID):

Identifies a non-negative integer number of the process.

Parent Process Number (PPID):

any process (other than the Init process) is created by another process called the parent process of the process being created, and the corresponding process claims to be the parent process number (PPID). For example ,a process creates a B process, and the process number of a is the parent process number of the B process.

Process Group number (PGID):

A process group is a collection of one or more processes. They are interconnected, and the process group can receive various signals from the same terminal, and the associated process has a process group number (PGID). This process is a bit similar to QQ Group, the group equivalent to QQ group, each process equivalent to each friend, the friends are pulled into this QQ group, mainly to facilitate management, especially notice some things, as long as in the group Roar, all received, simple rude. However , this process group number and QQ group number is a bit different, by default, the current process number will be treated as the current process group number .


The Linux operating system provides three function Getpid (), Getppid (), Getpgid () to get the process number.


Required header file :

#include <sys/types.h>

#include <unistd.h>


pid_t getpid (void);

features :

Get this process number (PID)

Parameters :

No

return value :

This process number



pid_t getppid (void);

features :

Gets the parent process number (PPID) of the process that called this function

Parameters :

No

return value :

The parent process number of the process calling this function (PPID)


pid_t getpgid (pid_t pid);

features :

Get Process group number (PGID)

Parameters :

PID: Process number

return value :

The parameter is 0 o'clock returns the current process group number , otherwise the process group number of the process 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 ();p rintf ("pid =%d\n", pid);pp id = getppid ();p rintf ("ppid =%d\n", ppid);p gid = Getpgid (PID); printf ("Pgid =%d\n", pgid); return 0;}

The results of the operation are as follows:



Linux system Programming--Introduction to the process

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.