当前位置:网站首页>< job search> process and signal
< job search> process and signal
2022-07-02 03:18:00 【Have love】
One 、 process
process : An address space in which one or more threads are running and the system resources required by the threads .
ps command :process status Check the status of the running process
PID:2-32768 1 Generally, it is a special process Init Retain ,init Processes are responsible for managing other processes .
1、 Create child process
Replacement process impression :
exec Series of functions : Switch the execution of a program from one program to another , After the new program starts , The original program is no longer executed .
Copy process impression :
fork function : Create a new process . The new process executes the same code as the original process , But it has its own data space 、 Environment and file descriptors .
Possible reasons for the failure of creating sub process :
(1) When the number of child processes owned by the parent process exceeds the specified limit (CHILD_MAX), It may cause the creation of child processes to fail .errno:EAGAIN
(2) There is not enough space in the process table to create new forms or there is not enough virtual memory .errno:ENOMEM
2、 Waiting process
(1)pid_t wait(int *stat_loc) Call... That returns the child process PID, If stat_loc It's not a null pointer , The status information of the child process will be written to the location it points to .
State information :
WIFEXITED(stat_val) If the subprocess ends normally , Return non 0 value
WEXITSTATUS(stat_val) If WIFEXITED Not 0, Return the exit code of the child process
WIFSIGNALED(stat_val) If the child process is terminated by an uncapped signal , Return non 0 value
WTERMSIG(stat_val) If WIFSIGNALED Return non 0 value , Returns a signal code
WIFSTOPED(stat_val) If the subprocess terminates unexpectedly , It returns non 0 value
WSTOPSIG(stat_val) If WIFSTOPED Not 0, It returns a signal code
(2)pid_t waitpid(pid_t pid, int *stat_loc, int option) Wait for the specified process
3、 Zombie process
After the child process terminates , it The connection with the parent process will remain , Until the parent process also terminates normally or the negative process calls wait To terminate . therefore , Entries representing child processes in the process table will not be released immediately , Its exit code needs to be saved for the parent process in the future wait Use . At this time, it will become a zombie process or a dead process .
If the parent process terminates abnormally ,PID by 1 Of init The process will be the parent process .
Two 、 I / O redirection
ex:
cat file.txt // aaaaaa
./upper < file.txt //AAAAAA
3、 ... and 、 The signal
A signal is an event generated by the system in response to certain conditions , The signal is generated by certain error conditions . If the executing process receives the signal but does not arrange to capture it in advance , The process will terminate .
Common signals :
SIGABORT Process aborted
SIGINT Terminal interrupt (ctrl+c)
SIGKILL Terminate the process ( This signal cannot be captured or ignored )
SIGSEGV Invalid memory segment access
SIGSTOP Stop executing
SIGCHLD The child process has stopped or exited , By default , It is ignored
SIGCONT Continue the suspended process
If you want to send a signal to the process , But this process is not the current foreground process , Need to use Kill Command sending signal
1、 Sending signal
int kill(pid_t pid, int sig);
pase();// Suspend program execution , Until a signal appears .
2、 Signal processing functions
(1)void (*signal(int sig, void (*func)(int)))
sig: Processed signals func: The function called after receiving the signal
signal The function returns the function pointer of the signal processing function .
Signal processing functions are also available SIG_IGV( Ignore the signal )、SIG_DFL( Restore default behavior )
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
void A(int sig)
{
printf("I got signal %d\n",sig);
(void) signal(SIGINT, SIGDFL);// Restore default behavior
}
int main()
{
(void) signal(SIGINT, A);
while(1)
{
printf("hh\n");
sleep(1);
}
}
Be careful , It is called in the signal processing function printf Functions are not safe .printf The internal implementation of needs to obtain the lock of the output first , Then modify the output , Finally, release the lock at the output . If the main program just gets the lock at the output , And the signal interrupts the main program , But the signal processing function is waiting for the main program to release the lock , This creates a deadlock .
(2) Robust signal interface
int sigaction(int sig, const struct sigaction *act, struct sigaction *oact);
sigaction Structure contains :
void (*) (int) sa_hander //function, SIG_IGN or SIG_DFL
sigset_t sa_mask // Signal set , Before calling the signal processing function , This signal set will be masked by the process
int sa_flags // The signal processing function is not reset by default , If it needs to be reset , It can be set to SA_RESETHAND
边栏推荐
- Load different fonts in QML
- Mmsegmentation series training and reasoning their own data set (3)
- 2022-2028 global soft capsule manufacturing machine industry research and trend analysis report
- el-table的render-header用法
- Detailed explanation of the difference between Verilog process assignment
- 自定义组件的 v-model
- 小米青年工程师,本来只是去打个酱油
- 2022-2028 global encryption software industry research and trend analysis report
- Baohong industry | 6 financial management models at different stages of life
- ORA-01547、ORA-01194、ORA-01110
猜你喜欢
GB/T-2423.xx 环境试验文件,整理包括了最新的文件里面
创业了...
[HCIA continuous update] overview of dynamic routing protocol
ZABBIX API creates hosts in batches according to the host information in Excel files
Verilog 过程赋值 区别 详解
West digital decided to raise the price of flash memory products immediately after the factory was polluted by materials
How to create an instance of the control defined in SAP ui5 XML view at runtime?
QT environment generates dump to solve abnormal crash
JIT deep analysis
Discrimination between sap Hana, s/4hana and SAP BTP
随机推荐
Retrofit's callback hell is really vulnerable in kotlin synergy mode
Gradle notes
Mongodb non relational database
MySQL connection query and subquery
Screenshot literacy tool download and use
/silicosis/geo/GSE184854_ scRNA-seq_ mouse_ lung_ ccr2/GSE184854_ RAW/GSM5598265_ matrix_ inflection_ demult
Cache processing scheme in high concurrency scenario
SAML2.0 notes (I)
About DNS
寻找重复数[抽象二分/快慢指针/二进制枚举]
Design details of SAP e-commerce cloud footernavigationcomponent
Verilog 时序控制
Unit · elementary C # learning notes
在QML中加载不同字体
Discussion on related configuration of thread pool
[HCIA continuous update] working principle of OSPF Protocol
Global and Chinese markets for electronic laryngoscope systems 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of X-ray detectors 2022-2028: Research Report on technology, participants, trends, market size and share
2022 hoisting machinery command examination paper and summary of hoisting machinery command examination
Use usedeferredvalue for asynchronous rendering