Linux programming process (I): Basic Process Overview

Source: Internet
Author: User
Tags sigint signal

1. What is a process?

From the user's perspective, a process is a program execution process.
From the core of the operating system, processes are the basic units of resources such as memory allocated by the operating system and CPU time slice.
A process is the smallest unit of resource allocation.
Each process has its own address space and execution status.
A multitasking operating system like UNIX allows many programs to run at the same time, and every running program forms a process.


2. Process Data Structure

Static Process description: consists of three parts: PCB, the relevant program segment, and the data structure set for which the program segment operates.
Process control block: describes the process and all information required to control the process.
Code segment: the code segment of the program that can be executed by the Process scheduler on the CPU.
Data Segment: The data segment of a process. It can be the raw data processed by the program corresponding to the process, or the intermediate or final data generated after the program is executed.

Stack segment: You need to perform data operations here when running the program, store temporary data, and open up function stacks. In Linux, the stack increases from high address to low address.


3. Processes and Procedures

The process is dynamic and the program is static.
The life cycle of a process is relatively short, and the program is permanent.
Process Data Structure PCB.
One process can correspond to only one program, and one program can correspond to multiple processes.


Iv. Process status change

Running status (task_running)
Stoppedsleep status (task_interruptible)
Uninterrupted sleep (task_uninterruptible)
Paused (task_stopped)
Dead state (task_zombie)


5. Process Control Block

Process description

A process identifier is used to uniquely identify a process.

Process control information

Current Process status
Process Priority
Program start address
Various timing information
Communication Information

Resource Information

Memory usage and data structure pointer for management
Exchange zone information
I/O device number, buffer, device-related number Structure
File System pointer

Field Protection Information

Register
PC
Program status word psw
Stack pointer


6. process ID

Each process is assigned a unique number, which we call process identifier or PID.
Is a positive integer ranging from 2 to 32768.
When a process is started, it selects the next unused number in sequence as its own PID.
Number 1 is generally reserved by the special process init


7. Process Creation

The names and formats of Process Creation primitives provided by different operating systems are different. However, after the Process Creation primitives are executed, the work done by the operating system is roughly the same, including the following:
Assign an internal identifier to the newly created process and create a process structure in the kernel.
Copy the environment of the parent process
Allocate resources to processes, including all elements (programs, Data, user stacks, etc.) required by the process image ),
Copy the content of the parent process address space to the process address space.
Set the Process status to ready, and insert the ready queue.


VIII. Destruction of processes

When a process is terminated, the operating system does the following:
Disable Soft Interrupt: No signal is processed because the process is about to terminate;
Recycle resources: release all resources allocated by the process, such as closing all opened files and releasing the data structure of the process;
Write accounting information: records the accounting data generated during the process (including various statistics during the process) into a global accounting file;
Set the process to a dead state: Send the sigchld signal to the parent process to send the termination information status to the specified storage unit;
Process Scheduling: Because the CPU has been released, the CPU needs to be distributed by the process scheduling.


9. Five methods to terminate a process

Return from main function return
Call exit (c-library function)
Call _ exit (system call)
Call abort (generate SIGABRT signal and terminate abnormally)
Signal termination (for example, SIGINT signal generated by Ctrl + C)

Note that the main function will call exit; exit will call _ exit; exit will call fflush, but _ exit will not call fflush.


Atexit can register a termination handler, and ansi c requires that a maximum of 32 Termination handlers can be registered. Calls to terminate the Handler are in the opposite order of registration

Int atexit (void (* function) (void ));


Reference: apue

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.