当前位置: 技术问答>linux和unix
紧急求助——一道去年清华操作系统的unix编程题目
来源: 互联网 发布时间:2015-01-22
本文导语: 给出下列程序的输出结构,并说明为什么 #include "studio.h" int main(void) { int I; int IntVariableA,IntVariableB; IntVariableA=100; IntVariableB=200; while((I=fork()==-1); printf("Test..."); if(I!=0) {...
给出下列程序的输出结构,并说明为什么
#include "studio.h"
int main(void)
{
int I;
int IntVariableA,IntVariableB;
IntVariableA=100;
IntVariableB=200;
while((I=fork()==-1);
printf("Test...");
if(I!=0)
{
printf("It is parent process.n");
IntVariableA+=IntVariableB;
wait(null);
printf("In parent: A=%d,B=%dn",IntVariableA,IntVariableB);
printf("My child process,ID number %d exited.n",I);
exit();
}
else
{
printf("It is child process.n");
IntVariableB+=IntVariablea;
printf("In child: A=%d,B=%dn",IntVariableA,IntVariableB);
}
printf("It is child or parent process.n");
exit()
}
#include "studio.h"
int main(void)
{
int I;
int IntVariableA,IntVariableB;
IntVariableA=100;
IntVariableB=200;
while((I=fork()==-1);
printf("Test...");
if(I!=0)
{
printf("It is parent process.n");
IntVariableA+=IntVariableB;
wait(null);
printf("In parent: A=%d,B=%dn",IntVariableA,IntVariableB);
printf("My child process,ID number %d exited.n",I);
exit();
}
else
{
printf("It is child process.n");
IntVariableB+=IntVariablea;
printf("In child: A=%d,B=%dn",IntVariableA,IntVariableB);
}
printf("It is child or parent process.n");
exit()
}
|
#include "studio.h" //应该是#include "stdio.h"吧
int main(void)
{
int I;
int IntVariableA,IntVariableB;
IntVariableA=100;
IntVariableB=200;
while((I=fork()==-1); //应该是 while((I=fork())==-1);
//fork函数产生一子进程,把子进程号给,再判断是否为-1。(为-1表示子进程产生出错),此句是为了一定要产生一子进程
printf("Test...");
if(I!=0) //如果I(子进程号)不为0,表示是父进程
{//为父进程处理部份
printf("It is parent process.n");
IntVariableA+=IntVariableB;
wait(null); //等待子进程运行结束
printf("In parent: A=%d,B=%dn",IntVariableA,IntVariableB);
printf("My child process,ID number %d exited.n",I);
exit(); //结束程序
}
else
{ //为子进程处理部份
printf("It is child process.n");
IntVariableB+=IntVariablea; // IntVariableB+=IntVariableA;
printf("In child: A=%d,B=%dn",IntVariableA,IntVariableB);
}
printf("It is child or parent process.n");
exit() // exit();退出子进程
}
输出结果:
Test...It is child process.
In child: A=100,B=300
It is child or parent process.
Test...It is parent process.
In parent: A=300,B=200
My child process,ID number 3058 exited.
最后一行的3058是子进程的进程号,每次运行不一定。
int main(void)
{
int I;
int IntVariableA,IntVariableB;
IntVariableA=100;
IntVariableB=200;
while((I=fork()==-1); //应该是 while((I=fork())==-1);
//fork函数产生一子进程,把子进程号给,再判断是否为-1。(为-1表示子进程产生出错),此句是为了一定要产生一子进程
printf("Test...");
if(I!=0) //如果I(子进程号)不为0,表示是父进程
{//为父进程处理部份
printf("It is parent process.n");
IntVariableA+=IntVariableB;
wait(null); //等待子进程运行结束
printf("In parent: A=%d,B=%dn",IntVariableA,IntVariableB);
printf("My child process,ID number %d exited.n",I);
exit(); //结束程序
}
else
{ //为子进程处理部份
printf("It is child process.n");
IntVariableB+=IntVariablea; // IntVariableB+=IntVariableA;
printf("In child: A=%d,B=%dn",IntVariableA,IntVariableB);
}
printf("It is child or parent process.n");
exit() // exit();退出子进程
}
输出结果:
Test...It is child process.
In child: A=100,B=300
It is child or parent process.
Test...It is parent process.
In parent: A=300,B=200
My child process,ID number 3058 exited.
最后一行的3058是子进程的进程号,每次运行不一定。
|
此程序的关键是while(I=fork()==-1);
等价:
while(1)
{ I=fork();/*创建子进程*/
/*如果I!=-1
UNIX创建了一个子进程:
系统复制父进程程序文本并且创建一个同父进程一样的环境
*/
if(I!=-1)
break;
}
WAIT 用于同子进程同步
IntVariableA 和 IntVariableB 用于显示
等价:
while(1)
{ I=fork();/*创建子进程*/
/*如果I!=-1
UNIX创建了一个子进程:
系统复制父进程程序文本并且创建一个同父进程一样的环境
*/
if(I!=-1)
break;
}
WAIT 用于同子进程同步
IntVariableA 和 IntVariableB 用于显示