当前位置:网站首页>Small exercise of process and signal
Small exercise of process and signal
2022-06-30 14:06:00 【Fat Xiao Deng】
Create a subprocess ,
Subprocess interval 7s send out 2 A signal to the parent process ,
Parent process registration 2 A signal response function ,
Create a signal set , Add all signals to the signal set ,
Judge 2 Whether the signals are in the signal set , Set the signal in the signal set to block ,
Time delay 10s relieve 1 Blocking of signals ,
stay 5s Then unblock another signal ,
Signal response function Printing process pid And signal value
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
void func1(int sig)
{
printf("pid=%d, received %d The signal , Execute the response function 1\n",getpid(),sig);
}
void func2(int sig)
{
printf("pid=%d, received %d The signal , Execute the response function 2\n",getpid(),sig);
}
int main(int argc, char const *argv[])
{
pid_t pid=fork();
if (pid > 0)
{
printf(" The parent process pid=%d\n",getpid());
signal(10, func1);// Register response function
signal(12, func2);
sigset_t myset;
// sigemptyset(&myset); // Clear the signal set
sigaddset(&myset,10); // Add signal SIGUSR1 To signal set 10
sigaddset(&myset,12); // Add signal SIGUSR2 To signal set 12
if (sigismember(&myset, 10)) // Judge 10 Whether the signal is myset Signal concentration
{
printf("10 The signal is myset Signal concentration \n");
}
else
{
printf("10 The signal is not myset Signal concentration \n");
}
if (sigismember(&myset, 12)) // Judge 12 Whether the signal is myset Signal concentration
{
printf("12 The signal is myset Signal concentration \n");
}
else
{
printf("12 The signal is not myset Signal concentration \n");
}
sigprocmask(SIG_BLOCK, &myset, NULL); // Set up myset The signals in the signal set are blocking
sigprocmask(SIG_UNBLOCK, &myset, NULL); // relieve myset A signal in a signal set Blocking
sigemptyset(&myset); // Clear the signal set
int a=10;
while(a--)
{
sleep(1);
printf(" The parent process %d The signal 10\n", a);
sigaddset(&myset,10); // Add signal SIGUSR1 To signal set 10
sigprocmask(SIG_UNBLOCK, &myset, NULL); // relieve myset A signal in a signal set Blocking
}
sigemptyset(&myset); // Clear the signal set
a=5;
while(a--)
{
sleep(1);
printf(" The parent process %d The signal 12\n", a);
sigaddset(&myset,12); // Add signal SIGUSR2 To signal set 12
sigprocmask(SIG_UNBLOCK, &myset, NULL); // relieve myset A signal in a signal set Blocking
}
}
else if (pid == 0)
{
int a=7;
while(a--)
{
sleep(1);
printf(" Subprocesses %d The signal 10\n", a);
}
kill(getppid(), 10);
a=7;
while(a--)
{
sleep(1);
printf(" Subprocesses %d The signal 12\n", a);
}
kill(getppid(), 12);
}
return 0;
}
边栏推荐
- exlipse同时操作多行。比如同时在多行同列输入相同的文字
- 重磅:国产IDE发布,由阿里研发,完全开源!
- Google Earth engine (GEE) -- converts string to number and applies it to time search (ee.date.fromymd)
- get请求与post提交区别的简易理解
- Lucky hash quiz system development (source code deployment) fun investment hash game play development (case requirements)
- Exlipse operates on multiple rows at the same time. For example, input the same text in multiple lines and columns at the same time
- Basic syntax of unity script (4) - access to other game objects
- PG Basics - logical structure management (table inheritance, partition table)
- Jetpack compose for perfect screen fit
- 深入理解.Net中的线程同步之构造模式(二)内核模式4.内核模式构造物的总结
猜你喜欢

半导体动态杂谈

On the simplification and acceleration of join operation

Read all the knowledge points about enterprise im in one article

【刷题篇】避免洪水泛滥

Pytoch viewing model parameter quantity and calculation quantity

Tencent two sides: @bean and @component are used on the same class. What happens?

How can I protect my private key?
![[Title brushing] avoid flooding](/img/2d/95498d54c0c3c5ca79bb72b7a977bc.png)
[Title brushing] avoid flooding
![[recruitment (Guangzhou)] Chenggong Yi (Guangzhou) Net core middle and Senior Development Engineer](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[recruitment (Guangzhou)] Chenggong Yi (Guangzhou) Net core middle and Senior Development Engineer

Shell programming overview
随机推荐
【刷题篇】供暖器
[kali] Kali system, software update (with image source)
Embedded development: five C features that may no longer be prohibited
Why can't the database table be written into data
MySQL如何将列合并?
Deep understanding Net (2) kernel mode 2 Kernel mode construct semaphone
How does MySQL merge columns?
Comprehensively analyze the basic features and summary of free and paid SSH tools
[the path of system analyst] Chapter 5 Software Engineering (Agile Development)
正则系列之断言Assertions
On the simplification and acceleration of join operation
目录相关命令
想請教一下,我在佛山,到哪裏開戶比較好?手機開戶是安全麼?
[Title brushing] heater
逆向调试入门-PE中的VA与RVA换算04/07
【 scientific literature measurement 】 mining and visualization of keywords in foreign and Chinese Literature
【Redis 系列】redis 学习十六,redis 字典(map) 及其核心编码结构
[Title brushing] avoid flooding
Apache Doris comparison optimization Encyclopedia
【科学文献计量】外文文献及中文文献关键词的挖掘与可视化