操作系统实验二进程管理.docx
- 文档编号:16720134
- 上传时间:2023-07-16
- 格式:DOCX
- 页数:15
- 大小:16.99KB
操作系统实验二进程管理.docx
《操作系统实验二进程管理.docx》由会员分享,可在线阅读,更多相关《操作系统实验二进程管理.docx(15页珍藏版)》请在冰点文库上搜索。
操作系统实验二进程管理
操作系统进程管理实验
实验题目:
(1)进程的创建编写一段程序,使用系统调用fork()创建两个子进程。
当此程序运行时,在系统中有一个父进程和两个子进程活动。
让每一个进程在屏幕上显示一个字符:
父进程显示字符“a”;子进程分别显示字符“b”和字符“c”。
试观察记录屏幕上的显示结果,并分析原因。
(2)进程的控制修改已编写的程序,将每个进程输出一个字符改为每个进程输出一句话,在观察程序执行时屏幕上出现的现象,并分析原因。
(3)编制一段程序,使其实现进程的软中断通信。
要求:
使用系统调用fork()创建两个子进程,再用系统调用signal()让父进程捕捉键盘上来的中断信号(即按Del键);当捕捉到中断信号后,父进程调用系统调用kill()向两个子进程发出信号,子进程捕捉到信号后分别输出下列信息后终止:
Childprocess1iskilledbyparent!
Childprocess2iskilledbyparent!
父进程等待两个子进程终止后,输出如下的信息后终止:
Parentprocessiskilled!
在上面的程序中增加语句signal(SIGINT,SIG_IGN)和signal(SIGQUIT,SIG_IGN),观察执行结果,并分析原因。
(4)进程的管道通信编制一段程序,实现进程的管道通信。
使用系统调用pipe()建立一条管道线;两个进程P1和P2分别向管道各写一句话:
Child1issendingamessage!
Child2issendingamessage!
而父进程则从管道中读出来自于两个子进程的信息,显示在屏幕上。
要求父进程先接收子进程P1发来的消息,然后再接收子进程P2发来的消息。
实验源程序及报告:
(1)、进程的创建
#include
intmain(intargc,char*argv[])
{
intpid1,pid2;/*forkfirstchildprocess*/
if((pid1=fork())<0)
{
printf("ProcessCreateFailed!
");
exit(-1);
}
if((pid1=fork())==0)
{
printf("b\n");
}
/*forksecondchildprocess*/
if((pid2=fork())<0)
{
printf("ProcessCreateFailed!
");
exit(-1);
}
if((pid2=fork())==0)
{
printf("c\n");
}
/*parentprocess*/
else
{
wait(NULL);
printf("a\n");
exit(0);
}
return0;
}
(2)、进程的控制
#include
intmain(intargc,char*argv[])
{intpid1,pid2;
/*forkfirstchildprocess*/
if((pid1=fork())<0)
{
printf("ProcessCreateFailed!
");
exit(-1);
}
if((pid1=fork())==0)
{
printf("ThisismyUnixOSprogram!
\n");
}
/*forksecondchildprocess*/
if((pid2=fork())<0)
{
printf("ProcessCreateFailed!
");
exit(-1);
}
if((pid2=fork())==0)
{
printf("ThisisthesecondChildprocess!
\n");
}
/*parentprocess*/
else
{
wait(NULL);
printf("ThisistheParentprocess\n");
exit(0);
}
return0;
}
(3)编制一段程序,使其实现进程的软中断通信。
#include
#include
#include
#include
intwait_flag;voidstop();
main()
{
intpid1,pid2;signal(3,stop);
while((pid1=fork())==-1);
if((pid1=fork())>0)
{
while((pid2=fork())==-1);
if((pid2=fork())>0)
{
wait_flag=1;signal(SIGINT,stop);
sleep(5);
kill(pid1,16);
kill(pid2,17);
wait(0);
wait(0);
printf("\nParentprocessiskilled.\n");
exit(0);
}
else
{
wait_flag=1;
signal(17,stop);
printf("\nChildprocess2iskilledbyparent.\n");
exit(0);
}
}
else
{
wait_flag=1;
signal(16,stop);
printf("\nChildprocess1iskilledbyparent.\n");
exit(0);
}
}
voidstop()
{
wait_flag=0;
}
(4)进程的管道通信
#include
#include
#include
intpid1,pid2;
intmain()
{
intfd[2];
charOutPipe[100],InPipe[100];
pipe(fd);
while((pid1=fork())==-1);
if(pid1==0)
{
lockf(fd[1],1,0);
sprintf(OutPipe,"\nChildprocess1issendingmessage!
\n");
write(fd[1],OutPipe,50);
sleep(5);
lockf(fd[1],0,0);
exit(0);
}
else
{
while((pid2=fork())==-1);
if(pid2==0)
{
lockf(fd[1],1,0);
sprintf(OutPipe,"\nChildprocess2issendingmessage!
\n");
write(fd[1],OutPipe,50);
sleep(3);
lockf(fd[1],0,0);
exit(0);
}
else
{
wait(0);
read(fd[0],InPipe,50);
printf("%s\n",InPipe);
wait(0);
read(fd[0],InPipe,50);
printf("%s\n",InPipe);
exit(0);
}
}
return0;
}
三、程序注释:
(1)进程的创建
源代码:
#include
intmain(intargc,char*argv[])
{
intpid1,pid2;
/*调用fork函数创建进程1*/
if((pid1=fork())<0)
{/*fork()返回值为负数,则创建进程失败*/
printf("ProcessCreateFailed!
");
exit(-1);
}
/**
/*fork()返回值为0,则创建子进程成功且当前进程为子进程
**/
if((pid1=fork())==0)
{
printf("b\n");//子进程1输出’b’;
}
/**
/*创建子进程2
***/
if((pid2=fork())<0)/*fork()返回值为负数,则创建进程失败*/
{
printf("ProcessCreateFailed!
");
exit(-1);
}
/**
/*fork()返回值为0,则创建子进程成功且当前进程为子进程
**/
if((pid2=fork())==0)
{
printf("c\n");//子进程2输出’c’;
}
/*fork()返回值大于0,则当前进程为父进程*/
else
{
wait(NULL);
printf("a\n");//父进程输出’a’;
exit(0);
}
return0;
}
(2)进程的控制
源代码:
#include
intmain(intargc,char*argv[])
{
intpid1,pid2;
/*调用fork函数创建进程1*/
if((pid1=fork())<0)
{/*fork()返回值为负数,则创建进程失败*/
printf("ProcessCreateFailed!
");
exit(-1);
}
/**
/*fork()返回值为0,则创建子进程成功且当前进程为子进程
**/
if((pid1=fork())==0)
{
printf("ThisismyUnixOSprogram!
\n");//子进程1输出句子;
}
/**
/*创建子进程2
***/
if((pid2=fork())<0)/*fork()返回值为负数,则创建进程失败*/
{
printf("ProcessCreateFailed!
");
exit(-1);
}
/**
/*fork()返回值为0,则创建子进程成功且当前进程为子进程
**/
if((pid2=fork())==0)
{
printf("ThisisthesecondChildprocess!
\n");//子进程2输出句子;
}
/*fork()返回值大于0,则当前进程为父进程*/
else
{
wait(NULL);
printf("ThisistheParentprocess\n");//父进程输出句子;
exit(0);
}
return0;
}
(3)编制一段程序,使其实现进程的软中断通信。
源代码:
(软中断用Ctrl+|实现)
#include
#include
#include
#include
intwait_flag;
voidstop();//stop()是自己定义的Signal()触发的自定义函数
main()
{
intpid1,pid2;//定义两个进程号参数
signal(3,stop);//Signal()触发软中断
while((pid1=fork())==-1);//程序等待成功创建子进程事件的发生
if((pid1=fork())>0)
{
while((pid2=fork())==-1);
if((pid2=fork())>0)
{/*当前进程为父进程,父进程发出两个软中断信号Kill子进程*/
wait_flag=1;
signal(SIGINT,stop);
sleep(5);
kill(pid1,16);
kill(pid2,17);
wait(0);//等待子进程死信号
wait(0);
printf("\nParentprocessiskilled.\n");//接收到子进程死信号后,杀死父进程
exit(0);
}
else
{/**
/*当前进程为子进程,则发送子进程Kill信号,杀死该子进程2
**/
wait_flag=1;
signal(17,stop);
printf("\nChildprocess2iskilledbyparent.\n");
exit(0);
}
}
else{/**
/*当前进程为子进程,则发送子进程Kill信号,杀死该子进程1
**/
wait_flag=1;
signal(16,stop);
printf("\nChildprocess1iskilledbyparent.\n");
exit(0);
}
}
voidstop()
{/**
/*自定义函数,供signal()调用
**/
wait_flag=0;
}
(4)进程的管道通信
源代码:
#include
#include
#include
intpid1,pid2;//定义两个进程号参数
intmain()
{
intfd[2];/*定义一个数组作为sys_pipe()的参数*/
charOutPipe[100],InPipe[100];
pipe(fd);/*调用sys_pipe()创建管道线*/
while((pid1=fork())==-1);
if(pid1==0)
{/**
/**根据sys_pipe()函数的定义fd[1]用于向管道写数据
*/
lockf(fd[1],1,0);/*锁定管道写入端fd[1]*/
/*进程1向管道写入要输出的句子*/
sprintf(OutPipe,"\nChildprocess1issendingmessage!
\n");
write(fd[1],OutPipe,50);
sleep(5);
lockf(fd[1],0,0);
exit(0);
}
else
{
while((pid2=fork())==-1);
if(pid2==0)
{
lockf(fd[1],1,0);/*锁定管道写入端fd[1]*/
/*进程2向管道写入要输出的句子*/
sprintf(OutPipe,"\nChildprocess2issendingmessage!
\n");
write(fd[1],OutPipe,50);/*调用pipe_write()函数写管道*/
sleep(3);
lockf(fd[1],0,0);
exit(0);
}
else
{
wait(0);
read(fd[0],InPipe,50);/*调用pipe_read()函数读管道*/
printf("%s\n",InPipe);
wait(0);
read(fd[0],InPipe,50);/*调用pipe_read()函数读管道*/
printf("%s\n",InPipe);
exit(0);
}
}
return0;
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 操作系统 实验 进程 管理