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 means that multiple users can use the computer at the same time;

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

The process is simply a running program, and a key feature of the Linux system is the ability to start multiple processes at the same time. According to 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 executing the 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, the program is static, the process is a running program, and the program is the executable code that is saved on the hard disk.

The difference between a process and a thread: In order for the computer to perform more 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. The process has a separate memory unit in the execution 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 some other identifying information in addition to the process ID, which can be obtained through the corresponding function. The function is declared 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: The executable 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: Used for function calls, which hold parameters of functions, local variables defined inside functions.

Status of the 4.Linux process

Running state: The process is running or is waiting to run in the Iozai run 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 state: 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 terminated, but the process descriptor persists until the parent process calls the wait () function.

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

Control of the 5.Linux process

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

Fork: Used to create a new process.

Exit: Used to terminate the process.

EXEC: Used to execute an application.

Wait: suspends the parent process and waits 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 general, the function has a maximum return value, but the fork function is very special, it has two return values, that is, the call returns two times at a time. After the fork function is successfully called, the current process is 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, the return value is the ID of the child process that was just created, and the other is the return value of the fork function in the child process, which is the return value of 0.
Fork returns two times if the process is created successfully, and only 1 is returned if the process creation fails.


As the graph shows, the above two run results are counted as the parent process runs first . In general, it is uncertain whether the parent process executes first or the child process after the fork, depending 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

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.