问题
按ctrl c给父进程传信号后,父进程会发送信号给子进程,为什么子进程没有反应?
而只有修改父进程给子进程发送的信号为SIGINT时才会有反应?
- 原因:由于在shell中运行了父进程,父进程又创建了两个子进程,此时两个子进程和父进程是同一个进程组的,当我们在shell中按ctrl c时,会给前台进程组(即父进程和两个子进程)发送了SIGINT信号,此时父进程有对应SIGINT信号的自定义操作,但子进程并没有,因此子进程此时会执行这个信号默认的操作:结束进程。所以说,在我们按下ctrl c时,子进程已经结束了,此时父进程在给子进程发信号,但子进程已经死了,所以发的信号自然就没用了。
- 如果修改父进程给子进程发送的信号为SIGINT,就可以了吗?
- 还是不太正确的
- 因为在实验中,子进程收到信号后执行操作后就会结束进程。当我们按下ctrl c时,就已经发送了SIGINT信号给了子进程,子进程虽然此时会执行我们的自定义操作,输出信息,但之后就结束进程了。然后父进程才发信号过来给子进程,但子进程此时也是已经死了,所以父进程的信号依然是不起效果的。
- 解决办法:
- 可以让子进程忽略 ctrl c 即 SIGINT 信号,父进程依旧发送16 / 17信号
- 在子进程中加一句 signal(SIGINT ,1); //忽略SIGINT信号
- 可以另起一个会话窗口,先($ ps -a )找到父进程的pid,一般父进程是同名的三个进程中pid最小的那个,然后($ kill -2 父进程pid)来单独给父进程发送SIGINT信号,这样就避免了一开始按 ctrl c 时子进程跟着父进程接收了SININT信号的问题。
- 可以让子进程忽略 ctrl c 即 SIGINT 信号,父进程依旧发送16 / 17信号
实验具体内容
实验内容
- 编写程序:用fork( )创建两个子进程,再用系统调用signal( )让父进程捕捉键盘上来的中断信号(即按^c键);捕捉到中断信号后,父进程用系统调用kill( )向两个子进程发出信号,子进程捕捉到信号后分别输出下列信息后终止:
- Child process1 is killed by parent!
- Child process2 is killed by parent!
- 父进程等待两个子进程终止后,输出如下的信息后终止:
- Parent process is killed!
- 分析利用软中断通信实现进程同步的机理
概念
信号的基本概念
- 每个信号都对应一个正整数常量(称为signal number,即信号编号。定义在系统头文件<signal.h>中),代表同一用户的诸进程之间传送事先约定的信息的类型,用于通知某进程发生了某异常事件。每个进程在运行时,都要通过信号机制来检查是否有信号到达。若有,便中断正在执行的程序,转向与该信号相对应的处理程序,以完成对该事件的处理;处理结束后再返回到原来的断点继续执行。实质上,信号机制是对中断机制的一种模拟,故在早期的Linux版本中又把它称为软中断。
- 信号与中断的相似点:
- (1)采用了相同的异步通信方式;
- (2)当检测出有信号或中断请求时,都暂停正在执行的程序而转去执行相应的处理程序;
- (3)都在处理完毕后返回到原来的断点;
- (4)对信号或中断都可进行屏蔽。
- 信号与中断的区别:
- (1)中断有优先级,而信号没有优先级,所有的信号都是平等的;
- (2)信号处理程序是在用户态下运行的,而中断处理程序是在核心态下运行;
- (3)中断响应是及时的,而信号响应通常都有较大的时间延迟。
- 信号机制具有以下三方面的功能:
- (1)发送信号。发送信号的程序用系统调用kill( )实现;
- (2)预置对信号的处理方式。接收信号的程序用signal( )来实现对处理方式的预置;
- (3)收受信号的进程按事先的规定完成对相应事件的处理。
信号的发送
- 信号的发送,是指由发送进程把信号送到指定进程的信号域的某一位上。如果目标进程正在一个可被中断的优先级上睡眠,核心便将它唤醒,发送进程就此结束。一个进程可能在其信号域中有多个位被置位,代表有多种类型的信号到达,但对于一类信号,进程却只能记住其中的某一个。
- 进程用kill( )向一个进程或一组进程发送一个信号。
对信号的处理
- 当一个进程要进入或退出一个低优先级睡眠状态时,或一个进程即将从核心态返回用户态时,核心都要检查该进程是否已收到软中断。当进程处于核心态时,即使收到软中断也不予理睬;只有当它返回到用户态后,才处理软中断信号。对软中断信号的处理分三种情况进行:
- (1)如果进程收到的软中断是一个已决定要忽略的信号(function=1),进程不做任何处理便立即返回;
- (2)进程收到软中断后便退出(function=0);
- (3)执行用户设置的软中断处理程序。
涉及函数
kill( )
- 系统调用格式
- int kill(pid,sig)
- 参数定义
- int pid,sig;
- 其中,pid是一个或一组进程的标识符,参数sig是要发送的软中断信号
- (1)pid>0时,核心将信号发送给进程pid
- (2)pid=0时,核心将信号发送给与发送进程同组的所有进程
- (3)pid=-1时,核心将信号发送给所有用户标识符真正等于发送进程的有效用户标识号的进程
signal( )
- 预置对信号的处理方式,允许调用进程控制软中断信号。
- 系统调用格式
- signal(sig,function)
- 头文件为
- #include <signal.h>
- 参数定义
- signal(sig,function)
- int sig;
- void (*func) ( )
- 其中sig用于指定信号的类型,sig为0则表示没有收到任何信号,余者如下表:
值 | 名 字 | 说 明 |
01 | SIGHUP | 挂起(hangup) |
02 | SIGINT | 中断,当用户从键盘按^c键或^break键时 |
03 | SIGQUIT | 退出,当用户从键盘按quit键时 |
04 | SIGILL | 非法指令 |
05 | SIGTRAP | 跟踪陷阱(trace trap),启动进程,跟踪代码的执行 |
06 | SIGIOT | IOT指令 |
07 | SIGEMT | EMT指令 |
08 | SIGFPE | 浮点运算溢出 |
09 | SIGKILL | 杀死、终止进程 |
10 | SIGBUS | 总线错误 |
11 | SIGSEGV | 段违例(segmentation violation),进程试图去访问其虚地址空间以外的位置 |
12 | SIGSYS | 系统调用中参数错,如系统调用号非法 |
13 | SIGPIPE | 向某个非读管道中写入数据 |
14 | SIGALRM | 闹钟。当某进程希望在某时间后接收信号时发此信号 |
15 | SIGTERM | 软件终止(software termination) |
16 | SIGUSR1 | 用户自定义信号1 |
17 | SIGUSR2 | 用户自定义信号2 |
18 | SIGCLD | 某个子进程死 |
19 | SIGPWR | 电源故障 |
- function:在该进程中的一个函数地址,在核心返回用户态时,它以软中断信号的序号作为参数调用该函数,对除了信号SIGKILL,SIGTRAP和SIGPWR以外的信号,核心自动地重新设置软中断信号处理程序的值为SIG_DFL,一个进程不能捕获SIGKILL信号。
- function 的解释如下:
- (1)function=1时,进程对sig类信号不予理睬,亦即屏蔽了该类信号;
- (2)function=0时,缺省值,进程在收到sig信号后应终止自己;
- (3)function为非0,非1类整数时,function的值即作为信号处理程序的指针。
源代码
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <iostream>
#include <sys/wait.h>
#include <sys/types.h>
using namespace std;
/*记录子进程pid,
* whois:
* 0:父进程
* 1:子进程1
* 2:子进程2*/
int pid1 = 0, pid2 = 0, whois = 0;
/*信号*/
#define COOLSIG SIGINT
/*响应信号*/
void do_exit(int)
{
switch (whois)
{
case 0:
{
kill(pid1, COOLSIG);
kill(pid2, COOLSIG);
cout << "<< wait1: " << wait(0) << endl;
cout << "<< wait2: " << wait(0) << endl;
cout << "<< 父进程退出" << endl;
}break;
case 1:;
case 2:
{
cout << "<< 子进程" << whois << "结束!" << endl;
}break;
}
exit(0);
}
int main()
{
cout << "<< COOLSIG = " << COOLSIG << endl;
pid1 = fork();
if (pid1 == -1){
cout << "<< fork error" << endl;
}
else if (pid1 == 0)
{
whois = 1; //子进程1
signal(COOLSIG, do_exit);
}
else
{
cout << "pid1 = " << pid1 << endl;
pid2 = fork(); //父进程
if (pid2 == -1)
{
cout << "<< fork error" << endl;
}
else if (pid2 == 0)
{
whois = 2; //子进程2
signal(COOLSIG,do_exit);
}
else
{
cout << "pid2 = " << pid2 << endl;
whois = 0; //父进程
signal(SIGINT, do_exit);
cout << "<< 父进程等待 ctrl + C ..." << endl;
}
}
while (1)
usleep(100);
cout << "<< 退出了,whois = " << whois << endl;
return 0;
}
运行结果截图

思考
- 本程序的预期结果为显示:
- Child process1 is killed by parent!
- Child process2 is killed by parent!
- Parent process is killed!
- 预期的结果可以正确显示吗?如果不可以,程序该如何修改才能得到正确结果?
- 不能,解决办法如下几种:
- 可以把给子进程发送和接收的信号改为 SIGINT(此办法可以使输出正常,但父进程的发给子进程的信号并没有用到)
- 可以让子进程忽略 ctrl c 即 SIGINT 信号,父进程依旧发送16 / 17信号
- 在子进程中加一句 signal(SIGINT ,1);
- 可以另起一个会话窗口,先($ ps -a )找到父进程的pid,然后使用命令($ kill -2 父进程pid)来单独给父进程发送SIGINT信号,这样就避免了一开始按 ctrl c 时子进程跟着父进程接收了SININT信号的问题。
- 该程序段前面部分用了两个wait(0),它们起什么作用?
- 一个wait(0)只会让父进程等待一个子进程结束,而本实验中有2个子进程,使用两个wait(0)可以让父进程等待两个子进程全都结束后才输出并结束。
Acheter kamagra site fiable
https://esfarmacia.men/# п»їfarmacia online
acheter sildenafil 100mg sans ordonnance
http://farmaciabarata.pro/# farmacias baratas online envГo gratis
http://pharmacieenligne.icu/# Pharmacie en ligne France
п»їfarmacia online farmacias online baratas farmacias online seguras en espaГ±a
http://farmaciabarata.pro/# farmacia online internacional
india pharmacy: canadian pharmacy india – best india pharmacy
Totally! Find expos‚ portals in the UK can be crushing, but there are many resources accessible to help you think the unmatched in unison because you. As I mentioned formerly, conducting an online search representing http://www.ampheon.co.uk/perl/pags/how-old-is-will-cain-from-fox-news.html “UK scuttlebutt websites” or “British information portals” is a great starting point. Not only purposefulness this hand out you a encyclopaedic list of report websites, but it choice also provide you with a improved savvy comprehension or of the in the air hearsay prospect in the UK.
Aeons ago you secure a liber veritatis of embryonic rumour portals, it’s critical to gauge each anyone to influence which upper-class suits your preferences. As an benchmark, BBC Intelligence is known benefit of its objective reporting of intelligence stories, while The Trustee is known for its in-depth criticism of political and popular issues. The Unconnected is known pro its investigative journalism, while The Times is known in search its vocation and investment capital coverage. During concession these differences, you can decide the information portal that caters to your interests and provides you with the newsflash you hope for to read.
Additionally, it’s significance considering local scuttlebutt portals because proper to regions within the UK. These portals yield coverage of events and dirt stories that are applicable to the area, which can be exceptionally cooperative if you’re looking to keep up with events in your town community. For exemplar, local good copy portals in London classify the Evening Paradigm and the Londonist, while Manchester Evening News and Liverpool Repercussion are popular in the North West.
Comprehensive, there are tons bulletin portals available in the UK, and it’s significant to do your research to see the one that suits your needs. Sooner than evaluating the unconventional news programme portals based on their coverage, dash, and article angle, you can select the individual that provides you with the most related and captivating low-down stories. Esteemed fortunes with your search, and I hope this tidings helps you discover the perfect news broadcast portal since you!
cheapest online pharmacy india: cheapest online pharmacy india – india pharmacy
Europe is a continent with a in clover representation and dissimilar culture. Lifestyle in Europe varies greatly depending on the provinces and region, but there are some commonalities that can be observed.
United of the defining features of human being in Europe is the putrid moment on work-life balance. Diverse European countries have laws mandating a reliable amount of vacation all together seeking workers, and some experience yet experimented with shorter workweeks. This allows seeking more just the same from time to time forth with next of kin and pursuing hobbies and interests.
https://www.machinefabriekelburg.nl/wp-content/pages/designer-anna-berezina.html
Europe is also known in support of its wealth cultural legacy, with assorted cities boasting centuries-old architecture, aptitude, and literature. Museums, galleries, and factual sites are bountiful, and visitors can bury themselves in the record and customs of the continent.
In addition to cultural attractions, Europe is haunt to a afield variety of natural beauty. From the dramatic fjords of Norway to the cheery beaches of the Mediterranean, there is no shortage of stunning landscapes to explore.
Of ambit, life in Europe is not without its challenges. Varied countries are grappling with issues such as gains unevenness, immigration, and federal instability. However, the people of Europe are resilient and get a fancy history of overcoming adversity.
Blanket, life in Europe is opulent and assorted, with something to offer for everyone. Whether you’re interested in information, refinement, nature, or unmistakably enjoying a good work-life match, Europe is a horrendous place to entitle home.
Бурение скважин сверху воду – это процесс создания отверстий в течение поднебесной для извлечения подземных вожак, кои могут применяться чтобы разных мишеней, включая питьевую воду, увлажнение растений, промышленные необходимости а также другие: https://zenwriting.net/cracknoise1/kak-uznat-gde-burit-skvazhinu-voda. Для бурения скважин используют специальное ясс, подобное как бурильные агрегата, какие проникают в течение землю да основывают дыры глубиной через пары 10-ов ут пары сотен метров.
После формирования скважины проводится тестирование, чтобы определить ее производительность и штрих воды. Затем щель оборудуется насосом и еще прочими системами, чтоб защитить хронический доступ к воде. Хотя бурение скважин сверху воду представляет необходимую цена на обеспечивании подхода к непорочною водопитьевой восе и утилизируется в течение разных секторах экономики промышленности, этот процесс что ль показывать негативное суггестивность сверху обступающую среду. Поэтому что поделаешь соблюдать должные правила а также регуляции.
Бурение скважин сверху водичку – этто эпидпроцесс организации отверстий на земле чтобы извлечения подземных вод. Эти скважины утилизируются для хозпитьевой воды, сплав растений, промышленных надобностей и остальных целей. Эпидпроцесс бурения скважин охватывает в себе эксплуатация специального оснащения, подобного яко бурильные предписания, коим проникают на грунт также создают отверстия: https://pastelink.net/b6kpq4yl. Эти скважины элементарно быть владельцем глубину от пары 10-ов до пары сотен метров.
После создания скважины, умельцы объезжают стресс-тестирование, чтоб фиксировать ее эффективность а также штрих воды. Через некоторое время скважина снабжается насосом равным образом прочими общественный порядок, чтобы защитить постоянный доступ ко воде. Эмпайр скважин на водичку является принципиальным процессом, яже обеспечивает путь буква аккуратной хозпитьевой здесь также используется в различных секторах экономики промышленности. Однако, этот процесс что ль носить отрицательное суггестивность на брать в кольцо среду, то-то что поделаешь хранить отвечающие философия равным образом регуляции.
To presume from present rumour, adhere to these tips:
Look representing credible sources: https://lostweekendnyc.com/articles/?alarm-mode-identifying-media-coverage-that-creates.html. It’s high-ranking to guard that the expos‚ outset you are reading is reliable and unbiased. Some examples of good sources tabulate BBC, Reuters, and The Modish York Times. Interpret multiple sources to stimulate a well-rounded view of a discriminating statement event. This can support you return a more complete display and escape bias. Be in the know of the angle the article is coming from, as flush with reputable news sources can contain bias. Fact-check the information with another source if a expos‚ article seems too sensational or unbelievable. Many times fetch persuaded you are reading a current article, as news can change quickly.
By following these tips, you can befit a more aware of rumour reader and more wisely know the beget around you.
http://indiapharmacy.cheap/# pharmacy website india
canadian pharmacy india: top 10 online pharmacy in india – india pharmacy
is canadian pharmacy legit pharmacies in canada that ship to the us canada drugs online reviews
legitimate canadian pharmacy: pharmacy in canada – safe canadian pharmacy
http://misoprostol.guru/# п»їcytotec pills online
http://lipitor.pro/# brand name lipitor
http://ciprofloxacin.ink/# cipro ciprofloxacin
https://misoprostol.guru/# buy cytotec online
https://ciprofloxacin.ink/# buy cipro
http://lipitor.pro/# lipitor 10mg price comparison
https://avodart.pro/# can i buy cheap avodart pill
http://lisinopril.pro/# zestril 5 mg
Paxlovid buy online: paxlovid pharmacy – paxlovid pill