109021060黄庆操作系统实验报告.docx
《109021060黄庆操作系统实验报告.docx》由会员分享,可在线阅读,更多相关《109021060黄庆操作系统实验报告.docx(21页珍藏版)》请在冰豆网上搜索。
![109021060黄庆操作系统实验报告.docx](https://file1.bdocx.com/fileroot1/2022-11/18/a7f1060f-36ec-4b29-ac96-db78c3d5f249/a7f1060f-36ec-4b29-ac96-db78c3d5f2491.gif)
109021060黄庆操作系统实验报告
操作系统原理
实验报告
班级计算机092班
学号_109021040__
姓名_吴峰_____
浙江科技学院信息与电子工程学院
2010-2011学年第2学期
操作系统原理实验报告
班级_计算机092__学号__109021060___姓名__黄庆__
实验1进程管理
一、实验目的
1.弄清进程和程序的区别,加深对进程概念的理解。
2.了解并发进程的执行过程,进一步认识并发执行的实质。
3.掌握解决进程互斥使用资源的方法。
二、实验内容
1.管道通信
使用系统调用pipe()建立一个管道,然后使用系统调用fork()创建2个子进程p1和p2。
这2个子进程分别向管道中写入字符串:
“Childprocessp1issendingmessage!
”和“Childprocessp2issendingmessage!
”,而父进程则从管道中读出来自两个子进程的信息,并显示在屏幕上。
2.软中断通信
使用系统调用fork()创建2个子进程p1和p2,在父进程中使用系统调用signal()捕捉来自键盘上的软中断信号SIGINT(即按Ctrl-C),当捕捉到软中断信号SIGINT后,父进程使用系统调用kill()分别向2个子进程发出软中断信号SIGUSR1和SIGUSR2,子进程捕捉到信号后分别输出信息“Childprocessp1iskilledbyparent!
”和“Childprocessp2iskilledbyparent!
”后终止。
而父进程等待2个子进程终止后,输出信息“Parentprocessiskilled!
”后终止。
三、实验要求
1.根据实验内容编写C程序。
2.上机调试程序。
3.记录并分析程序运行结果。
四、程序说明和程序流程图
1.管道通信
2.软中断通信
五、程序代码
1.管道通信
#include#include
#include
main()
{
inti,r,P1,P2,fd[2];
charbuf[50],s[50];
pipe(fd);
while((P1=fork())==-1);
if(P1==0)
{lockf(fd[1],1,0);sprintf(buf,"childprocessP1issendingmessages!
\n");
printf("childprocessP1!
\n");
write(fd[1],buf,50);
sleep(5);
lockf(fd[1],0,0);
exit(0);
}
else
{while((P2=fork())==-1);if(P2==0)
{
lockf(fd[1],1,0);
sprintf(buf,"childprocessP2issendingmessages\n");
printf("childprocessP2!
\n");
write(fd[1],buf,50);
sleep(5);
lockf(fd[1],0,0);
exit(0);
}
wait(0);
if(r=read(fd[0],s,50)==-1)
printf("can'treadpipe\n");
elseprintf("%s\n",s);
wait(0);
if(r=read(fd[0],s,50)==-1)
printf("can'treadpipe\n");
elseprintf("%s\n",s);
exit(0);
}
}
2.软中断通信
#include
#include
#include
intp1,p2;
voidmain()
{voidppdo();
voidp1do();
voidp2do();
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("\nParentprocessiskilled!
\n");
exit(0);
}
voidppdo()
{kill(p1,SIGUSR1);
kill(p2,SIGUSR2);
}
voidp1do()
{printf("\nChildprocessp1iskilledbyparent!
\n");
exit(0);
}
voidp2do()
{printf("\nChildprocessp2iskilledbyparent!
\n");
exit(0);
}
六、程序运行结果及分析
1.运行结果如下图:
2.运行结果如下图:
操作系统原理实验报告
班级_计算机092__学号__109021060___姓名__黄庆__
实验2进程通信
一、实验目的
1.了解进程间通信IPC的三种方式:
消息队列、共享内存和信号量。
2.掌握使用消息队列进行进程间通信的有关系统调用和编程方法。
3.掌握使用共享内存进行进程间通信的有关系统调用和编程方法。
二、实验内容
1.消息队列
使用系统调用msgget()、msgsnd()、msgrcv()和msgctl(),用消息队列机制实现客户进程和服务器进程间的通信。
客户进程首先建立一个描述符为msgqid的消息队列,接着向服务器进程发送一个消息正文为自己的进程标识pid且类型为1的消息,然后接收来自服务器进程的消息,并在屏幕上显示:
“Clientreceivesamessagefromxxxx!
”,其中“xxxx”为服务器进程的进程标识。
服务器进程首先捕捉软中断信号(除不能捕捉的SIGKILL),若捕捉到时则调用函数cleanup()删除消息队列,终止服务器进程。
否则重复下列操作:
接收所有类型为1的消息,并在屏幕上显示:
“Serverreceivesamessagefromxxxx!
”,其中“xxxx”为客户进程的进程标识;然后服务器进程将客户进程的进程标识作为返回消息的类型,而将自己的进程标识作为消息正文发送给客户进程。
2.共享内存
使用系统调用shmget()、shmat()和shmctl(),用共享内存机制实现进程间的通信。
其中一个进程向共享内存中写入数据,另一个进程从共享内存中读出数据并显示在屏幕上。
三、实验要求
1.根据实验内容编写C程序。
2.上机调试程序。
3.记录并分析程序运行结果。
四、程序说明和程序流程图
1.消息队列
2.共享内存
五、程序代码
1.
(1)/*msg_client.c*/
#include
#include
#include
#include
#include
#include
#defineMSGKEY75
structmsgform
{longmtype;
charmtext[256];
}
main()
{structmsgformmsg;
intmsgqid,pid,*pint;/*文件主同组用户其他用户rwxrwxrwx*/
msgqid=msgget(MSGKEY,0777);/*rw-rw-rw-*/
pid=getpid();
pint=(int*)msg.mtext;
*pint=pid;
msg.mtype=1;
msgsnd(msgqid,&msg,sizeof(int),0);
msgrcv(msgqid,&msg,256,pid,0);
printf("client:
receivefrompid%d\n",*pint);
}
(2)/*msg_server.c*/
#include
#include
#include
#include
#include
#include
#defineMSGKEY75
structmsgform
{longmtype;
charmtext[256];}msg;
intmsgqid;
main()
{inti,pid,*pint;
externcleanup();
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;
pid=*pint;
printf("server:
receivefrompid%d\n",pid);
msg.mtype=pid;
*pint=getpid();
msgsnd(msgqid,&msg,
sizeof(int),0);}
}
cleanup()
{msgctl(msgqid,IPC_RMID,0);
exit(0);}
2.
(1)
#include
#include
#include
#defineSHMKEY75
#defineK1024
intshmid;
main()
{inti,*pint;
char*addr;
externclearup();
for(i=1;i<20;i++)signal(i,clearup);
shmid=shmget(SHMKEY,16*K,0777|IPC_CREAT);
addr=shmat(shmid,0,0);
printf("addr0x%x\n",addr);
pint=(int*)addr;
for(i=0;i<256;i++)*pint++=i;
pint=(int*)addr;
*pint=256;
pause();
}
clearup()
{shmctl(shmid,IPC_RMID,0);
exit(0);
}
(2)
#include
#include
#include
#include
#defineSHMKEY75
#defineK1024
intshmid;
main()
{inti,*pint;
char*addr;
shmid=shmget(SHMKEY,8*K,0777);
addr=shmat(shmid,0,0);
pi