操作系统原理实验报告最终版

  • 格式:doc
  • 大小:829.50 KB
  • 文档页数:28
exit(0);
}
实验2共享内存
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#define SHMKEY 75
#define K 1024
int shmid;
main()
{
int i, * pint;
char * addr;
extern char * shmat();
exit(0);
}
void ppdo()
{
kill(p1,SIGUSR1);
kill(p2,SIGUSR2);
}
void p1do()
{
printf("\nChild process p1 is killed by parent!\n");
exit(0);
}
void p2do()
{
printf("\nChild process p2 is killed by parent!\n");
实验后思考:
通过这次实验,深刻地了解到了管道通信和软中断通信的详细过程,深化了老师课堂上的讲解,对整个过程的把握也更加清晰了。
很值得的一次学习经历,做完实验,再画流程图,程序运行的细节熟悉于心,了如指掌。
七.指导教师评议
成绩等级
一、实验目的
1.了。
else printf("%s", s);
exit(0);
}
}
/*exp1-2.c*/
#include<stdio.h>
#include<stdlib.h>
#include<signal.h>
int p1,p2;
void main()
{
void ppdo();
void p1do();
void p2do();
2.掌握使用消息队列进行进程间通信的有关系统调用和编程方法。
3.掌握使用共享内存进行进程间通信的有关系统调用和编程方法。
二、实验内容
1.消息队列
使用系统调用msgget( )、msgsnd( )、msgrcv( )和msgctl( ),用消息队列机制实现客户进程和服务器进程间的通信。客户进程首先建立一个描述符为msgqid的消息队列,接着向服务器进程发送一个消息正文为自己的进程标识pid且类型为1的消息,然后接收来自服务器进程的消息,并在屏幕上显示:“Client receives a message from xxxx!”,其中“xxxx”为服务器进程的进程标识。服务器进程首先捕捉软中断信号(除不能捕捉的SIGKILL),若捕捉到时则调用函数cleanup( )删除消息队列,终止服务器进程。否则重复下列操作:接收所有类型为1的消息,并在屏幕上显示:“Server receives a message from xxxx!”,其中“xxxx”为客户进程的进程标识;然后服务器进程将客户进程的进程标识作为返回消息的类型,而将自己的进程标识作为消息正文发送给客户进程。
signal(SIGINT,ppdo);
p1=fork();
if(p1==0)
{
signal(SIGUSR1,p1do);
for(;;);
}
else {
p2=fork();
if(p2==0) {
signal(SIGUSR2,p2do);
for(;;);
}
}
wait(0);
wait(0);
printf("\nParent process is killed!\n");
三、实验要求
1.根据实验内容编写C程序。
2.上机调试程序。
3.记录并分析程序运行结果。
四、程序说明和程序流程图
实验1管道通信——所涉及的流程图:
实验2软中断信号——所涉及的流程图:
五、程序代码
/*expe1_1.c*/
#include <stdio.h>
void main( )
{
int i, r, p1, p2, fd[2];
2.软中断通信
使用系统调用fork( )创建2个子进程p1和p2,在父进程中使用系统调用signal( )捕捉来自键盘上的软中断信号SIGINT(即按Ctrl-C),当捕捉到软中断信号SIGINT后,父进程使用系统调用kill( )分别向2个子进程发出软中断信号SIGUSR1和SIGUSR2,子进程捕捉到信号后分别输出信息“Child process p1 is killed by parent!”和“Child process p2 is killed by parent!”后终止。而父进程等待2个子进程终止后,输出信息“Parent process is killed!”后终止。
printf("Child process P1! \n");
write(fd[1], buf, 50); /*把buf中的50个字符写入管道*/
sleep(5); /*睡眠5秒,让父进程读*/
lockf(fd[1], 0, 0); /*释放管道写入端*/
exit(0); /*关闭P1*/
}
else /*从父进程返回,执行父进程*/
extern cleanup();
for (i=0;i<20;i++)
signal(i,cleanup);
msgqid=msgget(MSGKEY, 0777|IPC_CREAT);
for (;;)
{ msgrcv(msgqid,&msg, 256,1,0);
pint=(int*)msg.mtext;
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define MSGKEY 75
struct msgform
{ long mtype;
char mtext[256];}msg;
int msgqid;
main()
{
int i,pid,*pint;
{
while ((p2=fork())==-1); /*创建子进程P2,失败时循环*/
if (p2==0) /*从子进程P2返回,执行子进程P2 */
{
lockf(fd[1], 1, 0); /*锁定写入端*/
sprintf(buf, "Child process P2 is sending messages! \n");
if ((r=read(fd[0], s, 50))== -1)
printf("cannot read pipe! \n");
else printf("%s", s);
wait(0);
if ((r=read(fd[0], s, 50))== -1)
printf("cannot read pipe! \n");
实验1消息队列流程图
五、程序代码
实验1消息队列
/* msg_client.c */
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define MSGKEY 75
struct msgform
{
long mtype;
char mtext[256];
实验2软中断通信运行结果截图
实验2软中断通信结果分析
先预设中断信号SIGINT,再先后创建子进程P1和P2,预设中断信号SIGUSR1,SIGUER2,当我们按下“Ctrl+C”时,父进程发出中断信号SIGUSR1和SIGUSR2,通知子进程P1和P2,子进程捕捉到信号后分别输出相应的信息后,终止,最后输出“Parent process is killed!”后终止。
shmid = shmget(SHMKEY, 8 * K, 0777);
addr = shmat (shmid,0,0);
pint = (int *)addr;
while ( * pint==0 )
for (i=0; i<256; *pint++)
printf("%d\n", *pint++);
}
#include<sys/types.h>
}
main()
{
struct msgform msg;
int msgqid,pid,*pint; /*文件主同组用户其他用户rwxrwxrwx */
msgqid=msgget(MSGKEY,0777); /* rw-rw-rw- */
pid=getpid();
pint=(int*)msg.mtext;
char buf[50], s[50];
pipe(fd); /*父进程建立管道*/
while ((p1=fork())==-1); /*创建子进程P1,失败时循环*/
if (p1==0) /*由子进程P1返回,执行子进程P1 */
{
lockf(fd[1], 1, 0); /*加锁锁定写入端*/
sprintf(buf, "Child process P1 is sending messages! \n");
pid=*pint;
printf("server: receive from pid %d\n",pid);
msg.mtype=pid;