操作系统实验报告进程的创建.docx

上传人:b****1 文档编号:12488515 上传时间:2023-04-19 格式:DOCX 页数:10 大小:99.79KB
下载 相关 举报
操作系统实验报告进程的创建.docx_第1页
第1页 / 共10页
操作系统实验报告进程的创建.docx_第2页
第2页 / 共10页
操作系统实验报告进程的创建.docx_第3页
第3页 / 共10页
操作系统实验报告进程的创建.docx_第4页
第4页 / 共10页
操作系统实验报告进程的创建.docx_第5页
第5页 / 共10页
点击查看更多>>
下载资源
资源描述

操作系统实验报告进程的创建.docx

《操作系统实验报告进程的创建.docx》由会员分享,可在线阅读,更多相关《操作系统实验报告进程的创建.docx(10页珍藏版)》请在冰豆网上搜索。

操作系统实验报告进程的创建.docx

操作系统实验报告进程的创建

实验题目

进程的创建

小组合作

姓名

班级

学号

一、实验目的

1、了解进程的创建。

2、了解进程间的调用以及实现。

3、分析进程竞争资源的现象,学习解决互斥的方法。

4、加深对进程概念的理解,认识并发执行的本质。

二.实验环境

Windows系统的计算机一台,安装了Linux虚拟机

三、实验内容与步骤

1、fork()系统调用的使用例子

程序代码:

#include

#include

#include

intglob=3;

intmain(void)

{

pid_tpid;intloc=3;

printf("beforefork();glod=%d,loc=%d.\n",glob,loc);

if((pid=fork())<0)

{

printf("fork()error.\n");

exit(0);

}

elseif(pid==0)

{

glob++;

loc--;

printf("childprocesschangesglobandloc:

\n");

}

else

wait(0);

printf("parentprocessdoesn'tchangetheglobandloc:

\n");

printf("glob=%d,loc=%d\n",glob,loc);

exit(0);

}

运行结果:

2、理解vofork()调用:

程序代码:

#include

#include

#include

intglob=3;

intmain(void)

{

pid_tpid;

intloc=3;

if((pid=vfork())<0)

{

printf("vfork()error\n");

exit(0);

}

elseif(pid==0)

{

glob++;

loc--;

printf("childprocesschangestheglobandloc\n");

exit(0);

}

else

printf("parentprocessdoesn'tchangetheglobandloc\n");

printf("glob=%d,val=%d\n",glob,loc);

}

运行结果:

3、给进程指定一个新的运行程序的函数exec().

程序代码:

printe1.c代码:

#include

intmain(intargc,char*argv[])

{

intn;

char**ptr;

externchar**environ;

for(n=0;n

printf("argv[%d]:

%s\n",n,argv[n]);

for(ptr=environ;*ptr!

=0;ptr++)

printf("%s\n",*ptr);

exit(0);

}

file4.c代码如下:

#include

#include

#include

#include

char*env_list[]={"USER=root","PATH=/root/",NULL};

intmain()

{

pid_tpid;

if((pid=fork())<0)

{

printf("forkerror!

\n");

exit(0);

}

elseif(pid==0)

{

if(execle("/root/print1","print1","arg1","arg2",(char*)0,env_list)<0)

printf("execleerror!

\n");

exit(0);

}

if((waitpid(pid,NULL,0))<0)

printf("WAITERROR!

\n");

exit(0);

if((pid=fork())<0)

{

printf("forkerror!

\n");

exit(0);

}

elseif(pid==0)

{

if(execlp("print1","print1","arg1",(char*)0)<0)

printf("execleerror!

\n");

exit(0);

}

exit(0);

}

运行结果:

4、进程终止函数exit()。

程序代码:

#include

main()

{

printf("thisisaexitsystemcall!

!

\n");

exit(0);

printf("thissentenceneverbedisplayen:

\n");

}

#include

main()

{

printf("thisisa_exit_testsystemcall!

\n");

printf("contentinbuffer");

exit(0);

}

运行结果:

5、wait()函数和sleep()函数。

程序代码:

#include

main()

{

intpid1;

if(pid1=fork())

{

if(fork())

{

printf("parent'scontext,\n");

printf("parent'swaitingthechild1terminate,\n");

wait(0);

printf("parent'swaitingthechild2terminate,\n");

wait(0);

printf("parentterminates,\n");

exit(0);

}

else

printf("child2'scontext,\n");

sleep(5);

printf("child2terminates,\n");

exit(0);

}

else

{

if(pid1==0)

{

printf("child1'scontext,\n");

sleep(10);

printf("child1terminates,\n");

exit(0);

}

}

}

运行结果:

6、编写一段程序,父进程使用fork()创建两个子进程,利用输出函数putchar父进程显示字符”a”,两个子进程分别显示“b”和“c”。

程序代码:

#include

#include

#include

intmain()

{

intpid;

if(pid=fork())

{

if(fork())

{

printf("parentprocessis\n");

putchar('A');

printf("\n");

}

else

{

printf("child2processis\n");

putchar('C');

printf("\n");

}

}

else

{

if(pid==0)

printf("child1processis\n");

putchar('B');

printf("\n");

}

}

运行结果:

四、实验过程与分析

1、在1例子中,调用正确完成时,给父进程返回的是被创建子进程标识,给子进程自己返回的是0;创建失败时,返回给父进程的是-1。

2、在2例子中,vfork()调用后需要注意两点:

(1)子进程先运行,父进程挂起。

子进程调用exec()或exit()之后。

父进程的执行顺序不再有限制。

(2)子进程在调用exec()或exit()之前。

父进程被激活,就会造成死锁。

3、在6例子中,上述程序是父进程先创建一个子进程,若成功,再创建另一个子进程,之后三个进程并发执行。

究竟谁先执行,是随机的。

所以执行结果有多重种。

五、实验总结

1、一个进程调用exec()函数来运行一个新程序。

之后该进程的代码段、数据段和堆栈段就被新程序的所代替。

新程序从自己的main()函数开始执行。

exec()函数有6种不同的形式,任何一个都可以完成exec()的功能,只是调用参数不同。

2、在父子进程同步中,当一个进程结束时,产生一个终止状态字,然后核心发一个SIGCHLD信号通知父进程。

因为子进程结束是异步于父进程的,故父进程要结束之前,要同步等待子进程终止。

这是通过调用系统调用wait或waitpid来实现的。

当父进程通过调用wait或waitpid同步等待子进程结束时,可能有以下几种情况:

(1)如果子进程还未结束,父进程阻塞等待;

(2)如果子进程已经结束,其终止状态字SIGCHLD放在指定位置等待父进程提取,这时,父进程可立即得到终止状态字并返回;

(3)如果没有子进程,父进程立即错误并返回。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 考试认证 > 公务员考试

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1