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

/ 9,839评论 / 36307阅读 / 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. Jamesexite说道:

    Buy Cenforce 100mg Online: order cenforce – buy cenforce

  2. Elmersoalf说道:

    Buy Vardenafil 20mg online Buy Vardenafil 20mg Buy Vardenafil 20mg

  3. Charlesvox说道:

    http://cenforce.pro/# buy cenforce

  4. Elmersoalf说道:

    cheap kamagra kamagra Kamagra tablets

  5. Jamesexite说道:

    Buy generic Levitra online: Levitra 20mg price – Vardenafil buy online

  6. JamesEssep说道:

    We’ll buy or invest in your site and you Investment/Buying ranging from $50,000 to $500,000, depending on stage, market volume, market share https://buy-site.pages.dev/

  7. Elmersoalf说道:

    Buy Vardenafil 20mg online Levitra generic price Levitra online USA fast

  8. Tylerreunk说道:

    http://kamagra.win/# Kamagra Oral Jelly

  9. Charlesvox说道:

    https://levitrav.store/# Cheap Levitra online

  10. Jamesexite说道:

    cheapest cialis: Generic Cialis without a doctor prescription – buy cialis pill

  11. Charlesvox说道:

    https://cialist.pro/# Buy Cialis online

  12. Jamesexite说道:

    Cialis over the counter: buy cialis overseas – Buy Tadalafil 5mg

  13. Jamesexite说道:

    cenforce for sale: cenforce for sale – cenforce for sale

  14. Charlesvox说道:

    http://viagras.online/# sildenafil online

  15. Ronaldjah说道:

    Купить компрессоры https://kompressoroil.ru/ по самым выгодным ценам в Москве в интернет-магазине. Широкий выбор компрессоров. В каталоге можно ознакомиться с ценами, отзывами, фотографиями и подробными характеристиками компрессоров.

  16. Elmersoalf说道:

    Kamagra 100mg kamagra.win buy kamagra online usa

  17. Elmersoalf说道:

    п»їkamagra п»їkamagra п»їkamagra

  18. Charlesvox说道:

    http://cialist.pro/# Cheap Cialis

  19. Georgefrege说道:

    Sildenafil 100mg price: Cheap Viagra 100mg – viagra without prescription

  20. Georgefrege说道:

    buy kamagra online usa: kamagra.win – buy kamagra online usa

  21. Jamesexite说道:

    Levitra online USA fast: Cheap Levitra online – Levitra 10 mg buy online

  22. Elmersoalf说道:

    Buy Tadalafil 5mg Generic Cialis without a doctor prescription Buy Tadalafil 5mg

  23. DavidBiolo说道:

    http://levitrav.store/# Cheap Levitra online

  24. DavidBiolo说道:

    https://cialist.pro/# Cialis over the counter

  25. WilliamPyday说道:

    Воздушные компрессоры https://kompressorgaz.ru/ купить по самым низким ценам только у нас с гарантией и бесплатной доставкой. Широкий ассортимент воздушных поршневых компрессоров.

  26. Charlesvox说道:

    http://viagras.online/# buy Viagra online

  27. DavidBiolo说道:

    https://kamagra.win/# Kamagra 100mg

  28. DavidBiolo说道:

    http://cialist.pro/# buy cialis pill

  29. Jamesexite说道:

    Sildenafil Citrate Tablets 100mg: Buy generic 100mg Viagra online – Cheap Viagra 100mg

  30. Georgefrege说道:

    Buy Tadalafil 20mg: cialist.pro – Buy Tadalafil 5mg

  31. Elmersoalf说道:

    Cheap Sildenafil 100mg viagras.online Viagra generic over the counter

  32. Charlesvox说道:

    https://kamagra.win/# sildenafil oral jelly 100mg kamagra

  33. Jamesexite说道:

    Buy Vardenafil 20mg online: levitrav.store – Buy Vardenafil online

  34. Jamesexite说道:

    cheapest viagra: Cheapest place to buy Viagra – cheapest viagra

  35. Charlesvox说道:

    https://kamagra.win/# sildenafil oral jelly 100mg kamagra

  36. Elmersoalf说道:

    Tadalafil price cialist.pro Buy Tadalafil 10mg

  37. Georgefrege说道:

    Purchase Cenforce Online: Purchase Cenforce Online – Cenforce 100mg tablets for sale

  38. Tylerreunk说道:

    https://cialist.pro/# Generic Tadalafil 20mg price

  39. MartinBam说道:

    Воздушные компрессоры https://porshkompressor.ru/ в Москве – купить по низким ценам в интернет-магазине. Широкий ассортимент воздушных поршневых компрессоров. В каталоге – передвижные, стационарные модели, с прямым и ременным приводом, сухого сжатия и маслозаполненные.

  40. Elmersoalf说道:

    Cheap Levitra online Levitra generic price Vardenafil price

  41. Charlesvox说道:

    https://cenforce.pro/# cheapest cenforce

  42. Jamesexite说道:

    sildenafil oral jelly 100mg kamagra: kamagra oral jelly – super kamagra

  43. Charlesvox说道:

    http://kamagra.win/# super kamagra

  44. Elmersoalf说道:

    Cheap generic Viagra Buy Viagra online cheap Cheap generic Viagra online

  45. Jamesexite说道:

    Levitra online USA fast: buy Levitra over the counter – buy Levitra over the counter

  46. Elmersoalf说道:

    Cialis over the counter Generic Tadalafil 20mg price Cialis 20mg price in USA

  47. chistka_czEi说道:

    портативный лазерный очиститель ржавчины цена https://apparaty-lazernoy-ochistki.ru/ .

  48. Jamesexite说道:

    Viagra online price: Cheap Viagra 100mg – buy Viagra over the counter

  49. Jamesexite说道:

    cenforce for sale: buy cenforce – Purchase Cenforce Online

发表回复

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