西南交通大学操作系统实验二——软中断通信实验

实验目的

本实验要求学生了解什么是信号,掌握软中断的基本原理;

掌握中断信号的使用、进程的创建以及系统计时器的使用。

通过对本实验的学习,学生能够学会进程的创建方法,更能加深对Linux中的信号机制的认识,并会使用软中断信号来实现进程间的通信。


实验内容

实验 1:编译并运行程序test2.c,当按下Crtl+\组合键时,打印出子进程结束的信息,最后打印出父进程结束的信息。

创建一个子进程; 子进程分别等待信号16,如果收到信号则显示结束信息,并发出结束信号;

父进程等待SIGQUIT信号,如果收到信号则向子进程发送信号16,接着等子进程结束,如果都结束了则显示结束信息,并退出进程。

实验2:编写两种三个进程通信情况。

由一个父进程创建两个子进程,之后通过终端输入Crtl+\组合键向父进程发送软中断信号,终止两个子进程以及父进程。

由一个父进程创建一个子进程,之后该子进程再创建一个孙进程,通过终端输入Crtl+\组合键向父进程发送软中断信号,依次终止孙进程、子进程、父进程。


本文章仅提供实验2的两种代码。


代码1,由一个父进程创建两个子进程,之后通过终端输入Crtl+\组合键向父进程发送软中断信号,终止两个子进程以及父进程:

#include<cstdio>
#include<cstdlib>
#include<csignal>
#include<unistd.h>
#include <sys/wait.h>
#include "iostream"

using namespace std;

void waiting();
void stop();
int wait_mark;

int main()
{
    pid_t p1, p2;
    p1 = fork();
    if (p1 != 0) {
        lockf(1, 1, 0);
        cout << "Parent Process " << getpid() << endl;
        lockf(1, 0, 0);
        wait_mark = 1;
        ::signal(SIGQUIT, reinterpret_cast<__sighandler_t>(stop));
        p2 = fork();
        if (p2 == 0) {
            lockf(1, 1, 0);
            cout << "Child Process " << getpid() << " created by " << getppid() << endl;
            lockf(1, 0, 0);

            ::signal(SIGQUIT, SIG_IGN);
            wait_mark = 1;
            ::signal(16, reinterpret_cast<__sighandler_t>(stop));
            waiting();

            lockf(1, 1 , 0);
            cout << "Child Process " << getpid() << " is killed by parent " << getppid() << endl;
            lockf(1, 0, 0);
            ::exit(0);
        }
        waiting();
        kill(p1, 16);
        wait(nullptr);
        kill(p2, 16);
        wait(nullptr);
        lockf(1, 1, 0);
        cout << "parent process if killed" << endl;
        lockf(1, 0, 0);
        ::exit(0);
    } else if (p1 == 0) {
        lockf(1, 1, 0);
        cout << "Child Process " << getpid() << " created by " << getppid() << endl;
        lockf(1, 0, 0);

        ::signal(SIGQUIT, SIG_IGN);
        wait_mark = 1;
        ::signal(16, reinterpret_cast<__sighandler_t>(stop));
        waiting();

        lockf(1, 1 , 0);
        cout << "Child Process " << getpid() << " is killed by parent " << getppid() << endl;
        lockf(1, 0, 0);
        ::exit(0);
    }
    return 0;
}
void waiting( )
{
    while (wait_mark != 0);
}
void stop()
{
    wait_mark=0;
}

代码2,由一个父进程创建一个子进程,之后该子进程再创建一个孙进程,通过终端输入Crtl+\组合键向父进程发送软中断信号,依次终止孙进程、子进程、父进程:


#include<cstdio>
#include<cstdlib>
#include<csignal>
#include<unistd.h>
#include <sys/wait.h>
#include "iostream"

using namespace std;

void waiting();
void stop();
int wait_mark;

int main()
{
    pid_t p1, p2;
    p1 = fork();
    if (p1 != 0) {
        lockf(1, 1, 0);
        cout << "Parent Process " << getpid() << endl;
        lockf(1, 0, 0);
        wait_mark = 1;
        ::signal(SIGQUIT, reinterpret_cast<__sighandler_t>(stop));
        p2 = fork();
        if (p2 == 0) {
            lockf(1, 1, 0);
            cout << "Child Process " << getpid() << " created by " << getppid() << endl;
            lockf(1, 0, 0);

            ::signal(SIGQUIT, SIG_IGN);
            wait_mark = 1;
            ::signal(16, reinterpret_cast<__sighandler_t>(stop));
            waiting();

            lockf(1, 1 , 0);
            cout << "Child Process " << getpid() << " is killed by parent " << getppid() << endl;
            lockf(1, 0, 0);
            ::exit(0);
        }
        waiting();
        kill(p1, 16);
        wait(nullptr);
        kill(p2, 16);
        wait(nullptr);
        lockf(1, 1, 0);
        cout << "parent process if killed" << endl;
        lockf(1, 0, 0);
        ::exit(0);
    } else if (p1 == 0) {
        lockf(1, 1, 0);
        cout << "Child Process " << getpid() << " created by " << getppid() << endl;
        lockf(1, 0, 0);

        ::signal(SIGQUIT, SIG_IGN);
        wait_mark = 1;
        ::signal(16, reinterpret_cast<__sighandler_t>(stop));
        waiting();

        lockf(1, 1 , 0);
        cout << "Child Process " << getpid() << " is killed by parent " << getppid() << endl;
        lockf(1, 0, 0);
        ::exit(0);
    }
    return 0;
}
void waiting( )
{
    while (wait_mark != 0);
}
void stop()
{
    wait_mark=0;
}

注意:本系列文章代码均为C++代码,使用gcc编译会报错,应当使用g++编译。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jellyfish Knight

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值