Home > Hosting > Server

Can linux create multiple processes

2023-02-04 11:10:36

<p style="text-align: left;">Most people don’t quite understand the knowledge points of this article on whether linux can create multiple processes, so the editor summarizes the following content for you. The content is detailed, the steps are clear, and it has certain reference value. I hope you can read it. This article can be rewarding, let&#39;s take a look at this article on whether linux can create multiple processes.</p><p><br/></p><blockquote><p style="text-align: left;">Linux can create multiple processes. Linux supports multiple processes and can handle multiple tasks at the same time to maximize the use of system resources. Communication methods between linux processes: 1. Use unnamed pipes; 2. Use well-known pipes (FIFO); 3. Use signal single; 4. Use shared memory; 5. Use message queues; 6. Use semaphores.</p></blockquote><p style="text-align: left;"><strong>Linux can create multiple processes. </strong></p><p style="text-align: left;">Linux supports multiple processes. One of the benefits of a multi-process system is that it can handle multiple tasks at the same time to maximize the use of system resources.</p><h3 style="text-align: left;"><strong>Chapter 1 Introduction to Linux Multi-Process</strong></h3><p style="text-align: left;"><strong>1.1 Overview</strong></p><h4 style="text-align: left;">1.1.1 Process Concept&lt; /h4&gt;<p>In linux, a running program is called a process. <br/><strong>Program</strong>: static concept, it is a compiled binary file<br/><strong>Process</strong>: dynamic concept, when the program is running, the system will Automatically run a corresponding process<br/> The process includes three parts: <strong>process control block (PCB), code segment, and data segment</strong><br/> Process control block: a structure is used in linux To represent, record the status information of the process<br/><strong>Zombie process</strong>: the parent process exits before the child process<br/> If you create a child process, but the child process is not recycled in the parent process resources of the process, then the child process will become a zombie process, and the zombie process will eventually be recycled by a process called INIT in the system. <br/><strong>The init process (process No. 1) is the first process that runs when the system starts, and is the ancestor process of all processes. </strong></p></h4><h4 style="text-align: left;">1.1.2 Process view shell command</h4><ul class=" list-paddingleft-2"><li><p style="text-align: left;"><strong>top</strong> View dynamic process information</p></li><li><p style="text-align: left;"><strong>ps -ef</strong> View process details</p></li><li><p style="text-align: left;"><strong>pstree</strong> Display process information in a tree format</p></li><li><p style="text-align: left;"><strong>bg</strong> Put the suspended process Run in the background</p></li></ul><p style="text-align: left;"><strong>1.2 Process running status</strong></p><h4 style="text-align: left;">1.2.1 Running status</h4><ul class=" list-paddingleft-2"><li><p style="text-align: left;">Execution state (RUNNING): The process is occupying the CPU.</p></li><li><p style="text-align: left;">Ready state (RUNNING): The process is in the waiting queue waiting for scheduling.</p></li><li><p style="text-align: left;">Light sleep (INTERRUPTABLE): At this time, the process is waiting for the occurrence of an event or some kind of system resource, and can respond to the signal.</p></li><li><p style="text-align: left;">Deep sleep (UNINTERRUPTABLE): At this time, the process is waiting for an event to occur or some kind of system resource, and cannot respond to the signal.</p></li><li><p style="text-align: left;">Stop state (STOPPED): The process is suspended at this time.</p></li><li><p style="text-align: left;">Zombie state (ZOMBIE): At this time, the process cannot be scheduled, but the PCB has not been released.</p></li><li><p style="text-align: left;">DEAD: This is a terminated process and the PCB will be released</p></li></ul><h4 style="text-align: left;">1.2.2 User state/kernel state</h4><p style="text-align: left;">Kernel state: also called <strong>kernel space</strong>, which is the area where kernel processes/threads are located. Mainly responsible for operating system and hardware interaction. <br/> User Mode: Also called <strong>User Space</strong>, it is the area where user processes/threads are located. Mainly used to execute user programs.</p><p><img src="//img.freeonlinedomain.com/uploads/allimg/20230205/1-230205115030252.jpg" title="" alt="1_1.jpg"/></p><p style="text-align: left;"><strong>1. Difference</strong><br/> Kernel mode: The running code is not subject to any restrictions, and the CPU can execute any instruction. <br/>User mode: <strong>CPU cannot be scheduled, and hardware cannot be accessed directly</strong>. The running code needs to be checked by the CPU a lot, and cannot directly access kernel data and programs, that is, it cannot access any valid address like a kernel-mode thread.</p><p style="text-align: left;">When the operating system executes user programs, it mainly works in the user state, and only switches to the kernel state when it performs tasks that it does not have permission to complete.</p><p style="text-align: left;"><strong>2. Distinguish between user mode and kernel mode reasons</strong></p><ul class=" list-paddingleft-2"><li><p style="text-align: left;">Protection mechanism to prevent user processes from misoperation or malicious damage to the system&lt; /p&gt;</p></li><li><p style="text-align: left;">Ensure the centralized management of resources and reduce resource conflicts.</p></li></ul><p style="text-align: left;"><strong>3. Switch from user mode to kernel mode</strong><br/><strong>(1) System call (active)</strong> <br/>The system call (system call) is the interface provided by the operating system to the user process to request the operating system to perform some privileged operations, that is, the window that provides services for the user process. Under Linux, you can use the man syscalls command to view all system call API interfaces provided by Linux. <br/>Because the user mode cannot complete certain tasks, the user mode will request to switch to the kernel mode, and the kernel mode completes the switch through interrupts specially opened for users.</p><p style="text-align: left;"><strong>(2) Peripheral device interrupt (passive)</strong><br/> The peripheral device sends out an interrupt signal. When the interrupt occurs, the currently running process is suspended, and the operating system kernel For interrupt process processing, if the CPU executes the user mode program before the interrupt, it is equivalent to switching from the user mode to the kernel mode. <br/><em>Interrupts are used to ensure that CPU control is handed over to the operating system, so that the operating system can perform certain operations. </em></p><p style="text-align: left;"><strong>(3) Abnormal (passive)</strong><br/> Some unknown exceptions occur when executing user programs, which will switch from user programs to the kernel The program that handles the exception is switched to the kernel state.</p><p style="text-align: left;"><strong>1.3 Process interface function</strong></p><h4 style="text-align: left;">1.3.1 Process creation fork/vfork</h4><p style="text-align: left;">1, fork(), vfork()&lt; br/&gt; (1) The newly created process is called a child process, which copies all the resources of the parent process (only once at the time of creation, <strong>the value of the global variable will be different in the future</strong>), the parent and child process who Who comes first is uncertain.</p><p style="text-align: left;"><br/></p><p style="text-align: left;"><code>#include<unistd.h>pid_t fork(void); Return value: &gt; 0 means it is in the parent process. The return value at this time is **child process process id number** == 0 means in the process of the child &lt; 0 Error creating process &nbsp; &nbsp; &nbsp; &nbsp; #include<sys types.h="">#include<unistd.h>pid_t vfork(void);</unistd.h></sys></unistd.h></code></p><p><br/></p><p style="text-align: left;">(2)**vfork()**The child process shares all the resources of the parent process, <strong>It must be the child process first run</strong>, and then the parent process runs (even if you add sleep() to artificially interfere, it is useless)<br/> (3) Note<br/>The use of exit() in the child process is completely different from the result of not using it Not the same<br/>Whether sleep() is used in the parent-child process to give up the cpu time slice is also different<br/>Whether wait() is used in the parent process, the result of waitpid() is also different</p><p style="text-align: left;">&lt; p&gt;</p><p style="text-align: left;">(4) Process switching execution<br/></p><p><br/></p><h4 style="text-align: left;">1.3.2 Process exit exit/_exit</h4><p style="text-align: left;">1 , exit(), _exit()</p><p style="text-align: left;"><br/></p><p style="text-align: left;"><code>#include<stdlib.h>void exit(int status); void _exit(int status); Parameters: Status ---&gt; Status when the process exits The status can be agreed by yourself in the actual writing of the program: For example: exit(2)----&quot;Open file error occurred &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,&nbsp; Exit(0)----&quot;Exit normally return: void the difference: Exit() will refresh the IO buffer when exiting, and then exit (responsible exit) _exit() Exit directly (irresponsible exit)</stdlib.h></code></p><p><br/></p><h4 style="text-align: left;">1.3.3 Wait for the child process to exit (the parent process recycles resources) wait/waitpid</h4><p style="text-align: left;">1. wait()</p><p style="text-align: left;"><br/></p><p style="text-align: left;"><code>#include<sys wait.h="">pid_t wait(int *stat_loc); Return value: the id number of the child process you recycled Parameters: stat_loc -- &quot;Save the status information when the child process exits (not just the return value) stat_loc not only saves the value when the exit exits, but also saves which signal the child process exits when it exits. What is the reason for the error.</sys></code></p><p><br/></p><p><br/></p><p style="text-align: left;">2, waitpid()</p><p style="text-align: left;"><br/></p><p style="text-align: left;"><code>pid_t waitpid(pid_t pid, int *stat_loc, int options); recycle child process/process group Parameters: pid ---- &quot;You specify the id of the child process to be recycled &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; back&nbsp; ==0 Wait for a child process in this process group to exit &gt; 0 Waiting for the process whose PID is pid stat_loc-----&quot;Store the exit status of the child process (can be NULL) options ----&quot;Generally set to 0 WNOHANG returns immediately when there are no child processes WUNTRACED returns immediately when a child process is suspended WCONTINUED returns immediately when a child process receives SIGCONT Return value: -1 Execution failed &gt; 0 success The return value is the PID of the recycled process 0 WNOHANG is specified, and there is no exited child process</code></p><p><br/></p><h4 style="text-align: left;">1.3.4 Get process id–getpid</h4><p style="text-align: left;"><br/></p><p style="text-align: left;"><code>( 1) Get your own id getpid() #include<unistd.h>pid_t getpid(void); Return value: the id number of the process (2) Get the parent process id getppid() #include<unistd.h>pid_t getppid(void); Return value: the id number of the parent process</unistd.h></unistd.h></code></p><p><br/></p><h3 style="text-align: left;"><strong>Chapter 2 Linux multi-process communication method</strong></h3><p style="text-align: left;">Whether it is communication between processes or communication between threads. It is nothing more than to solve a problem: the allocation of shared resources (coordinating the access of different processes/threads to shared resources)</p><p style="text-align: left;"><strong>2.1 Communication methods between processes</strong></p><p style="text-align: left;">1. Traditional interprocess communication method</p><ul class=" list-paddingleft-2"><li><p style="text-align: left;">Unnamed pipe</p></li><li><p style="text-align: left;">Famous pipe</p></li><li><p style="text-align: left;">Signal</p></li></ul><p style="text-align: left;">2, System V IPC object</p><ul class=" list-paddingleft-2"><li><p style="text-align: left;">Shared memory</p></li><li><p style="text-align: left;">Message queue</p></li><li><p style="text-align: left;">Semaphore</p></li></ul><p style="text-align: left;">3, BSD&lt; /p&gt;</p><ol class=" list-paddingleft-2"><li><p style="text-align: left;">Network socket (socket)</p></li></ol><h4 style="text-align: left;">2.1.1 Anonymous pipe</h4><p><br/></p><p style="text-align: left;">1. Features : The most primitive way of communication between processes<br/>It can only communicate between processes with <strong>affinity</strong> (parent-child process, sibling process);<br/>It has no name (it exists );<br/> Can be created in the sharing between linux and windows (there is no pipeline file generated at all), but the well-known pipe cannot be created (generated pipeline file);<br/>Half-duplex communication.</p><p style="text-align: left;">2. Use of unnamed pipes<br/> (1) Create pipe()</p><p style="text-align: left;"><br/></p><p style="text-align: left;"><code>#include<unistd.h>int pipe(int fildes[2]); Parameters: fildes[2] contains two file descriptors fildes[0], fildes[1] fildes[0] read end &nbsp; fildes[1] write end Return value: return 0 on success, return -1 on failure</unistd.h></code></p><p><br/></p><p style="text-align: left;">(2) send and receive pipe information</p><p style="text-align: left;"><br/></p><p style="text-align: left;"><code>myid = fork(); //Create child process if(myid == 0) { write(fd[1],&quot;dad,thanks!&quot;,20); //The child process sends a message to the parent process close(fd[1]); close(fd[0]); exit(0); } else if(myid &gt; 0) { read(fd[0],buf,20); //The parent process is blocked to receive messages from the child process printf(&quot;buf is:%sn&quot;,buf); close(fd[1]); close(fd[0]); }</code></p><p><br/></p><h4 style="text-align: left;">2.1.2 Famous pipe (FIFO)</h4><p style="text-align: left;">1. Features: between any two processes</p><ul class=" list-paddingleft-2"><li><p style="text-align: left;">Cannot be created in the share between linux and windows;</p></li><li><p style="text-align: left;">Guarantee the atomicity of writing (atomicity: either do not do it, do it once The tone is finished without external interference);</p></li><li><p style="text-align: left;">The well-known pipeline<strong>cannot be overridden and created</strong> (general generationUse the access() function in the code to determine whether it exists, if there is already a pipeline with the same name, it cannot be created again);</p></li><li><p style="text-align: left;">Remember to close it after use;</p></li><li><p style="text-align: left;">When the pipe is opened as <strong>read-only</strong>, it will block until another process opens the pipe as <strong>write-only</strong>, then It is not blocked; if it is opened in a readable and writable manner, it will not be blocked.</p></li><li><p style="text-align: left;">Full-duplex communication, half-duplex.</p></li></ul><p style="text-align: left;">2. Use of famous pipes<br/> (1) Create mkfifo()</p><p style="text-align: left;"><br/></p><p style="text-align: left;"><code>#include<sys types.h="">#include<sys stat.h="">int mkfifo(const char *pathname, mode_t mode); Parameters: pathname The path name of the named pipe mode: permission 0666 Return value: 0 success -1 Failed</sys></sys></code></p><p><br/></p><p style="text-align: left;">(2) FIFO process information sending and receiving</p><p style="text-align: left;"><br/></p><p style="text-align: left;"><code>fifo_read.c :------- ---》 #define FIFO1 &quot;myfifo1&quot; #define FIFO2 &quot;myfifo2&quot; int main(void) { int my_fd,fd1,fd2; char r_buff[30]; char w_buff[30]; bzero(r_buff,30); if(access(FIFO1,F_OK)==-1) { my_fd = mkfifo(FIFO1,0664); //create pipeline 1 if(my_fd == -1) { perror(&quot;failed!n&quot;); return -1; } } if(access(FIFO2,F_OK)==-1) { my_fd = mkfifo(FIFO2,0664); //create pipeline 2 if(my_fd == -1) { perror(&quot;failed!n&quot;); return -1; } } fd1 = open(FIFO1,O_RDONLY); //Open pipe 1 for read-only, get the pipe file descriptor if(fd1==-1) { the pricentf(&quot;open fifo1 file failed!n&quot;); exit(0); } fd2 = open(FIFO2,O_WRONLY); //Open pipe 2 for writing only, get the pipe file descriptor if(fd2==-1) { printf(&quot;open fifo2 file failed!n&quot;); exit(0); } while(1) { bzero(r_buff,30); read(fd1,r_buff,sizeof(r_buff)); //Read the message of pipeline 1 printf(&quot;client receive message is: %sn&quot;,r_buff); printf(&quot;client please input a message!n&quot;); fgets(w_buff,30,stdin); write(fd2,w_buff,30); //Send information to pipeline 2 } close(fd2); close(fd1); return 0; } fifo_write.c :-----------》 #define FIFO1 &quot;myfifo1&quot; #define FIFO2 &quot;myfifo2&quot; int main(void) { int my_fd,fd1,fd2; char w_buff[30]; char r_buff[30]; bzero(w_buff,30); if(access(FIFO1,F_OK)==-1) { my_fd = mkfifo(FIFO1,0664); if(my_fd == -1) { perror(&quot;failed!n&quot;); return -1; } } if(access(FIFO2,F_OK)==-1) { my_fd = mkfifo(FIFO2,0664); if(my_fd == -1) { perror(&quot;failed!n&quot;); return -1; } } fd1 = open(FIFO1,O_WRONLY); if(fd1==-1) { printf(&quot;open fifo1 file failed!n&quot;); exit(0); } fd2 = open(FIFO2,O_RDONLY);if(fd2==-1) { printf(&quot;open fifo2 file failed!n&quot;); exit(0); } while(1) { bzero(w_buff,30); printf(&quot;server please input a message!n&quot;); fgets(w_buff,30,stdin); write(fd1,w_buff,strlen(w_buff)); //write message to pipeline 1 file read(fd2,r_buff,30); //Read information from pipeline 2 printf(&quot;server receive message is:%sn&quot;,r_buff); } close(fd1); close(fd2); return 0; }</code></p><p><br/></p><h4 style="text-align: left;">2.1.3 Signal single</h4><p style="text-align: left;">When the program (process) is running, the outside world will send a signal to the program from time to time. The program is faced with two choices: <br/> Ignore it (blocking/ignoring)<br/> Blocking: refers to suspending the signal and waiting until the program is finished running before responding<br/> Ignoring: discarding the signal<br/> /&gt; Respond to it</p><p style="text-align: left;">1. What are the signals in linux: kill -l view</p><p style="text-align: left;"><br/></p><p style="text-align: left;"><code>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</code></p><p><br/></p><p style="text-align: left;">(1) Signals 1 to 31 are called non-real-time signals: queues are not supported (if multiple signals come at the same time, the response Irregular)<br/> (2) User-defined signals 10) SIGUSR1 12) SIGUSR2 <br/> (3) Signals 34 to 64 are called real-time signals: support queues, which are added later in the Linux system <br/> Signals are similar to interrupts: hardware and software<br/><strong>There are two special signals above: SIGKILL, SIGSTOP cannot be ignored, nor can they be blocked</strong></p><p style="text-align: left;">2 , Signal-related operation functions<br/> (1) Send signal kill()</p><p style="text-align: left;"><br/></p><p style="text-align: left;"><code>#include<signal.h>int kill(pid_t pid, int sig); Parameters: pid ----&quot;process id Positive number: the process number of the process to receive the signal 0: The signal is sent to all processes in the same process group as the pid process -1: The signal is sent to all processes in the process table (except the process with the largest process number) sig ----&quot;signal name Return value: 0 success -1 Error</signal.h></code></p><p><br/></p><p style="text-align: left;">(2) Signal capture signal()</p><p style="text-align: left;"><br/></p><p style="text-align: left;"><code>#include<signal.h>void (*signal(int sig, void (*func)(int)))(int); //SIGKILL Parameters: sig ---- &quot;The signal you need to capture void (*func)(int) ----&quot;Function pointer, callback function, call the function when the corresponding signal is captured; the second parameter can pass a function pointer, you can also use the following two macros definition: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SIG_IGN ----&gt; The signal you caught will be ignored &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SIG_DFL----- &quot;The signal you capture will respond in the system&#39;s default way Return value: success: set the previous signal processing method Error: -1</signal.h></code></p><p><br/></p><p style="text-align: left;">(3) Waiting for signal pause()</p><p style="text-align: left;"><br/></p><p style="text-align: left;"><code>#include<unistd.h>int pause(void); Return value: -1 Set the error value to EINTR 0 Success</unistd.h></code></p><p><br/></p><p style="text-align: left;">(4) Signal blocking<br/> Each process has its own signal mask (that is, the process is running The signals that will be blocked are called signal masks). <br/>A series of functions about signal mask operation:</p><p style="text-align: left;"><br/></p><p style="text-align: left;"><code>#include<signal.h>int sigemptyset(sigset_t *set): Clear signal mask &nbsp; int sigfillset(sigset_t *set): Add all signals to the signal mask &nbsp; int sigaddset(sigset_t *set, int signum): add a specific signal to the signal mask int sigdelset(sigset_t *set, int signum): delete a specific signal from the mask int sigismember(const sigset_t *set, int signum): determine if a signal is in the mask Parameters: sigset_t ---- &quot;store the signal blocked by the process</signal.h></code></p><p><br/></p><p style="text-align: left;">(5) Configure the signal mask sigprocmask()—block or unblock the signal</p><p style="text-align: left;">&lt; p&gt;</p><p style="text-align: left;"><code>#include<signal.h>int sigprocmask(int how, const sigset_t *restrict set, sigset_t *restrictoset) Parameters: How --- &quot;SIG_BLOCK Add the signal contained in the set to the original signal mask &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SIG_SETMASK Use set to replace the original signal mask &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SIG_UNBLOCK removes the signal contained in the set from the original mask set ---&gt; new signal mask &nbsp; oset --- &quot;Original signal mask The signal mask in the original process includes: SIGINT , SIGCONT</signal.h></code></p><p><br/></p><p style="text-align: left;">(6) Capture the specified signal and obtain the signal carrying information sigaction()</p><p style="text-align: left;"><br/></p><p style="text-align: left;"><code>#include<signal.h>int sigaction(</signal.h></code></p><p style="text-align: left;"><code><signal.h>int sig, const struct sigaction *restrict act, struct sigaction *restrict oact); parameter:&nbsp;</signal.h></code></p><p style="text-align: left;"><code><signal.h>sig --- &quot;The signal you want to capture&nbsp;</signal.h></code></p><p style="text-align: left;"><code><signal.h>Act---&quot;The response function corresponding to the signal you need to capture is defined in this structure&nbsp;</signal.h></code></p><p style="text-align: left;"><code><signal.h>oact --- &quot;Original&nbsp;</signal.h></code></p><p style="text-align: left;"><code><signal.h>struct sigaction { void(*) (int) sa_handler ----&quot; signal response function &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</signal.h></code></p><p style="text-align: left;"><code><signal.h>sa_mask --- &quot;the mask of the signal int sa_flags ----&quot; SA_SIGINFO &nbsp;&nbsp; void(*) (int, siginfo_t * , void )---&quot;signal response function } sa_flags ---&quot; equal to SA_SIGINFO, then the response function of the signal is void(*) (int, siginfo_t * , void ) If not equal, then the response function of the signal is void(*) (int)&nbsp;</signal.h></code></p><p style="text-align: left;"><code><signal.h>siginfo_t---&quot;/usr/include/i386-linux-gnu/bits/siginfo.h saves the status information of the signal, the label of the signal, the id of the process that sent the signal, etc.</signal.h></code></p><p><br/></p>


Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us