Experiment 4 Soft interrupt signal and processing

"Operating System" Experiment Report

Name

Rhyme

student ID

10086

Experiment number

4

experiment name

Soft interrupt signal and processing

Purpose

1. Understand the basic principles of process soft interrupt communication in Linux system

2. Understand and be familiar with the basic concept and method of using soft interrupt signal to realize process communication in Linux system

3. Familiar with the relevant system call functions/library functions provided by Linux, and be able to use these functions

Experimental content

Experiment Topics ( 1 )

1. Use the sleep , pause , and alarm functions to set the alarm clock and display information at regular intervals. topic content

Experiment code, experiment process (snapshot in interface mode), experiment result (snapshot)

Read and run the program and answer the following questions:

Question 1: What is the meaning of the red part? How much time is left in the alarm at this time?

Meaning send SIGALRM signal, 10s left

Question 2: What is the meaning of the blue part?

suspend the process until a signal is caught

Question 3: What is the running result of the program, why is there no input content of the last printf statement in the running result?

Because the process was terminated without being woken up

Question 4: Please add a signal() function to the program to capture the signal and process the signal so that the program displays "I have been woken up".

#include<unistd.h>

#include<stdio.h>

#include<stdlib.h>

#include<signal.h>

int main(){

         int ret;

         alarm(50);

         sleep(30);

         right=alert(10);

         printf("%d\n",ret);

         signal(SIGALRM,alarm);

         pause();

    printf("I have been waken up.\n");

}

实验心得(遇到什么问题,如何解决)

看PPT和文档学习各个函数作用

实验题目(2

父进程通过kill()向子进程发送信号

实验代码、实验过程(界面方式下的抓图)、实验结果(抓图)

问题1:红色部分的含义是什么?

等待pid退出

问题2:若不添加红色程序会出现什么情况?

主进程会先于子进程结束

实验心得(遇到什么问题,如何解决)

实验题目(3

3.编制一段程序,使其实现进程的软中断通信。

实验代码、实验过程(界面方式下的抓图)、实验结果(抓图)

要求:使用系统调用fork()创建两个子进程,再用系统调用signal()让父进程捕捉键盘上来的中断信号(即按ctrl-c键);当捕捉到中断信号后,父进程用系统调用kill()向两个子进程发出信号,子进程捕捉到信号后分别输出下列信息后终止:

Child process 1 is killed by parent!

Child process 2 is killed by parent!

父进程等待两个子进程终止后,输出如下的信息后终止:

Parent process is killed!

#include <unistd.h>

#include <stdio.h>

#include <signal.h>

void waiting();

void stop();

void keep_alive();

int wait_mark;

int main(){

    int p1,p2;

         while((p1=fork())==-1);

         if(p1>0){

                 while((p2=fork())==-1);

                 if(p2>0){

                          printf("parent\n");

                          wait_mark=1;

                          signal(SIGINT, stop); 

                          waiting();

                          kill(p1, SIGINT);

                          kill(p2, SIGINT);

                          wait(0);

                          wait(0);

                          printf("parent process is killed children and exit!\n");

                          exit(0);

                 }else{

                          printf("p2\n");

                          //wait_mark=1;

                          signal(SIGINT, keep_alive);

                          pause();

                          printf("child process2 is killed by parent!\n");

                          exit(0);

             }

         }else{

                 printf("p1\n");

                 //wait_mark=1;

                 signal(SIGINT, keep_alive);

                 pause();

                 printf("child process1 is killed by parent!\n");

                 exit(0);

    }

}

void waiting(){

    while(wait_mark!=0);

}

void stop(){

    wait_mark=0;

}

void keep_alive(){

}

阅读并运行程序并回答以下问题:

问题1:红色部分的含义是什么?

接收Ctrl+C

问题2:程序的运行结果是什么?

实验心得(遇到什么问题,如何解决)

Guess you like

Origin blog.csdn.net/qq_54499870/article/details/127987214