非常感谢各位的点赞支持!
大家的评论都收到!感谢感谢!
姓名: | 学号: | 专业年级: | 班级: | |
分组: | 实验室: | 指导教师: | 实验日期: | |
实验的准备阶段 (指导教师填写) | 课程名称 | 计算机操作系统 | ||
实验名称 | 进程通信 | |||
实验目的 |
| |||
实验内容 |
使用系统调用 pipe()函数建立一条管道线,两个子进程分别向管道各写一句话。 Child process 1 is sending a message! Child process 2 is sending a message! 而父进程则从管道中读出来自于两个子进程的信息,显示在屏幕上。 要求∶父进程先接收子进程P1发来的消息,然后再接收子进程 P2发来的消息,
Child process 1 is killed by parent!! Child process 2 is killed by parent!! 父进程调用wait()函数等待两个子进程终止后,输出以下信息后终止。 Parent process is killed! 多运行几次编写的程序,简略分析出现不同结果的原因。 | |||
实验类型 (打R) | ☑验证性 □演示性 ☑设计性 □综合性 | |||
实验的重点、难点 | pipe、kill、signal、lockf等函数的使用 | |||
实验环境 | VMWare、RedHat Linux | |||
实验的实施阶段 | 实验步骤及实验结果 | 任务一:管道通信的程序 创建管道:使用 pipe() 系统调用创建一个管道,它返回两个文件描述符,一个用于写入,一个用于读取。 创建子进程:使用 fork() 系统调用创建一个子进程,子进程将会继承父进程的文件描述符,包括管道的文件描述符。 父子进程通信:父进程通过写入管道的文件描述符向管道中写入数据,子进程通过读取管道的文件描述符从管道中读取数据,实现了父子进程之间的通信。 代码如下: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> int main() { int fd[2]; pid_t pid; char buf[100]; if (pipe(fd) < 0) { perror("pipe error"); exit(1); } pid = fork(); if (pid < 0) { perror("fork error"); exit(1); } else if (pid > 0) { // parent process close(fd[1]); // close the write end of the pipe read(fd[0], buf, sizeof(buf)); printf("Parent process received message from Child process 1: %s\n", buf); wait(NULL); // wait for the first child process to finish read(fd[0], buf, sizeof(buf)); printf("Parent process received message from Child process 2: %s\n", buf); close(fd[0]); // close the read end of the pipe } else { // child process close(fd[0]); // close the read end of the pipe char* message1 = "Child process 1 is sending a message!"; char* message2 = "Child process 2 is sending a message!"; write(fd[1], message1, strlen(message1) + 1); write(fd[1], message2, strlen(message2) + 1); close(fd[1]); // close the write end of the pipe exit(0); } return 0; } 任务二:信号处理的程序 注册信号处理函数:使用 signal() 函数注册信号处理函数,指定当接收到特定信号时应该执行的处理函数。 接收信号:程序运行时可以通过发送信号(例如使用 kill 命令)来触发注册的信号处理函数。 处理信号:当接收到指定的信号时,注册的信号处理函数会被调用,程序会执行相应的处理操作。 代码如下: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> #include <sys/wait.h> void sigint_handler(int signo) { printf("Received SIGINT signal from keyboard\n"); } void sigchld_handler(int signo) { if (signo == SIGCHLD) { printf("Child process is killed by parent!\n"); exit(0); } } int main() { pid_t pid1, pid2; int status; if ((pid1 = fork()) < 0) { perror("fork error"); exit(1); } else if (pid1 == 0) { // first child process signal(SIGCHLD, sigchld_handler); while(1) { // do something } } else { if ((pid2 = fork()) < 0) { perror("fork error"); exit(1); } else if (pid2 == 0) { // second child process signal(SIGCHLD, sigchld_handler); while(1) { // do something } } else { // parent process signal(SIGINT, sigint_handler); sleep(1); // wait for the user to send SIGINT signal kill(pid1, 16); kill(pid2, 17); wait(&status); printf("Parent process is killed!\n"); exit(0); } } return 0; } 对于多次运行产生不同结果的考虑: 多次运行上述程序可能会出现不同的结果,这是由于进程调度的随机性导致的。在父进程发送信号给子进程后,子进程可能会以不同的顺序响应信号,导致输出的顺序不同。此外,由于信号的异步性,子进程在接收到信号后的处理速度也可能不同,也会导致输出的顺序不同。这些都是导致不同结果的原因。 | ||
实验结果的处理阶段 | 实验结果的分析与总结 | 通过对这两个任务的实现,我学到了以下内容: 1.进程间通信的基本原理:了解了使用管道进行进程间通信的基本原理,包括创建管道、父子进程通信等。 2.信号处理的基本原理:学会了如何注册信号处理函数,以及如何处理接收到的信号。 3.多进程编程的基础知识:通过实现这两个任务,也学到了一些关于多进程编程的基础知识,比如创建子进程、进程间通信等 |