Linux system programming - process concept, process management, signal processing

1. Process knowledge points

The process in the operating system is the process of one-time execution of the program and the basic unit of dynamic execution of the operating system; Whenever a new process is created, the operating system will assign a unique identifier to the new process to facilitate subsequent management of the process.

The concept of process has two main points:

First, a process is an entity. Each process has its own virtual address space, including text area, data area, and stack area. The text area stores the code executed by the processor; The data area stores variables and dynamically allocated memory; The stack area stores the instructions and local variables called by the active process.

Second, process is an "executing program", which is essentially different from program. The program is static, which is an ordered set of instructions stored on disk; The process is a dynamic concept. It is a running program, including the process of dynamic creation, scheduling and extinction of the process. It is the basic scheduling unit of Linux. Only when the processor gives life to a program can it become an active entity called a process.

On the command line of Linux, you can use ps command to view the detailed information of the process running in the background.

2. View the internal space layout of the executable

The process is dynamic (in memory) and the program is static_ a. Out (on hard disk).
Under Linux, all processes run in the virtual address space - MMU. The space of each process is independent (physical address).

[wbyq@wbyq linux_c]$ gcc app.c 
[wbyq@wbyq linux_c]$ ls
a.out  app.c  shell.sh
[wbyq@wbyq linux_c]$ ./a.out 
[wbyq@wbyq linux_c]$ size a.out 
   text    data     bss     dec     hex filename
    960     248       8    1216     4c0 a.out
text :Text segment. The size of the logical code stored in the program. if while ....
data :Data segment. The size of global and static variables that have been initialized in the program.
bss  :BSS paragraph.  The size of uninitialized global and static variables in the program.
dec  :Total program size in decimal format
hex  :Total program size in hexadecimal format
filename :Name of the file

3. Process foreground and background switching mode

[wbyq@wbyq linux_c]$ ./a.out &     Background operation
[1] 14705  
[wbyq@wbyq linux_c]$ jobs     View processes running in the background
[1]+  Running                 ./a.out &
[wbyq@wbyq linux_c]$ fg 1      Switch the background process to the foreground
./a.out
^Z
[1]+  Stopped                 ./a.out
[wbyq@wbyq linux_c]$ jobs    
[1]+  Stopped                 ./a.out
[wbyq@wbyq linux_c]$ bg 1     Change the process stopped in the background to the execution state
[1]+ ./a.out &
[wbyq@wbyq linux_c]$ jobs   
[1]+  Running                 ./a.out &
[wbyq@wbyq linux_c]$ fg 1
./a.out

4. kill Command

4.1 check legal signals

The Kill Command sends a signal to the process What legal signals can the current system send? By kill -l

[wbyq@wbyq linux_c]$ kill -l
 1) SIGHUP   2) SIGINT   3) SIGQUIT  4) SIGILL   5) SIGTRAP
 6) SIGABRT  7) SIGBUS   8) SIGFPE   9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT   17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG  24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM   27) SIGPROF 28) SIGWINCH    29) SIGIO   30) SIGPWR
31) SIGSYS  34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3
38) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7
58) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX    

4.2 how to send signals?

kill [-s signal|-p] [--] pid...
kill -l [signal]

usage:
kill -s <signal> <process PID>
kill -signal <process PID>

[wbyq@wbyq linux_c]$ ps
  PID TTY          TIME CMD
 9877 pts/0    00:00:01 bash
14983 pts/0    00:00:01 a.out
14984 pts/0    00:00:00 ps
[wbyq@wbyq linux_c]$ kill -s 2 14983

4.3 how does the C language program capture signals?

#include <stdio.h>
#include <signal.h>
#include <stdlib.h>

void sighandler(int sig)
{
    printf("sig=%d\n",sig);
    exit(0);
}

int main()
{
    signal(SIGINT,sighandler);  
    while(1)
    {

    }
    return 0;
}

5. ps command

[wbyq@wbyq linux_c]$ ps -aux
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 2880 1432 ? Ss Aug16 0:06 /sbin/init
root 2 0.0 0.0 0 0 ? S Aug16 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S Aug16 0:03 [migration/0]
root 4 0.0 0.0 0 0 ? S Aug16 0:18 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S Aug16 0:00 [migration/0]
root 6 0.0 0.0 0 0 ? S Aug16 0:07 [watchdog/0]
root 7 0.0 0.0 0 0 ? S Aug16 0:05 [migration/1]
root 8 0.0 0.0 0 0 ? S Aug16 0:00 [migration/1]
root 9 0.0 0.0 0 0 ? S Aug16 0:20 [ksoftirqd/1]
root 10 0.0 0.0 0 0 ? S Aug16 0:14 [watchdog/1]
root 11 0.0 0.0 0 0 ? S Aug16 0:46 [events/0]
root 12 0.0 0.0 0 0 ? S Aug16 1:18 [events/1]
USER:  The name of the user account that started the process
PID:  Of the process ID Number, which is unique in the current system
%CPU:  CPU Percentage occupied
%MEM:  Percentage of memory used
VSZ:  Occupy virtual memory( swap Size of (space)
RSS:  Size of resident memory (physical memory) occupied
TTY:  The terminal on which the process runs. “? ” Table unknown or terminal not required
STAT:  Displays the current status of the process.
D Non interruptible sleep. Usually IO. 
R function. Running or waiting in the run queue.
S Dormancy. Waiting for an event, signal.
T stop it. Process received message SIGSTOP, SIGSTP, SIGTIN, SIGTOU Signal.
X The dead process should not appear.
Z Dead process.
It will usually be followed by the following letters to indicate more detailed status.
< High priority
N Low priority
L have pages In memory locked. For real-time or custom IO. 
s Process leader, who has child processes.
l Multithreading
+ Located in the foreground process group.
START:  The time the process was started.
TIME:  Total used by the process CPU time
COMMAND:  The name of the command that started the process

Keywords: Linux Operation & Maintenance server

Added by Steffen on Mon, 14 Feb 2022 16:35:04 +0200