[linux操作系统实验]信号机制(按ctrl c给父进程发信号后,子进程没有反应?)

/ 9,852评论 / 36366阅读 / 1点赞

问题

按ctrl c给父进程传信号后,父进程会发送信号给子进程,为什么子进程没有反应?

而只有修改父进程给子进程发送的信号为SIGINT时才会有反应?


实验具体内容

实验内容

概念

信号的基本概念

信号的发送

对信号的处理

涉及函数

kill( )

signal( )

名  字说          明
01SIGHUP挂起(hangup)
02SIGINT中断,当用户从键盘按^c键或^break键时
03SIGQUIT退出,当用户从键盘按quit键时
04SIGILL非法指令
05SIGTRAP跟踪陷阱(trace trap),启动进程,跟踪代码的执行
06SIGIOTIOT指令
07SIGEMTEMT指令
08SIGFPE浮点运算溢出
09SIGKILL杀死、终止进程 
10SIGBUS总线错误
11SIGSEGV段违例(segmentation  violation),进程试图去访问其虚地址空间以外的位置
12SIGSYS系统调用中参数错,如系统调用号非法
13SIGPIPE向某个非读管道中写入数据
14SIGALRM闹钟。当某进程希望在某时间后接收信号时发此信号
15SIGTERM软件终止(software  termination)
16SIGUSR1用户自定义信号1
17SIGUSR2用户自定义信号2
18SIGCLD某个子进程死
19SIGPWR电源故障

源代码

#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;
}

运行结果截图

思考

  1. CharlesGib说道:

    neurontin discount: where can i buy neurontin online – neurontin 300 mg price

  2. CharlesGib说道:

    doxycycline generic: doxy – doxycycline 100 mg

  3. CharlesGib说道:

    neurontin 800 mg tablet: neurontin 600 mg pill – neurontin cap

  4. MarvinMek说道:

    doxycycline without prescription doxycycline medication 200 mg doxycycline

  5. Richardfrota说道:

    prednisone 50 mg price: prednisone 40 mg price – 60 mg prednisone daily

  6. DonaldEleva说道:

    Купить квартиру в новостройке https://newhomesale.ru/ в Казани. Продажа новой недвижимости в ЖК новостройках по ценам от застройщика.

  7. MarvinMek说道:

    amoxicillin without a doctors prescription amoxicillin 500mg for sale uk amoxicillin discount coupon

  8. HenryIsona说道:

    200 mg doxycycline: doxycycline 50 mg – doxycycline online

  9. Billyeroge说道:

    https://zithromaxa.store/# zithromax buy online

  10. MarvinMek说道:

    neurontin 600 mg cost gabapentin generic neurontin 300 mg caps

  11. Billyeroge说道:

    https://prednisoned.online/# purchase prednisone 10mg

  12. Billyeroge说道:

    http://prednisoned.online/# can i order prednisone

  13. HenryIsona说道:

    cost of prednisone 5mg tablets: prednisone 20 mg tablets coupon – prednisone 2.5 mg tab

  14. oborudovan_ivkt说道:

    оборудование для диспетчерских центров https://oborudovanie-dispetcherskih-centrov.ru .

  15. HenryIsona说道:

    medicine neurontin capsules: buying neurontin online – neurontin 400 mg cost

  16. HenryIsona说道:

    prednisone medicine: online order prednisone 10mg – prednisone 50 mg canada

  17. Billyeroge说道:

    http://doxycyclinea.online/# generic for doxycycline

  18. MarvinMek说道:

    buy neurontin uk how much is neurontin pills cost of neurontin 600mg

  19. MarvinMek说道:

    amoxicillin 500 mg tablet order amoxicillin uk can you buy amoxicillin over the counter canada

  20. DonaldtAg说道:

    maple leaf pharmacy in canada: canadian online pharmacy – canadian pharmacy cheap

  21. Williamfug说道:

    canadian online drugstore: canadian pharmacy no scripts – canada drugs online

  22. Donaldkenty说道:

    mexico drug stores pharmacies: mexico pharmacies prescription drugs – mexico drug stores pharmacies

  23. DavidTaK说道:

    п»їbest mexican online pharmacies mexican pharmaceuticals online mexican online pharmacies prescription drugs

  24. JustinHit说道:

    курсы по эксель – Обучение с гарантиями государственного университета.

  25. Felipelam说道:

    http://pharmcanada.shop/# safe canadian pharmacy

  26. DonaldtAg说道:

    indian pharmacies safe: indian pharmacy paypal – п»їlegitimate online pharmacies india

  27. Williamfug说道:

    canadian pharmacy no prescription: indian pharmacy no prescription – buy medication online without prescription

  28. Felipelam说道:

    http://pharmworld.store/# online pharmacy no prescription needed

  29. Felipelam说道:

    https://pharmindia.online/# online shopping pharmacy india

  30. Williamfug说道:

    prescription drugs from canada: pharm world – pharmacy coupons

  31. Williamfug说道:

    canadian online pharmacy: canada pharmacy 24h – canada drugs online review

  32. DavidTaK说道:

    pharmacy with no prescription no prescription on line pharmacies cheap prescription drugs online

  33. Ricardocah说道:

    Продажа квартир в Казани https://kupitkvartiruzdes.ru/ от застройщика. Большой выбор квартир. Возможность купить онлайн. Квартиры с дизайнерской отделкой.

  34. Felipelam说道:

    https://pharmcanada.shop/# canadian pharmacy

  35. DavidTaK说道:

    prescription drugs canada no prescription meds online no prescription

  36. Williamfug说道:

    mexico drug stores pharmacies: mexican mail order pharmacies – medication from mexico pharmacy

  37. DavidTaK说道:

    canadian pharmacy in canada legitimate canadian mail order pharmacy canadian drug stores

  38. Felipelam说道:

    https://pharmindia.online/# Online medicine home delivery

  39. Williamfug说道:

    legit canadian pharmacy online: canada pharmacy reviews – canadian pharmacy service

  40. Felipelam说道:

    http://pharmindia.online/# india pharmacy mail order

  41. AlvinLek说道:

    Купить квартиру https://newflatsale.ru/ в новостройке: однокомнатную, двухкомнатную, трехкомнатную в жилом комплексе в рассрочку, ипотеку, мат. капитал от застройщика.

  42. Williamfug说道:

    cheap prescription medication online: buying prescription medicine online – ordering prescription drugs from canada

  43. Donaldkenty说道:

    cheapest prescription pharmacy: pharm world – online pharmacy non prescription drugs

  44. Felipelam说道:

    http://pharmmexico.online/# buying from online mexican pharmacy

  45. Williamfug说道:

    top online pharmacy india: india pharmacy mail order – top 10 pharmacies in india

  46. DavidTaK说道:

    canadian pharmacy 1 internet online drugstore canadian drug pharmacy buying drugs from canada

  47. Felipelam说道:

    http://pharmindia.online/# world pharmacy india

  48. DavidTaK说道:

    canadian pharmacies not requiring prescription cheapest pharmacy best canadian pharmacy no prescription

  49. Williamfug说道:

    canadian pharmacy coupon code: cheapest pharmacy for prescription drugs – canadian pharmacy without prescription

  50. DonaldtAg说道:

    offshore pharmacy no prescription: legal online pharmacy coupon code – canadian pharmacy no prescription

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注