Based on CentOS, this paper deeply explains the process concept, PCB, process creation, process status, zombie process and orphan process by von Neumann system and operating system concept.
catalogue
7, Get process identifier through system call
8, Create process - fork-1 through system call
1, Von Neumann system
Most of our common computers follow the von Neumann system.
2, Operating system
Any computer system contains a basic set of programs called the operating system (OS). In general, the operating system includes:
Kernel (process management, memory management, file management, driver management)
Other programs (such as function libraries, shell programs, etc.)
The purpose of designing OS is to interact with hardware, manage all software and hardware resources, and provide a good execution environment for user programs (Applications).
It is a software for integrated software and hardware management.
Computer management hardware: describe first and then organize
1. Describe it with struct structure
2. Organize and use linked lists or other efficient data structures
3, Basic concepts of process
An execution instance of a program, the program being executed, etc,
From the perspective of kernel: the entity that allocates system resources (CPU time, memory).
4, Description process - PCB
Process information is placed in a data structure called process control block, which can be understood as a collection of process attributes, called PCB (process control block). The PCB under Linux operating system is task_struct. It is a part of the process entity and the most important record data structure in the operating system. PCB is the only sign of the existence of a process. The system can and can only control and schedule the process through PCB. PCB records all the information required by the operating system to describe the current situation of the process and control the operation of the process.
1.task_ struct
In Linux, the structure describing a process is called task_struct. task_struct is a data structure of Linux kernel. It will be loaded into RAM (memory) and contain process information.
Content classification:
Identifier: the unique identifier describing this process, which is used to distinguish other processes.
Status: task status, exit code, exit signal, etc.
Priority: the priority relative to other processes.
Program counter: the address of the next instruction to be executed in a program.
Memory pointers: including pointers to program code and process related data, as well as pointers to memory blocks shared with other processes
Context data: data in the processor's registers when the process executes.
I/O status information: including the displayed I/O requests, the I/O devices assigned to the process and the list of files used by the process.
Accounting information: may include the total processor time, the total number of clocks used, time limit, accounting number, etc.
Other information
5, Organizational process
You can find it in the kernel source code. All processes running in the system use task_ The form of struct linked list exists in the kernel.
6, View process
Process information can be viewed through the / proc system folder
You can use user level tools such as top and ps to get
7, Get process identifier through system call
Process id (PID) parent process id (PPID)
#include <stdio.h> #include <sys/types.h> #include <unistd.h> int main() { printf("pid: %d\n", getpid()); printf("ppid: %d\n", getppid()); return 0; }
8, Create process - fork-1 through system call
fork has two return values
The parent and child processes share the code, and the data opens up space respectively. One copy is private (copy on write)
if is usually used for shunting after fork:
#include <stdio.h> #include <sys/types.h> #include <unistd.h> int main() { int ret = fork(); if(ret < 0){ perror("fork"); return 1; } else if(ret == 0) { //child printf("I am child : %d!, ret: %d\n", getpid(), ret); }else { //father printf("I am father : %d!, ret: %d\n", getpid(), ret); } sleep(1); return 0; }
9, Process status
1. Type of status
A process can have several states (in the Linux kernel, a process is sometimes called a task).
The status is defined in the kernel source code:
/* * The task state array is a strange "bitmap" of * reasons to sleep. Thus "running" is zero, and * you can test for combinations of others with * simple bit tests. */ static const char * const task_state_array[] = { "R (running)", /* 0 */ "S (sleeping)", /* 1 */ "D (disk sleep)", /* 2 */ "T (stopped)", /* 4 */ "t (tracing stop)", /* 8 */ "X (dead)", /* 16 */ "Z (zombie)", /* 32 */ };
Of which:
R running: it does not mean that the process must be running. It indicates that the process is either running or in the running queue.
S sleep state: it means that the process is waiting for the event to complete (sleep here is sometimes called interruptible sleep).
D Disk sleep: sometimes called non interruptible sleep. Processes in this state usually wait for the end of IO.
T stopped: the (T) process can be stopped by sending SIGSTOP signal to the process. The suspended process can continue to run by sending SIGCONT signal.
X dead state: this state is just a return state. You won't see this state in the task list.
10, Zombie process
Zombies is a special state. Zombies will occur when the process exits and the parent process does not read the return code of the child process exit.
The dead process will remain in the process table in the termination state, and will always wait for the parent process to read the exit state code. Therefore, as long as the child process exits, the parent process is still running, but the parent process does not read the child process state, and the child process enters the Z state.
For example:
#include <stdio.h> #include <stdlib.h> int main() { pid_t id = fork(); if(id < 0){ perror("fork"); return 1; } else if(id > 0){ //parent printf("parent[%d] is sleeping...\n", getpid()); sleep(30); }else{ printf("child[%d] is begin Z...\n", getpid()); sleep(5); exit(EXIT_SUCCESS); } return 0; }
Zombie process harm: the exit state of the process must be maintained. But if the parent process does not read all the time, the child process will always be in Z state.
Maintaining the exit status itself is data maintenance, which also belongs to the basic process information. Therefore, in the saved task_struct(PCB), the Z status does not exit and the PCB must be maintained all the time.
A parent process creates many child processes, which will cause a waste of memory resources. Because the data structure object itself will occupy memory, think about defining a structure variable in C to open up space at a certain location in memory, which will cause memory leakage.
11, Orphan process
The parent process exits first, and the child process is called "orphan process". The orphan process is adopted by init process 1, and the init process recycles it.
#include <iostream> #include <sys/types.h> #include <unistd.h> #include <stdlib.h> int main () { int ret = fork(); while(1) { if(ret > 0) //Description is the parent process { printf("father : %d, ret : %d\n", getpid(), ret); sleep(3); exit(0); } else if(ret == 0) { printf("child : %d, ret : %d\n", getpid(), ret); sleep(5); } else { printf("fork\n"); return 1; } } return 0; }