操作系统实验一
- 格式:doc
- 大小:610.50 KB
- 文档页数:5
实验报告实验名称:进程管理、管道通信
实验目的:
1、熟悉linux下利用gcc、gdb编译、调试C程序
2、掌握进程的概念,明确进程的含义
3、认识并了解并发执行的实质
4、掌握进程间无名管道的通信
实验准备:
1、预习linux下利用gcc编译c程序。
2、参考课件及资料掌握进程的创建过程。
3、参考课件及资料掌握进程的并发执行。
4、参考课件及资料掌握进程间无名管道的通信
实验内容:
内容一:敲通如下程序,分析运行结果。
main()
{
int i;
while((i=fork())==-1);
printf("i=%d",i);
if(i)printf("It is a parent process!");
else printf("It is a child process!");
}
结果分析:系统不一定先调用父进程还是子进程因此结果是子在前父在后或父在前或子在后
内容二:编写程序,用系统调用fork()创建两子进程。
父进程显示50次字符串“father”,子进程1显示50次字符串“son”,子进程2显示50次字符串“daughter”。
观察并记录屏幕上显示结果,分析原因。
(提示:可在各进程中加入sleep,观察结果分析原因)
#include <stdio.h>
main( )
{
int p1,p2,i;
while((p1=fork( ))== -1); /*创建子进程p1*/
if (p1==0)
for(i=0;i<50;i++)
printf("daughter %d\n",i);
else
{
while((p2=fork( ))== -1); /*创建子进程p2*/
if(p2==0)
for(i=0;i<50;i++)
printf("son %d\n",i);
else
for(i=0;i<50;i++)
printf("parent %d\n",i);
}
}
结果分析:输出50个son
50个parent
50个dughter
不一定先输出子进程或父进程
内容三:敲通如下程序,写出运行结果,分析程序功能。
#include <pthread.h>
void *ptest(void *arg)
{
printf(" This is the new thread!" );
return(NULL);
main()
{ pthread_t tid;
printf(" This is the parent process !" );
pthread_create(&tid,NULL,ptest,NULL);
sleep(1);
return;
}
结果分析:进入主程序时输出“This is the parent process !”
用创建线程的函数该函数中执行了指针ptest指向的函数,
而后输出“This is the new thread!”返回值为空。
内容四:敲通管道通信(课件)例题,写出运行结果,分析程序功能#include <stdio.h>
main()
{
int x,fd[2];
char buf[30],s[30];
pipe(fd);
while((x=fork()) == -1);
if (x==0)
{
sprintf(buf,"This is an example\n");
write(fd[1],buf,30);
exit(0);
}
else
{
wait(0);
read(fd[0],s,30);
printf("%s",s);
}
}
运行结果:
输出This is an example!
创建管道fd,创建进程,进程标识符为x。
如果调用子进程则向缓冲区里写入字符串“This is an example”,然后从buf里向pipe里写入30个字符然后退出子进程,而后调用父进程从该缓冲区里读出30个字符并且显示出来。
如果先调用父进程则父进程会被要求等待,从pipe里读出的内容为空,直到调用子进程完成整个写入输出的过程。
内容五:编写一程序,建立一个管道。
同时,父进程生产子进程P1,P2,这两个子进程分别向管道中写入各自的字符串,父进程分别读出它们,并显示出来。
#include<stdio.h>
main()
{
int i,r,p1,p2,fd[2];
char buf[50],s[50];
pipe(fd); /*父进程建立管道*/
while((p1=fork())==-1);/*创建子进程P1,失败时循环*/
if(p1==0)/*由子进程P1返回,执行子进程P1*/
else /*从父进程返回,执行父进程*/
{
lockf(fd[1],1,0);/*加锁锁定写入端*/
sprintf(buf,"child process P1 is sending messages!\n");
printf("child processP1!\n");
write(fd[1],buf,50);/*把buf中的50个字符写入管道*/
sleep(5);/*睡眠5秒,让父进程读*/
lockf(fd[1],0,0);/*释放管道写入端*/
exit(0);/*关闭P1*/
}
{
while((p2=fork())==-1);/*创建子进程P2,失败时循环*/
if(p2==0)/*从子进程P2返回,执行P2*/
{
lockf(fd[1],1,0);/*锁定写入端*/
sprintf(buf,"child process P2 is sending messages\n");
printf("child process P2 ! \n");
1,1 顶端
write(fd[1],buf,50);/*把buf中字符写入管道*/
sleep(5);/*睡眠等待*/
lockf(fd[1],0,0);/*释放管道写入端*/
exit(0);/*关闭P2*/
}
}
}
while((p1=fork())==-1);/*创建子进程P1,失败时循环*/
if(p1==0)/*由子进程P1返回,执行子进程P1*/
else /*从父进程返回,执行父进程*/
{
lockf(fd[1],1,0);/*加锁锁定写入端*/
sprintf(buf,"child process P1 is sending messages!\n");
printf("child processP1!\n");
write(fd[1],buf,50);/*把buf中的50个字符写入管道*/
sleep(5);/*睡眠5秒,让父进程读*/
lockf(fd[1],0,0);/*释放管道写入端*/
exit(0);/*关闭P1*/
}
{
while((p2=fork())==-1);/*创建子进程P2,失败时循环*/
if(p2==0)/*从子进程P2返回,执行P2*/
{
lockf(fd[1],1,0);/*锁定写入端*/
sprintf(buf,"child process P2 is sending messages\n");
printf("child process P2 ! \n");
write(fd[1],buf,50);/*把buf中字符写入管道*/
sleep(5);/*睡眠等待*/
lockf(fd[1],0,0);/*释放管道写入端*/
exit(0);/*关闭P2*/
}
}
程序出来了没调试成功。