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

/ 10,651评论 / 38437阅读 / 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. StevenWearo说道:

    can you buy clomid no prescription: clomid rex pharm – clomid cost

  2. StevenWearo说道:

    prednisone 10mg prices: prednisone – prednisone 5 50mg tablet price

  3. Brandenven说道:

    buy priligy max pharm: buy priligy – priligy max pharm

  4. StevenWearo说道:

    dapoxetine online: dapoxetine price – cheap priligy

  5. Brandenven说道:

    clomid tablet: clomid purchase online rex pharm – generic clomid without prescription

  6. Derekjup说道:

    prednisone uk: order Prednisone – prednisone 20mg price in india

  7. CurtisWer说道:

    BBgate MarketPlace 2024 Breaking Bad Gate Forum
    BBgate MarketPlace

  8. Brandenven说道:

    buy dapoxetine online: buy dapoxetine online – buy priligy max pharm

  9. StevenWearo说道:

    dapoxetine online: priligy max pharm – max pharm

  10. CurtisWer说道:

    BBgate MarketPlace 2024 Breaking Bad Gate Forum
    BBgate MarketPlace

  11. Derekjup说道:

    prednisone 2 5 mg: prednisone ray pharm – buy prednisone canada

  12. Derekjup说道:

    amoxicillin 200 mg tablet: com pharm – amoxicillin online canada

  13. StevenWearo说道:

    can i order cheap clomid without rx: generic clomid – buying generic clomid without a prescription

  14. Brandenven说道:

    875 mg amoxicillin cost: amoxil – amoxicillin 30 capsules price

  15. CurtisWer说道:

    BBgate MarketPlace 2024 Breaking Bad Gate Forum
    BBgate MarketPlace

  16. StevenWearo说道:

    buy priligy max pharm: priligy – dapoxetine online

  17. Brandenven说道:

    50 mg prednisone from canada: prednisone – prednisone without rx

  18. Brandenven说道:

    prednisone pill: ray pharm – prednisone 20 mg in india

  19. StevenWearo说道:

    prescription for amoxicillin: com pharm – amoxacillian without a percription

  20. Brandenven说道:

    prednisone 5093: cheap prednisone – mail order prednisone

  21. StevenWearo说道:

    amoxicillin price without insurance: Amoxicillin Com Pharm – amoxicillin 825 mg

  22. Brandenven说道:

    prednisone 12 tablets price: generic Prednisone – prednisone 54

  23. StevenWearo说道:

    get generic clomid price: can you buy generic clomid pills – how can i get cheap clomid

  24. Brandenven说道:

    can you buy prednisone over the counter in canada: prednisoneraypharm – buy prednisone 40 mg

  25. Derekjup说道:

    Priligy tablets: dapoxetine price – buy priligy

  26. Derekjup说道:

    buy dapoxetine online: priligy – buy priligy

  27. Brandenven说道:

    Priligy tablets: dapoxetine online – priligy

  28. StevenWearo说道:

    where can you buy prednisone: prednisone ray pharm – purchase prednisone canada

  29. Тут можно преобрести где купить сейф для ружья купить сейф для ружья москва

  30. Brandenven说道:

    order cheap clomid without dr prescription: buy clomid – cost of generic clomid no prescription

  31. StevenWearo说道:

    prednisone 50 mg tablet cost: prednisone ray pharm – 20 mg prednisone tablet

  32. Bradleyarrip说道:

    prednisone cost 10mg prednisone 1 mg daily buy prednisone online no script

  33. StevenWearo说道:

    dapoxetine online: dapoxetine online – buy priligy max pharm

  34. Brandenven说道:

    max pharm: Priligy tablets – buy priligy

  35. Узнай все о двустороннее варикоцеле степени варикоцеле

  36. Appreciate the balance and fairness, like a judge, but without the gavel.

  37. StevenWearo说道:

    amoxicillin price canada: amoxil – amoxicillin no prescipion

  38. Brandenven说道:

    buy priligy: priligy maxpharm – priligy

  39. Derekjup说道:

    can i get cheap clomid without insurance: can i purchase cheap clomid prices – can i order generic clomid without insurance

  40. Предлагаем услуги профессиональных инженеров офицальной мастерской.
    Еслли вы искали официальный сервисный центр xiaomi, можете посмотреть на сайте: сервисный центр xiaomi
    Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!

  41. StevenWearo说道:

    clomid medication: clomid rex pharm – can you buy generic clomid online

  42. Bradleyarrip说道:

    how to buy amoxycillin Amoxicillin for sale cost of amoxicillin prescription

  43. Bradleyarrip说道:

    how to buy cheap clomid prices clomid online get cheap clomid

发表回复

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