Linux Learning Eight---basic knowledge of Linux process

Source: Internet
Author: User

First, the Linux process

Linux is a multi-user multitasking operating system.

Multi-user refers to the ability of multiple users to use the computer at the same time.

multitasking means that Linux can run several tasks at the same time.

The process is simply the execution of the program, a key feature of the Linux system is the ability to start multiple processes at the same time. Based on the definition of the operating system: The process is the smallest unit of operating system resource management.

Concept of 1.Linux Process

A process is a dynamic entity, which is the process of running a program once, and the process is the basic unit of the operating system resource allocation.


The difference between a process and a program: The process is dynamic and the program is static. A process is a program that executes, and a program is an executable code that is saved on a hard disk.

The difference between a process and a thread: In order for the computer to run many other tasks at the same time, many threads are divided within the process. A thread is a basic unit within a process that is smaller than the process and can run independently. A process has a separate memory unit in its running process, but the thread does not have a system resource, and it shares all the resources owned by the process with other threads of the same process.


Sign of the 2.Linux process

In the Linux operating system, each process is identified by a unique process ID.

The process ID is a non-negative number, and each process has other identifying information in addition to the process ID. They can all be obtained by corresponding functions. The function declaration is in the Unistd.h header file.

Getpid (): Get process ID

Getppid (): Gets the ID of the process parent process

Getuid (): Get process actual user ID

Geteuid (): Get process valid User ID

Getgid (): Get the actual group ID of the process

Getegid (): Get process valid Group ID

Structure of the 3.Linux process

A process in Linux consists of 3 parts:code Snippets, data segments, stack segments.
Code snippet: A running code that holds the program.
Data segment: A global variable, constant, or static variable that holds a program.
Heap area in a stack segment: stores dynamically allocated memory variables.

Stacks in a stack segment: for function calls, which hold the parameters of a function, local variables defined inside the function.

Status of the 4.Linux process

Execution status: The process is executing or waiting for execution in the Iozai execution queue.

interruptible Wait Status: The process is waiting for an event to complete.

The wait process can be awakened by a signal or timer.

Non-interruptible wait status: The process is waiting for an event to complete. The wait process cannot be awakened by a signal or timer and must wait until the waiting event occurs.


Zombie state: The process has been terminated. However, the process description descriptor still exists until the parent process calls the wait () function and releases it.

Stop state: The process stops executing after receiving sigstop, SIGSTP, Sigtin, Sigtou signals, or the process is being traced (when the program is debugged). The process is in a tracked state).

Control of the 5.Linux process

Linux Process Control includes creating processes, running new programs, exiting processes, and changing process priorities .

Fork: Used to create a new process.

Exit: Used to terminate the process.

EXEC: Used to run an application.

Wait: suspends the parent process. Wait for the child process to terminate.


Getpid: Gets the process ID of the current process.

Nice: Change the priority of the process.

Ii. Creating a process

#include <stdio.h> #include <sys/types.h> #include <unistd.h>int main () {    pid_t pid;       printf ("Process Creation study\n");    Pid=fork ();    Switch (PID)    {case       0:         printf ("Child process is Tunning,curpid are%d,parentpid is%d\n",                            pid,getppid ()) ;         break;       Case-1:         printf ("Process creation failed\n");         break;       Default:         printf ("Parent process is Running,childpid are%d,parentpid is%d\n",                             pid,getpid ());        break;    }    Exit (0);     return 0;}
In normal cases, the function has a maximum return value. But the fork function is special, it has two return values, that is, the call returns two times at a time. After a successful call to the fork function, the current process has actually split into two processes, one is the original parent process, and the other is the child process that was just created. The parent process is separated from where the fork function is called. The fork function has two return values. One is the return value after the parent process calls the fork function, which is the ID of the child process that was just created. Another is the return value of the fork function in the child process. The return value is 0.


Fork returns two times if the process is created successfully, and if the process creation fails, it simply returns-1.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvbgfuemhpahvpxzewmdg2/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/southeast ">

Visible from the diagram. The above two execution results are counted as the parent process executes first . In general, it is indeterminate whether the parent process executes first or the child process before the fork is executed. This depends on the scheduling algorithm used by the kernel.

The return value after the parent process calls the fork function is the ID (3465) of the child process just created, and the parent process ID (3464) is obtained through the GETPID function.

The return value after the child process calls the fork function is 0, and the child process gets the parent process ID (3464) through the Getppid function.

Linux Learning Eight---basic knowledge of Linux process

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.