当前位置:网站首页>IPC:Interrupts and Signals
IPC:Interrupts and Signals
2022-08-10 15:04:00 【51CTO】
In this section will look at ways in which two processes can communicate. When a process terminates abnormally it usually tries to send a signal indicating what went wrong. C programs (and UNIX) can trap these for diagnostics. Also user specified communication can take place in this way.
Signals are software generated interrupts that are sent to a process when a event happens. Signals can be synchronously generated by an error in an application, such as SIGFPE and SIGSEGV, but most signals are asynchronous. Signals can be posted to a process when the system detects a software event, such as a user entering an interrupt or stop or a kill request from another process. Signals can also be come directly from the OS kernel when a hardware event such as a bus error or an illegal instruction is encountered. The system defines a set of signals that can be posted to a process. Signal delivery is analogous to hardware interrupts in that a signal can be blocked from being delivered in the future. Most signals cause termination of the receiving process if no action is taken by the process in response to the signal. Some signals stop the receiving process and other signals can be ignored. Each signal has a default action which is one of the following:
- The signal is discarded after being received
- The process is terminated after the signal is received
- A core file is written, then the process is terminated
- Stop the process after the signal is received
Each signal defined by the system falls into one of five classes:
- Hardware conditions
- Software conditions
- Input/output notification
- Process control
- Resource control
Macros are defined in <signal.h>
These include:
SIGHUP 1 /* hangup */ | SIGINT 2 /* interrupt */ |
SIGQUIT 3 /* quit */ | SIGILL 4 /* illegal instruction */ |
SIGABRT 6 /* used by abort */ | SIGKILL 9 /* hard kill */ |
SIGALRM 14 /* alarm clock */ | |
SIGCONT 19 /* continue a stopped process */ | |
SIGCHLD 20 /* to parent on child stop or exit */ | |
Signals can be numbered from 0 to 31.
Sending Signals -- kill(), raise()
There are two common functions used to send signals
int kill(int pid, int signal) - a system call that send a signal to a process, pid. If pid is greater than zero, the signal is sent to the process whose process ID is equal to pid. If pid is 0, the signal is sent to all processes, except system processes.
kill() returns 0 for a successful call, -1 otherwise and sets errno
int raise(int sig) sends the signal sig to the executing program. raise() actually uses kill()
kill(getpid(), sig);
There is also a UNIX command called kill that can be used to send signals from the command line - see man
NOTE: that unless caught or ignored, the kill
Only processes with certain access privileges can be killed off.
Basic rule: only processes that have the same user can send/receive messages.
The SIGKILL
For examplekill(getpid(),SIGINT);
This would have a similar effect to exit() command. Also ctrl-c typed from the command sends a SIGINT
unsigned int alarm(unsigned int seconds) -- sends the signal SIGALRM
Signal Handling -- signal()
An application program can specify a function called a signal handler to be invoked when a specific signal is received. When a signal handler is invoked on receipt of a signal, it is said to catch the signal. A process can deal with a signal in one of the following ways:
- The process can let the default action happen
- The process can block the signal (some signals cannot be ignored)
- the process can catch the signal with a handler.
Signal handlers usually execute on the current stack of the process. This lets the signal handler return to the point that execution was interrupted in the process. This can be changed on a per-signal basis so that a signal handler executes on a special stack. If a process must resume in a different context than the interrupted one, it must restore the previous context itself
Receiving signals is straighforward with the function:
int (*signal(int sig, void (*func)()))() -- that is to say the function signal() will call the func functions if the process receives a signal sig. Signal returns a pointer to function func if successful or it returns an error to errno
func()
SIG_DFL
SID_DFL(), which will terminate the process upon receipt of sig. SIG_IGN
SIG_IGN() which will disregard the sig action ( UNLESS it is SIGKILL). A function address
-- a user specified function.
SIG_DFL and SIG_IGN are defined in signal.h
Thus to ignore a ctrl-c
signal(SIGINT, SIG_IGN);
TO reset system so that SIGINT
signal(SIGINT, SIG_DFL);
So lets write a program to trap a ctrl-c but not quit on this signal. We have a function sigproc() that is executed when we trap a ctrl-c. We will also set another function to quit the program if it traps the SIGQUIT
#include <stdio.h> void sigproc(void); void quitproc(void); main() { signal(SIGINT, sigproc); signal(SIGQUIT, quitproc); printf(``ctrl-c disabled use ctrl-
to quit
n'');
for(;;); /* infinite loop */} void sigproc() { signal(SIGINT, sigproc); /* */ /* NOTE some versions of UNIX will reset signal to default after each call. So for portability reset signal each time */ printf(``you have pressed ctrl-c
n'');
} void quitproc() { printf(``ctrl-
pressed to quit
n'');
exit(0); /* normal exit status */ }
sig_talk.c
Let us now write a program that communicates between child and parent processes using kill() and signal().
fork() creates the child process from the parent. The pid
The parent can then send messages to child using the pid and kill().
The child picks up these signals with signal()
An example of communicating process using signals is sig_talk.c:
/* sig_talk.c --- Example of how 2 processes can talk *//* to each other using kill() and signal() */ /* We will fork() 2 process and let the parent send a few */ /* signals to it`s child */ /* cc sig_talk.c -o sig_talk */ #include <stdio.h> #include <signal.h> void sighup(); /* routines child will call upon sigtrap */ void sigint(); void sigquit(); main() { int pid; /* get child process */ if ((pid = fork()) < 0) { perror("fork"); exit(1); } if (pid == 0) { /* child */ signal(SIGHUP,sighup); /* set function calls */ signal(SIGINT,sigint); signal(SIGQUIT, sigquit); for(;;); /* loop for ever */ } else /* parent */ { /* pid hold id of child */ printf("/nPARENT: sending SIGHUP/n/n"); kill(pid,SIGHUP); sleep(3); /* pause for 3 secs */ printf("/nPARENT: sending SIGINT/n/n"); kill(pid,SIGINT); sleep(3); /* pause for 3 secs */ printf("/nPARENT: sending SIGQUIT/n/n"); kill(pid,SIGQUIT); sleep(3); } } void sighup() { signal(SIGHUP,sighup); /* reset signal */ printf("CHILD: I have received a SIGHUP/n"); } void sigint() { signal(SIGINT,sigint); /* reset signal */ printf("CHILD: I have received a SIGINT/n"); } void sigquit() { printf("My DADDY has Killed me!!!/n"); exit(0); }
Other signal functions
There are a few other functions defined in signal.h:
int sighold(int sig) -- adds sig
int sigrelse(int sig) -- removes sig
int sigignore(int sig) -- sets the disposition of sig to SIG_IGN
int sigpause(int sig) -- removes sig
边栏推荐
- Community News——Congratulations to Dolphin Scheduling China User Group for 9 new "Community Administrators"
- 颜色空间
- SWIG tutorial "two"
- Zhaoqi Technology Innovation High-level Talent Entrepreneurship Competition Platform
- 机器学习总结(一)
- fatal error C1083 无法打开包括文件'io.h' No such file
- systemui状态栏添加新图标
- [Semantic Segmentation] DeepLab Series
- 第壹章模块大全之《re模块》
- “低代码”编程或将是软件开发的未来
猜你喜欢
It is reported that the original Meitu executive joined Weilai mobile phone, the top product may exceed 7,000 yuan
一个 ABAP 开发的新浪微博语义情感分析工具
富爸爸穷爸爸之读书笔记
Understanding_Data_Types_in_Go
Appium for APP automation testing
[Semantic Segmentation] DeepLab Series
E. Cross Swapping(并查集变形/好题)
机器学习总结(一)
Recommend a few had better use the MySQL open source client, collection!
Azure IoT 合作伙伴技术赋能工作坊:IoT Dev Hack
随机推荐
A test tool for ABAP Development Tool custom service endpoint
关于async\await 的理解和思考
fastposter v2.9.1 程序员必备海报生成器
Detailed understanding of anonymous functions and all built-in functions (Part 2)
电商秒杀项目收获(二)
宝塔面板开放Redis给指定外网机器
An ABAP tool that can print the browsing history of a user in the system for BSP applications
Mysql statement analysis, storage engine, index optimization, etc.
2022 CCF中国开源大会会议通知(第四轮)
NFT数字藏品——数字藏品发行平台开发
“蔚来杯“2022牛客暑期多校训练营7
NPM - Cannot read properties of null (reading 'pickAlgorithm') 解决方案
Introduction to the functional logic of metaForce Fosage 2.0 system development
Appium for APP automation testing
storage of data in memory
腾讯云TDP-对象存储COS产品新用户福利
Basic learning of XML
Oracle数据库备份dmp文件太大,有什么办法可以在备份的时候拆分成多个dmp吗?
使用 ABAP 正则表达式解析 uuid 的值
SYM32——RTC实时时钟程序讲解