操作系统实验报告进程通信管理
- 格式:docx
- 大小:99.56 KB
- 文档页数:8
2.给父进程中 断信号, 父进程开始 终止子进程,但是由于 添加了
signal(SIGINT, SIG_IGN);
2.
编制一段程序,实现进程的管道通信。使用系统调用
Child
Child 2 is sending a message!
而父进程则从管道中读出来自两个子进程的信息,显示在屏幕上。
漳州师范学院
实验报告
班 级
同组人
课程名称:操作系统
实验题目:进程通信管理
实验目的与要求
1.加深理解进程并发执行的概念,认识多进程并发执行的实质;
2.观察进程争夺资源的现象,分析其进程和原因,学习解决进程互斥 的方法;
3.了解Linux系统中多进程之间通过软中断通信以及管道通信基本原 理和应用方法;
实 验 环 境 的 配 置
{
while(wait_mark!=0);
}
void stop()
{
wait_mark=0;
}
1
理解分析:1.先创建父进程,由父进程分别产生子进程1和子进程2,依次输出pl,
p2,pare nt。
2.给父进程中断信号,父进程终止子进程,运行stop函数wait_mark=O;跳
出wait ing函数,输出pare nt process is killed!
PC兼容机。Window xp以上操作系统
第1页
闽南师范大学
1.软中断通信
编制一段程序,使用系统调用fork()创建两个子进程,再用系统调用signal()让父进程捕捉键盘上来的中断信号(即按ctrl+c键),当捕捉到中断信 号后,父进程用系统调用kill()向两个子进程发出信号,子进程捕捉到信 号后,分别输出下列信息后终止:
}
else
{//子进程2
printf("p2\n");
wait_mark=1;
signal(17,stop);
waiting();
lockf(1,1,0);
printf("child process 2 is killed by parent!\n"); lockf(1,0,0);
exit(0);
}
Pl
P2parent child process1iskilledbyparent!£hiIdprocess2Iskilledby parent*parentprocess iskilled!
[ractOldcaLhostroot1#rccshiy.e-dshiv
理解分析:1.先创建父进程,由父进程分别产生子进程1和子进程2,依次输出p1,
{//父进程
prin tf("pare nt\n“);
wait_mark=1;//将等待标志设置为1
signal(SIGINT,stop);
waiting();
kill(p1,16);〃
kill(p2,17);
wait(0);
wait(0);
printf("parent process is killed!\n");//父进程等待两个子进程终止后,输出一下信息后终止exit(0);
}
else
{//子进程1
printf("p1\n");
wait_mark=1;
signal(16,stop);
waiting。;
lockf(1,1,0);
printf("child process 1 is killed by parent!\n");
lockf(1,0,0);
exitg()
[rodtfilDcalhiDStroot]ff gcc shiyB£ shiy [ruotfiloca IhiDStToot] #.a/sh iy
Pl
P2
parent child process2iskilledby par&nt!child process 1 iskilledby parent! parent process iskilled!
[roatfilDcaLhost root]#./shiy
Pl
P2
parentchild process 1 iskilledby parent! child process2iskilledby parent!parentprocess iskilled!
[raatOlDcalhost root]#»/shiy
2•修改程序,查看修改前结果跟修改后结果的区别,分析原因。
else {
sig nal(SIGINT, SIG」GN); /* add this line */
sig nal(17,stop);
}
else {
signal(SIGINT, SIG」GN); /* add this line */
sig nal(16,stop);
while((pid 1=fork())==-1);
if(pid1==0)〃对子进程1的操作
{
prin tf("p1\n");
lockf(fd[1],1,0);//对管道写入端进行软锁
spri ntf(OutPipe,"Child1is sen di ng a message!");//Outipe数组存放"Child.." write(fd[1],OutPipe,50);〃把OutPipe的内容写入管道sleep(1);
child process1is killed by pare nt!
child process 2 is killed by pare nt!
父进程等待两个子进程终止后,输出一下信息后终止:
pare nt process is killed!
程序:
#include <unistd.h>
#include <stdio.h>
程序:
#include<unistd.h>
#include<stdio.h>
#include<signal.h>
int pid1,pid2;
main()
{
int fd[3];
char OutPipe[100],l nPipe[100];
pipe(fd);//fd[O]存管道的读入端,fd[1]是管道的写入端
#include <signal.h>
void waiting。;
void stop();
int wait_mark;
int main()
{
int p1,p2;
while((p1=fork())==-1);
if(p1>0) //父进程产生子进程1
{
while((p2=fork())==-1);
if(p2>0)〃父进程产生了一个子进程2