#Linux中的GCC规划# Process-related knowledge

Based on C, further study linux kernel functions, system-level functions

August 2017

1. Knowledge

1.1 Basic concepts:

(1) Program
is an executable file stored on a disk.
(2) Process
is the execution instance of the program. Each process has independent permissions and responsibilities and runs in its own virtual address space.
The processes will not affect each other, but they can communicate.
(3) Process ID, process ID referred to as PID is a
non-negative integer, the digital identifier of the process.
Process task queue

1.2 Startup routine

(1) The "startup routine" is compiled and run before main.
(2) Collect command line parameters and pass them to argc and argv in main. And the environment table envp.
(3) Register the "termination function" atexit().

1.3 Termination of the process

(1) Normal termination

  • Return from the main function (return)
  • Call exit (standard C library function)
  • Call _exit or _Exit (system call)
  • The last thread returns from the "start routine"
  • The last thread calls pthread_exit

note:
return and exit() will refresh the standard IO cache and automatically call the termination function.
_exit() and _Exit(), neither refresh nor call.

(2) Abnormal termination

  • Call abort
  • Receive a signal and terminate
  • The last thread responds to the "cancel" request

(3) Process return

  • Usually the program returns 0 successfully, otherwise it returns non-zero
  • In the shell, you can produce the process return value (echo $?)

(4) Termination function

#include <stdio.h>
int atexit(void (*function)(void));
向内核登记终止函数,成功返回0,否则-1
  • Each started process registers a standard termination function by default
  • The termination function releases some resources occupied by the process when the process terminates
  • Register multiple termination functions, and the execution sequence is executed in a stack, first registering and then executing.

1.4 Schematic

Startup diagram

1.5 Process resource limit

The resources available in linux are as follows:

RLIMIT_AS 			进程可用的存储区大小
RLIMIT_CORE 		core文件最大字节数
RLIMIT_CPU 			CPU时间最大值
RLIMIT_DATA			数据段最大长度
RLIMIT_FSIZE		可创建文件的最大长度
RLIMIT_LOCKS		文件锁的最大数
RLIMIT_MEMLOCK使用mlock能否在存储器中锁定的最长字节数
RLIMIT_NOFILE		能打开的最大文件数
RLIMIT_NPROC		每个用户ID可拥有的最大子进程数
RLIMIT_RSS			最大驻内存集的字节长度
RLIMIT_STACK		栈的最大长度
  1. head File
#include <sys/resource.h>

struct rlimit{
	rlim_t rlim_cur;/*软件限制:当前限制*/
	rlim_t rlim_max;/*硬件限制:当前限制可以达到的最大值*/
}
  1. function
(1)获取进程的资源限制,存放在rlptr指向 的结构体中。成功返回0,失败非0。
int getrlimit(int resource , struct rlimit *rlptr);

(2)修改resource指定的资源限制,通过rlptr指向的结构体。成功返回0
int setlimit(int resource,const struct rlimie *rlptr);
  1. Configuration file
    (1) /etc/security/limits.conf
    (2) In Linux, the initialization of process resources is established by process 0 and inherited by subsequent processes.
  2. Modification rules for resource limits
    (1) The hardware resource limit must be greater than or equal to the software limit.
    (2) Any process can reduce or increase its software resource limit, but it must be greater than its software limit. Ordinary users cannot reverse this operation.
    (3) The super user can increase the hardware limit.

2. Instructions related to the process

2.1 PS instruction

You can view: process ID (PID), process user ID, process status STAT, process command and so on.

3. Common status of the process

3.1 Operating status

The current process of the system
Ready state process
PS command stat column == R

3.2 Waiting state

Waiting for the event to occur
Waiting for
the stat column of the system resource PS command == S

3.3 Stop state

The stat column of the PS command == T

3.4 Zombie status

The process terminates or ends
. There is still
a stat column that records the PS command in the process table entry == Z

3.5 Transformation relationship of process status

Process state transformation relationship

4. Process scheduling

4.1 General steps

(1) Process the work in the kernel
(2) Process the current process
(3) Select the process (real-time process and ordinary process)
(4) Process exchange

4.2 Scheduling information in task_struct

(1) Strategy

  • Rotation strategy
  • First in first out strategy

(2) Priority

  • jiffies variable

(3) Real-time priority

  • Between real-time processes

(4) Counter

5. Process ID

Processes have many identifiers:
current process ID, actual user ID, effective user ID, user group ID, parent process ID, process group ID.
Functions related to this:

  1. head File
#include <unistd.h>
#include <sys/types.h>
  1. Function to get process ID
pid_t getpid(void);           //获取当前进程的ID标识
pid_t getppid(void);         //获取父进程的ID标识
pid_t getpgrp(void);        //获取当前进程所在的进程组ID标识。
pid_t getpgid(pid_t pid);  //获取指定ID的进程所在的进程组ID标识。

uid_t getuid(void);           //获取当前进程的 实际用户ID
uid_t geteuid(void);          //获取当前进程的 有效用户ID

gid_t getgid(void);           //获取当前进程的用户组ID

6. Process creation

The key content of this article.

6.1 Function fork to create a child process


  1. The new process created by the fork function fork is called a child process. This function is called once and returns twice.
    The difference between returning twice:
    (1) In the parent process, the process ID of the new child process is returned.
    (2) In the new child process, 0 is returned. Because the data segment, heap, and stack of the child process are recreated.
    (3) The running sequence of parent and child processes is automatically determined according to system scheduling.
    (4) The child process copies the memory space of the parent process.

  2. The vfork function
    is similar to fork, but the child process runs first and does not copy the memory space of the parent process.

  3. Inherited attributes of child processes

  • User information and permissions
  • Directory information, signal information, environment, resource restrictions
  • Shared memory segment, heap, stack and data segment, shared code segment
  1. Unique attributes of child processes
  • Process ID
  • Lock information
  • operation hours
  • Pending signal
  1. Kernel structure changes when operating files
  • The child process inherits the file description table, but does not inherit but shares file table entries and i-nodes.
  • After a child process is created, the reference counter in the file table entry increases by 1 to become 2. When the parent process performs a close operation, the counter decreases by 1. The child process can still use file table entries. Only when the counter is 0, the file entry will be released.

6.2 Process parasitic exec function cluster

  • The exec function is used to execute another program. The newly executed program will replace the text, data, heap, and stack of the original process.
  • exec does not create a new process, the process ID before and after it has not changed.
  • After fork creates a child process, you can use the exec function to execute another program in the child process.
  1. head File
#include <unistd.h>

2. Function

//list 列出每个字符参数
int execl(const char *pathname,const char *arg0, ... /*(char*)0*/);
//argv 字符数组
int execv(const char *pathname,char * const argv[]);
//list 列出每个字符参数,环境表
int execle(const char *pathname,const char *arg0, ... /*(char*)0,char* const envp[]*/);
//argv 字符数组,环境表
int execve(const char *pathname,char * const argv[],char* const envp[]);
//
int execlp(const char *pathname,const char *arg0, ... /*(char*)0*/);
//
int execvp(const char *pathname,char * const argv[]);

上述所有的返回:出错返回-1 ,成功不返回。

6.3 system function

  • The system function is an internal component of a child process, and the exec function is called by the child process.
  1. head File
#include <stdlib.h>

2. Function

简化exec函数的使用,成功返回执行命令的状态,错误返回-1
int system(const char * command);

7. Several special processes

7.1 Daemon

  • A daemon is a process with a long lifetime. They often start when the system boots and terminate when the system shuts down.
  • All daemons run with the priority of super user (user ID is 0).
  • The daemon does not control the terminal.
  • The parent process of the daemon is the init process.

7.2 Orphan Process

  • When the parent process ends, the child process becomes an orphan process. Adopted by process 1 (init process).

7.3 Zombie Process

  • The child process ends, but the memory is not completely released (task_struct in the kernel is not released). This process becomes a zombie process.
  • When the parent process of the zombie process ends, it is adopted by the first process (init process) and finally recycled.
  • How to avoid the zombie process:
    (1) Let the parent process of the zombie process to recycle. The parent process checks whether the child process ends and recycles at regular intervals. Call wait() or waitpid() to notify the kernel to release the zombie process.
    (2) Use the signal SIGCHLD to notify the processing. And call wait() in the signal handler.
    (3) Let the zombie process become an orphan process and be recycled by the init process.
  1. head File
#include <sys/types.h>
#include <sys/wait.h>
  1. function
(1)等待子进程退出并回收。防止僵尸进程。成功返回子进程ID,出错返回-1。
pid_t wait(int *status);

(2)wait函数的非阻塞版本。成功返回子进程ID,出错返回-1。
pid_t waitpid(pid_t pid , int * status ,int options);

	1)pid参数。
			pid==-1   //等待任一子进程,功能和wait等效。
			pid== 0   //等待 同进程组ID的任一子进程。
			pid >  0   //等待指定的子进程
			pid < -1   //等待 组ID等于(-pid)的任一子进程。
	2)status参数。为空时,等待回收【任意状态】结束的子进程。不为空,则等待【指定状态】结束的子进程。
         检查wait和waitpid函数返回终止状态的宏
				WIFEXITED/WEXITSTATUS(status)    //若为正常终止子进程返回的状态,则为真。
				WIFSIGNALED/WTERMSIG(status)    //若为异常终止子进程返回的状态,则为真。(接到一个不能捕捉的信号)
				WIFSTOPED/WSTOPSIG(status)     //若为当前暂停子进程返回的状态,则为真。
	3)options参数。
				WNOHANG    //若由pid指定的子进程没有退出,则立即返回。waitpid不阻塞,返回值为0。
				WUNTRACED    //若某实现支持 作业控制,则有pid指定的任一子进程状态已暂停,其状态自暂停以来还没有报告过,则返回其状态。

  1. The difference between wait and waitpid functions
    (1) Before a child process terminates, wait blocks the caller. waitpid will not block.
    (2) wait waits for all child processes; waitpid waits for the specified child process.

8. Process Group

  • A collection of one or more processes
  • Can receive various signals from the same terminal. Common signals in the same group.
  • Unique process group ID
  • When all the processes in the group end, the process group will die.
  • The kill command sends a signal to the process group.

8.1 Get process group number

The previous article has given:

#include <unistd.h>
pid_t getpgrp(void);        //获取当前进程所在的进程组ID标识。
pid_t getpgid(pid_t pid);  //获取指定ID的进程所在的进程组ID标识。

8.2 Leader Process

  • The creation of a process group starts with the joining of the first process (group leader process). The ID of the leader process is used as the group ID.
  • The leader process can create a process group and the processes in the group.
  1. head File
#include <unistd.h>
  1. function
(1) 将进程加入到指定的进程组中。成功返回0,错误返回-1。
int setpgid(pid_t pid,pid_t pgid);

(2) pid参数为指定的进程号,pgid为进程组。

8.3 Foreground Process Group

  • In Linux, the group that automatically receives terminal signals becomes the foreground process group.
  • In the terminal, signals such as CTRL+c are first received by the foreground process group.
  • Several process groups started by the shell, the default parent process group is the foreground process group.
  1. head File
#include <unistd.h>
  1. function
(1)获取前台进程组ID。成功返回 前台进程组ID,错误返回-1。
pid_t tcgetpgrp(int fd); 

(2)使用pgrpid,设置前台进程组ID
int tcsetpgrp(int fd,pid_t pgrpid);

(3)fd必须引用该会话的控制终端。0表示当前正在使用的终端。

Guess you like

Origin blog.csdn.net/Kshine2017/article/details/102624873