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

/ 2,843评论 / 10539阅读 / 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. Charlesvox说道:

    http://levitrav.store/# Vardenafil price

  2. Elmersoalf说道:

    Cenforce 150 mg online cenforce.pro Cenforce 100mg tablets for sale

  3. KerrySharl说道:

    Компрессоры воздушные https://kompressorpnevmo.ru/ купить в Москве по лучшей цене. Широкий выбор брендов. Доставка по всей РФ. Скидки, подарки, гарантия от магазина.

  4. RandallGuesE说道:

    nolvadex side effects: tamoxifen and bone density – tamoxifen and ovarian cancer

  5. WilliamRen说道:

    https://cytotec.club/# cytotec online

  6. RonaldSwobe说道:

    https://finasteride.store/# cost generic propecia price

  7. Davidfrify说道:

    cytotec buy online usa buy cytotec buy cytotec pills

  8. RonaldSwobe说道:

    https://ciprofloxacin.tech/# cipro online no prescription in the usa

  9. RandallGuesE说道:

    1 lisinopril: prinivil price – lisinopril 3

  10. JamesKab说道:

    На этом сайте https://www.rabota-zarabotok.ru/ вы найдете полезную информацию, и отзывы о разных финансовых сайтах. Здесь очень много полезной информации, и разоблачение мошенников. А также узнайте где начать зарабатывать первые деньги в интернете.

  11. RandallGuesE说道:

    buy cytotec in usa: Misoprostol 200 mg buy online – buy misoprostol over the counter

  12. Davidfrify说道:

    Misoprostol 200 mg buy online purchase cytotec buy cytotec online fast delivery

  13. RonaldSwobe说道:

    https://nolvadex.life/# tamoxifen cost

  14. WilliamRen说道:

    https://nolvadex.life/# where to get nolvadex

  15. WilliamRen说道:

    https://lisinopril.network/# buy zestril online

  16. Davidfrify说道:

    antibiotics cipro ciprofloxacin mail online cipro online no prescription in the usa

  17. RandallGuesE说道:

    cytotec buy online usa: buy cytotec online – buy cytotec online

  18. WilliamRen说道:

    http://nolvadex.life/# tamoxifen pill

  19. Davidfrify说道:

    buy cipro ciprofloxacin generic buy cipro online without prescription

  20. WilliamRen说道:

    http://lisinopril.network/# can you buy lisinopril over the counter

  21. RandallGuesE说道:

    ciprofloxacin generic price: cipro for sale – ciprofloxacin order online

  22. Davidfrify说道:

    nolvadex for sale amazon tamoxifen for breast cancer prevention tamoxifen moa

  23. RandallGuesE说道:

    lisinopril 30 mg: zestoretic online – lisinopril prescription

  24. RandallGuesE说道:

    buy cytotec pills: buy cytotec in usa – buy misoprostol over the counter

  25. Dennisdrync说道:

    Компания КЗТО https://radiators-teplo.github.io/ известна производством высококачественных радиаторов, которые обеспечивают эффективное отопление и долговечность. Продукция КЗТО включает радиаторы различных модификаций, подходящие для любых помещений. Они изготавливаются из прочных материалов, что гарантирует устойчивость к коррозии и высокую теплоотдачу.

  26. Davidfrify说道:

    price lisinopril 20 mg lisinopril 5 mg drug lisinopril 5 mg

  27. WilliamRen说道:

    https://finasteride.store/# buying propecia for sale

  28. WilliamRen说道:

    https://lisinopril.network/# lisinopril 20 12.5 mg

  29. RandallGuesE说道:

    tamoxifen for breast cancer prevention: nolvadex for sale – does tamoxifen cause menopause

  30. WilliamRen说道:

    http://nolvadex.life/# nolvadex side effects

  31. Davidfrify说道:

    tamoxifen rash pictures where to get nolvadex aromatase inhibitor tamoxifen

  32. WilliamRen说道:

    http://finasteride.store/# cost cheap propecia without a prescription

  33. RandallGuesE说道:

    propecia online: buying generic propecia pill – buy cheap propecia online

  34. Davidfrify说道:

    lisinopril online pharmacy zestril 5 mg india price for 5 mg lisinopril

  35. RandallGuesE说道:

    tamoxifen and grapefruit: tamoxifen bone pain – nolvadex generic

  36. RandallGuesE说道:

    buy ciprofloxacin over the counter: cipro 500mg best prices – ciprofloxacin order online

  37. RonaldSwobe说道:

    http://nolvadex.life/# low dose tamoxifen

  38. WilliamRen说道:

    http://ciprofloxacin.tech/# cipro ciprofloxacin

  39. WilliamRen说道:

    http://nolvadex.life/# tamoxifen and ovarian cancer

  40. RonaldSwobe说道:

    https://lisinopril.network/# lisinopril 10 mg

  41. RonaldSwobe说道:

    https://finasteride.store/# buying cheap propecia without prescription

  42. RandallGuesE说道:

    buy cytotec online: buy cytotec over the counter – buy cytotec pills online cheap

  43. WilliamRen说道:

    http://cytotec.club/# Abortion pills online

  44. Davidfrify说道:

    where can i buy cipro online ciprofloxacin over the counter buy ciprofloxacin

  45. Davidfrify说道:

    buy propecia tablets cost of propecia without insurance buying propecia tablets

  46. Davidfrify说道:

    buy cipro online canada buy cipro online without prescription buy generic ciprofloxacin

  47. WilliamRen说道:

    https://cytotec.club/# order cytotec online

回复 Davidfrify 取消回复

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