操作系统的作业,不会做,谁能帮帮我啊

finalfantasy0915 2006-03-14 09:41:21
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int varA = 40;

int main(int argc, int **argv)
{
int varB = 29;
pid_t retFork=2;

fprintf(stderr, "PID: %4ld retFork:%2d varA: %2d varB: %2d\n", getpid(), retFork, varA, varB);
retFork = fork();
varA = 80;
fprintf(stderr, "PID: %4ld retFork:%2d varA: %2d varB: %2d\n", getpid(), retFork, varA, varB);
switch (retFork){
case -1:
fprintf(stderr, "The fork system call failed\n" );
fprintf(stderr, "PID: %4ld retFork: %2d\n", getpid(), retFork);
exit(1);
case 0:
varA += 10;
varB += 10;
fprintf(stderr, "PID: %4ld retFork: %2d\n", getpid(), retFork);
default:
fprintf(stderr, "PID: %4ld retFork: %2d\n", getpid(), retFork);
varA += 25;
varB += 25;
exit(1);
}
fprintf(stderr, "PID: %4ld varA: %2d varB: %2d\n", getpid(), varA, varB);
return 0;
}


程序在上面了,文件名叫createprocess.c,作业题目如下:

a. [4 points] 编译上述程序并运行它
i. 给出 parent process 和 the child process 的输出结果
ii. 观察变量retFork, varA and varB 的变化. 解释变量 retFork, varA 和 varB 的值变化的原因.

第二题不会翻译了,实在抱歉:
[4 points]Modify the createprocess.c program to have only the parent process voluntarily surrender the CPU for 2 seconds using the sleep system call. Compile and run the modified program.
i. Does the call to sleep function change the output produced by the two processes? Explain.
ii. Does the call to sleep function change the final value of the varA and varB variables in the two processes? Explain.


那位大虾可以给我详细的解释一下啊,这门课学的是在一头雾水。求正解!
...全文
145 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
jixingzhong 2006-03-15
  • 打赏
  • 举报
回复
楼主学操作系统,
这个都不会 ??
patrick84 2006-03-14
  • 打赏
  • 举报
回复
父子进程是有0,1之分的,上机测试就知道了
duduhaha 2006-03-14
  • 打赏
  • 举报
回复
这是最基本的问题.注意fork的作用就行了.依据返回值不同分清父子进程.
楼主在linux下试一下不就知道了.一定要亲手试一下.

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧