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

/ 9,847评论 / 36346阅读 / 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. DonaldtAg说道:

    best india pharmacy: top 10 pharmacies in india – indianpharmacy com

  2. Donaldkenty说道:

    india online pharmacy: top online pharmacy india – Online medicine order

  3. RogerPes说道:

    Почему посудомоечная машина https://kulbar.ru/2024/01/21/pochemu-posudomoechnaya-mashina-eto-neobhodimost-dlya-sovremennogo-doma/ необходимость для современного дома? Как использовать и как выбрать посудомойку?

  4. DavidTaK说道:

    reputable mexican pharmacies online mexican mail order pharmacies mexico drug stores pharmacies

  5. Felipelam说道:

    http://pharmworld.store/# mail order prescription drugs from canada

  6. Felipelam说道:

    https://pharmindia.online/# reputable indian pharmacies

  7. Felipelam说道:

    https://pharmnoprescription.icu/# order prescription from canada

  8. Williamfug说道:

    canada pharmacy online legit: canadian pharmacy store – escrow pharmacy canada

  9. DonaldtAg说道:

    mexican mail order pharmacies: mexico drug stores pharmacies – purple pharmacy mexico price list

  10. Felipelam说道:

    http://pharmworld.store/# non prescription medicine pharmacy

  11. DavidTaK说道:

    no prescription canadian pharmacy buy drugs without prescription prescription meds from canada

  12. DavidTaK说道:

    buying prescription drugs in mexico online mexico drug stores pharmacies buying from online mexican pharmacy

  13. Williamfug说道:

    medication canadian pharmacy: canadadrugpharmacy com – global pharmacy canada

  14. DavidTaK说道:

    canadian pharmacy 365 canadian pharmacy king reviews pharmacies in canada that ship to the us

  15. DonaldtAg说道:

    onlinepharmaciescanada com: reputable canadian pharmacy – reliable canadian pharmacy reviews

  16. Richardwog说道:

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

  17. DavidTaK说道:

    canada drugs coupon code pharm world canadian pharmacy discount code

  18. Williamfug说道:

    reputable mexican pharmacies online: buying prescription drugs in mexico – mexican border pharmacies shipping to usa

  19. Donaldkenty说道:

    buy drugs from canada: northwest canadian pharmacy – vipps approved canadian online pharmacy

  20. Felipelam说道:

    https://pharmmexico.online/# п»їbest mexican online pharmacies

  21. Williamfug说道:

    pharmacy website india: indian pharmacy – best india pharmacy

  22. Donaldkenty说道:

    canadian pharmacy antibiotics: safe canadian pharmacy – canada drug pharmacy

  23. Williamfug说道:

    canadian pharmacy coupon code: pharm world store – canadian pharmacy world coupons

  24. Felipelam说道:

    http://pharmnoprescription.icu/# canada prescription

  25. DavidTaK说道:

    online medication without prescription canadian pharmacy no prescription needed buying prescription drugs online without a prescription

  26. DavidTaK说道:

    cheap pharmacy no prescription pharm world pharmacy discount coupons

  27. Felipelam说道:

    http://pharmindia.online/# top 10 pharmacies in india

  28. Williamfug说道:

    mexican mail order pharmacies: medicine in mexico pharmacies – best online pharmacies in mexico

  29. DavidTaK说道:

    mexico drug stores pharmacies pharmacies in mexico that ship to usa mexican pharmacy

  30. DonaldtAg说道:

    canada online pharmacy: online pharmacy canada – canadian pharmacies that deliver to the us

  31. DonaldtAg说道:

    reputable mexican pharmacies online: п»їbest mexican online pharmacies – best online pharmacies in mexico

  32. Felipelam说道:

    https://pharmworld.store/# canadian pharmacy world coupon

  33. Williamfug说道:

    mexican border pharmacies shipping to usa: mexico drug stores pharmacies – mexico drug stores pharmacies

  34. DonaldtAg说道:

    canadian pharmacy scam: canadian 24 hour pharmacy – legitimate canadian mail order pharmacy

  35. DavidTaK说道:

    best canadian online pharmacy pharmacy rx world canada the canadian drugstore

  36. Felipelam说道:

    http://pharmmexico.online/# mexican rx online

  37. Williamfug说道:

    online pharmacy non prescription drugs: cheapest pharmacy – canadian pharmacy no prescription needed

  38. Williamfug说道:

    online shopping pharmacy india: buy medicines online in india – pharmacy website india

  39. Felipelam说道:

    http://pharmcanada.shop/# my canadian pharmacy rx

  40. DavidTaK说道:

    canadian pharmacy 1 internet online drugstore reliable canadian pharmacy canadian pharmacy store

  41. DonaldtAg说道:

    canadian pharmacy no prescription needed: pharm world – canadian pharmacy world coupon code

  42. Jeromeviaph说道:

    https://pharmmexico.online/# mexico pharmacy

  43. Jeromeviaph说道:

    https://pharmindia.online/# п»їlegitimate online pharmacies india

  44. DavidTaK说道:

    world pharmacy india buy prescription drugs from india cheapest online pharmacy india

  45. Felipelam说道:

    https://pharmmexico.online/# mexican drugstore online

  46. Donaldkenty说道:

    overseas online pharmacy-no prescription: online pharmacy that does not require a prescription – how to get prescription drugs from canada

  47. Jeromeviaph说道:

    https://pharmcanada.shop/# canadian pharmacy 1 internet online drugstore

  48. JamesSow说道:

    Продажа квартир https://novostroykihome.ru/ и недвижимости в Казани по выгодной стоимости на официальном сайте застройщика. Жилье в Казани: помощь в подборе и покупке новых квартир, цены за квадратный метр, фото, планировки.

  49. Williamfug说道:

    online pharmacy non prescription drugs: canadian pharmacy no prescription needed – canada pharmacy not requiring prescription

发表回复

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