当前位置:网站首页>< 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
边栏推荐
- 表单自定义校验规则
- 图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
- Spark Tuning
- [staff] restore mark (Introduction to the use of restore mark | example analysis of Metaphone mark and restore mark)
- Grpc快速实践
- KL divergence is a valuable article
- Calculation of page table size of level 2, level 3 and level 4 in protection mode (4k=4*2^10)
- ORA-01547、ORA-01194、ORA-01110
- Delphi xe10.4 installing alphacontrols15.12
- Just a few simple steps - start playing wechat applet
猜你喜欢

buu_ re_ crackMe

verilog 并行块实现
![[C Advanced] brother Peng takes you to play with strings and memory functions](/img/95/ab1bb0b3fa0b99e32233a5ca5d42a4.jpg)
[C Advanced] brother Peng takes you to play with strings and memory functions

How to develop digital collections? How to develop your own digital collections

Mongodb non relational database

Uniapp uses canvas to generate posters and save them locally

Verilog 避免 Latch

Continuous assignment of Verilog procedure

MSI announced that its motherboard products will cancel all paper accessories

图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
随机推荐
West digital decided to raise the price of flash memory products immediately after the factory was polluted by materials
PMP personal sprint preparation experience
寻找重复数[抽象二分/快慢指针/二进制枚举]
Gradle 笔记
4. Find the median of two positive arrays
Detailed explanation of the difference between Verilog process assignment
Named block Verilog
OSPF LSA message parsing (under update)
旋转框目标检测mmrotate v0.3.1 学习模型
Verilog parallel block implementation
Continuous assignment of Verilog procedure
Docker安装canal、mysql进行简单测试与实现redis和mysql缓存一致性
/silicosis/geo/GSE184854_ scRNA-seq_ mouse_ lung_ ccr2/GSE184854_ RAW/GSM5598265_ matrix_ inflection_ demult
Possible causes of runtime error
Intersection of Venn graph
Verilog 避免 Latch
Addition without addition, subtraction, multiplication and division (simple difficulty)
Global and Chinese markets for hand hygiene monitoring systems 2022-2028: Research Report on technology, participants, trends, market size and share
The capacity is upgraded again, and the new 256gb large capacity specification of Lexar rexa 2000x memory card is added
GSE104154_ scRNA-seq_ fibrotic MC_ bleomycin/normalized AM3