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

/ 1,233评论 / 4889阅读 / 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. Jamescix说道:

    prednisone 40 mg: non prescription prednisone 20mg – prednisone 20mg for sale

  2. Davidham说道:

    ivermectin new zealand ivermectin tablets п»їwhere to buy stromectol online

  3. Charlesflarm说道:

    https://stromectol.fun/# ivermectin cream canada cost

  4. Jamescix说道:

    stromectol cost: stromectol 12mg – stromectol price uk

  5. Charlesflarm说道:

    https://lisinopril.top/# lisinopril 20 mg purchase

  6. Thank you for being a constant source of inspiration through your blog. Asheville is proud to support you!

  7. Your writing has a way of drawing readers in and keeping them engaged till the very end. Asheville appreciates your talent!

  8. Leonardlex说道:

    antibiotic amoxicillin: buy amoxicillin online uk – amoxicillin azithromycin

  9. Davidham说道:

    ivermectin coronavirus stromectol generic ivermectin generic cream

  10. Jamescix说道:

    ivermectin 1%: ivermectin 3mg dosage – order stromectol online

  11. Charlesflarm说道:

    http://furosemide.guru/# lasix 100 mg tablet

  12. Stephenvew说道:

    http://buyprednisone.store/# 2.5 mg prednisone daily

  13. Charlesflarm说道:

    https://lisinopril.top/# lisinopril online purchase

  14. Jamescix说道:

    prinivil 20 mg cost: generic lisinopril 5 mg – zestril 10 mg cost

  15. Thank you for sharing your expertise with the world through your blog. Asheville residents are grateful for your contributions.

  16. Davidham说道:

    buy stromectol pills ivermectin 1% ivermectin 4 tablets price

  17. Your blog is like a ray of sunshine on a cloudy day – uplifting and inspiring. Sending love from Asheville!

  18. Charlesflarm说道:

    https://amoxil.cheap/# amoxicillin without a prescription

  19. Thank you for sharing such insightful content! In Asheville, we love your blog and can’t wait to come back for more.

  20. 27614 Auto Glass说道:

    Thank you for being a constant source of inspiration through your blog. Asheville is proud to support you!

  21. Jamescix说道:

    buy furosemide online: lasix side effects – buy furosemide online

  22. Charlesflarm说道:

    https://stromectol.fun/# ivermectin 3 mg

  23. Jamescix说道:

    buy prednisone with paypal canada: cortisol prednisone – prednisone 100 mg

  24. Davidham说道:

    can i buy prednisone online without prescription online order prednisone prednisone 2.5 mg

  25. Leonardlex说道:

    amoxicillin online without prescription: amoxicillin pills 500 mg – canadian pharmacy amoxicillin

  26. Your writing is like a beacon of light in the darkness, guiding us towards knowledge and understanding. Asheville appreciates your blog!

  27. Jamescix说道:

    prednisone uk price: drug prices prednisone – can you buy prednisone

  28. Charlesflarm说道:

    http://furosemide.guru/# buy lasix online

  29. Stephenvew说道:

    http://amoxil.cheap/# amoxicillin discount coupon

  30. Davidham说道:

    amoxicillin 500mg capsule can you buy amoxicillin over the counter in canada cost of amoxicillin prescription

  31. Charlesflarm说道:

    http://buyprednisone.store/# prednisone 20 mg tablets coupon

  32. Jamescix说道:

    furosemide 40 mg: furosemide 100 mg – lasix 40mg

  33. Leonardlex说道:

    amoxicillin 500mg buy online uk: amoxicillin in india – amoxicillin 500 mg where to buy

发表回复

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