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

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

wait(0);

printf("parent process doesn't change the glob and loc:\n");

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

exit(0);

}

运行结果:

2、理解vofork()调用:

程序代码:

#include<>

#include

#include<>

int glob=3;

int main(void)

{

pid_t pid;

int loc=3;

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

{

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

exit(0);

}

else if(pid==0)

{

glob++;

loc--;

printf("child process changes the glob and loc\n");

exit(0);

}

else

printf ("parent process doesn't change the glob and loc\n");

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

}

运行结果:

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

程序代码:

代码:

#include<>

int main(int argc,char * argv[])

{

int n;

char * * ptr;

extern char * * 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);

}

代码如下:

#include<>

#include

#include<>

#include

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

int main()

{

pid_t pid;

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

{

printf("fork error!\n");

exit(0);

}

else if(pid==0)

{

if(execle("/root/print1","print1","arg1","arg2",(char *)0,env_list)<0) printf("execle error!\n");

exit(0);

}

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

printf("WAIT ERROR!\n");

exit(0);

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

{

printf("fork error!\n");

exit(0);

}

else if(pid==0)

{

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

printf("execle error!\n");

exit(0);

}

exit(0);

}

运行结果:

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

程序代码:

#include<>

main()

{

printf("this is a exit system call!! \n");

exit(0);

printf("this sentence never be displayen:\n");

}

#include<>

main()

{

printf("this is a _exit_test system call! \n");

printf("content in buffer");

exit(0);

}

运行结果:

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

程序代码:

#include<>

main()

{

int pid1;

if(pid1=fork())

{

if(fork())

{

printf("parent's context,\n");

printf("parent's waiting the child1 terminate,\n");

wait(0);

printf("parent's waiting the child2 terminate,\n");

wait(0);

printf("parent terminates,\n");

exit(0);

}

else

printf("child2's context,\n");

sleep(5);

printf("child2 terminates,\n");

exit(0);

}

else

{

if(pid1==0)

{

printf("child1's context,\n");

sleep(10);

printf("child1 terminates,\n");

exit(0);

相关文档
最新文档