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

/ 9,843评论 / 36330阅读 / 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. Felipelam说道:

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

  2. Donaldkenty说道:

    indian pharmacy online: mail order pharmacy india – top 10 pharmacies in india

  3. DavidTaK说道:

    cheap canadian pharmacy online canadian pharmacy sarasota online pharmacy canada

  4. Williamfug说道:

    online pharmacy india: Online medicine home delivery – best online pharmacy india

  5. DavidTaK说道:

    indian pharmacy paypal top 10 pharmacies in india mail order pharmacy india

  6. Felipelam说道:

    http://pharmmexico.online/# medicine in mexico pharmacies

  7. Williamfug说道:

    mexican border pharmacies shipping to usa: medication from mexico pharmacy – best online pharmacies in mexico

  8. Williamfug说道:

    buy medicines online in india: buy prescription drugs from india – Online medicine home delivery

  9. Felipelam说道:

    http://pharmmexico.online/# mexico drug stores pharmacies

  10. DavidTaK说道:

    indianpharmacy com best online pharmacy india indian pharmacy

  11. Elmersoalf说道:

    Sildenafil 100mg price Cheap Viagra 100mg sildenafil over the counter

  12. Charlesvox说道:

    http://kamagra.win/# Kamagra 100mg price

  13. Charlesvox说道:

    https://cenforce.pro/# cenforce.pro

  14. Jamesexite说道:

    cheapest cenforce: Cenforce 100mg tablets for sale – Cenforce 100mg tablets for sale

  15. Jamesexite说道:

    sildenafil over the counter: viagras.online – Cheap Sildenafil 100mg

  16. Jamesexite说道:

    Buy Vardenafil online: buy Levitra over the counter – Buy Vardenafil 20mg

  17. Charlesvox说道:

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

  18. Chongtek说道:

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

  19. Jamesexite说道:

    Kamagra tablets: kamagra – Kamagra Oral Jelly

  20. Charlesvox说道:

    http://viagras.online/# best price for viagra 100mg

  21. Tylerreunk说道:

    http://cialist.pro/# cialis for sale

  22. Tylerreunk说道:

    http://cenforce.pro/# cheapest cenforce

  23. Elmersoalf说道:

    Kamagra 100mg price Kamagra 100mg price Kamagra tablets

  24. Georgefrege说道:

    Kamagra 100mg price: kamagra oral jelly – Kamagra 100mg price

  25. Charlesvox说道:

    http://cenforce.pro/# cenforce for sale

  26. Elmersoalf说道:

    buy viagra here cheapest viagra Sildenafil Citrate Tablets 100mg

  27. PhillipMEP说道:

    Заказать SEO продвижение сайтов https://seoshnikigo.ru/ в ТОП поисковых систем Яндекс и Google в Москве, оплата за результат и по факту. Кейсы, стратегии продвижения, скидки и акции, индивидуальный подход

  28. Charlesvox说道:

    http://viagras.online/# Viagra without a doctor prescription Canada

  29. Jamesexite说道:

    buy kamagra online usa: Kamagra Oral Jelly – buy kamagra online usa

  30. DavidBiolo说道:

    http://viagras.online/# best price for viagra 100mg

  31. DavidBiolo说道:

    http://levitrav.store/# Levitra tablet price

  32. Jamesexite说道:

    Cenforce 100mg tablets for sale: cenforce for sale – cheapest cenforce

  33. Jamesexite说道:

    Kamagra Oral Jelly: sildenafil oral jelly 100mg kamagra – Kamagra tablets

  34. Charlesvox说道:

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

  35. DavidBiolo说道:

    http://cenforce.pro/# cenforce.pro

  36. DavidBiolo说道:

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

  37. Georgefrege说道:

    Buy Cenforce 100mg Online: cenforce.pro – Buy Cenforce 100mg Online

  38. Elmersoalf说道:

    Buy generic Levitra online Levitra generic price Buy Vardenafil 20mg

  39. Jamesexite说道:

    Tadalafil Tablet: buy cialis online – Cialis 20mg price

  40. Elmersoalf说道:

    Buy generic Levitra online buy Levitra over the counter Levitra 20 mg for sale

  41. Charlesvox说道:

    http://levitrav.store/# Levitra tablet price

  42. Elmersoalf说道:

    cheapest cenforce buy cenforce buy cenforce

  43. RichardOrext说道:

    Продвижение сайтов в поисковых системах https://seoshnikiguru.ru/ с гарантией результата. SEO продвижение сайтов в ТОП-10 Яндекс, заказать поисковое сео продвижение, раскрутка веб сайта в Москве.

  44. Elmersoalf说道:

    Tadalafil price Cialis 20mg price in USA п»їcialis generic

  45. Georgefrege说道:

    buy cialis pill: Generic Tadalafil 20mg price – Buy Cialis online

  46. Charlesvox说道:

    http://kamagra.win/# п»їkamagra

  47. Charlesvox说道:

    https://kamagra.win/# п»їkamagra

  48. Jamesexite说道:

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

  49. Jamesexite说道:

    Buy Viagra online cheap: Buy Viagra online – Buy generic 100mg Viagra online

  50. Tylerreunk说道:

    http://cialist.pro/# п»їcialis generic

发表回复

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