-
操作系统导论
-
第五章
-
编码作业作业
categories:
- 操作系统导论
操作系统导论第五章:进程API
UNIX的系统调用
UNIX采用一对系统调用:fork()函数和exec()函数,非常有趣的创建新进程。
父进程还可以通过第三个系统调用wait(),等待其创建的子进程完成并且回收,注意,子进程不能调用it(),否则wait()函数会返回-1,而父进程调用则会返回子进程的PID。
fork()函数
先贴一段简单的调用fork函数的代码
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
printf("hello,world.\n");
int x = 100;
int rc = fork();
if(rc<0)
{
fprintf(stderr,"fork failed\n");
exit(1);
}
else if(rc==0)
{
printf("hello,I am child\n");
printf("x=%d\n",x+7);
}
else{
printf("hello,I am parent\n");
printf("x=%d\n",x+8);
}
return 0;
}
运行结果是:
可以看到是各改变各的值,并没有出现叠加的情况,父程序x变为108,子程序变为107a