叉子() [英] fork()

查看:65
本文介绍了叉子()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了以下代码,并询问可能的输出

是多少。我们正在学习流程和分叉。


int value;

int main(){

int pid,number = 1;

value = 2;

pid = fork();

value = value + 2;

number = number + 2;

if(pid){

printf(" 0%d%d \ n",value,number);

waitpid(pid,NULL,0);

}

else {

printf(" 1%d%d \ n",value,number);

}

返回0;

}


到目前为止我已经提出了3种可能性:


1)fork()失败;父母的pid被赋予一个负整数,

所以if()条件通过,输出为0 4 3


2)分叉()成功,孩子*(pid = 0从成功的fork())

打印1 4 3然后返回;父(pid =孩子的进程ID#)

启动打印0 4 3并返回


3)fork()成功,但是孩子的量子到期之前^

到达printf(),父母(pid =孩子的进程ID#)开始,

打印0 4 3然后等待孩子;孩子(pid = 0来自

成功的fork())开始备份并打印1 4 3并返回;

父母返回


*我猜孩子优先于父母(系统

依赖?)因为每次我'已经运行了程序输出已经

1 4 3然后是0 4 3.


^我猜CPU的量子长度会必须要非常小的

才能发生案例3,但这是可能的,对吧?


我是这个材料的新手,所以任何评论都会得到真正的赞赏。

I was given the following code, and asked what the possible outputs
could be. We''re learning about processes and forking.

int value;
int main(){
int pid, number = 1;
value = 2;
pid = fork();
value = value + 2;
number = number + 2;
if (pid) {
printf(" 0 %d %d \n", value, number);
waitpid(pid, NULL, 0);
}
else{
printf(" 1 %d %d \n", value, number);
}
return 0;
}

I''ve come up with 3 possibilities so far:

1) the fork() fails; the parent''s pid is assigned a negative integer,
so the if() condition passes and the output is 0 4 3

2) the fork() succeeds, the child* (pid = 0 from successful fork())
prints 1 4 3 and then returns; the parent (pid = child''s process id #)
starts up prints 0 4 3 and returns

3) the fork() succeeds, but the child''s quantum expires^ before it
reaches the printf(), the parent (pid = child''s process id #) starts,
prints 0 4 3 and then waits for the child; the child (pid = 0 from
successful fork()) starts back up and prints 1 4 3 and returns; the
parent returns

* I guess the child takes precedence over the parent (system
dependent?) because every time I''ve run the program the output has been
1 4 3 followed by 0 4 3.

^ I guess the CPU''s quantum length would have to be incredibly small
for case 3 to happen, but it''s possible, right?

I''m new to this material, so any comments would be truly appreciated.

推荐答案

st ***@stevedurkin.net 写道:
我得到了以下代码,并询问可能的输出是什么。我们正在学习过程和分叉。

int value;
int main(){
int pid,number = 1;
value = 2;
pid = fork();
I was given the following code, and asked what the possible outputs
could be. We''re learning about processes and forking.

int value;
int main(){
int pid, number = 1;
value = 2;
pid = fork();




对不起,但fork()不是ISO C的一部分。把你的问题带到

新闻:comp.unix.programmer,它可能会得到彻底的回答

和优雅。


HTH,

- ag


-

Artie Gold - 德克萨斯州奥斯汀

Topicality很重要!



Sorry, but fork() is not part of ISO C. Take your question to
news:comp.unix.programmer, where it will likely be answered thoroughly
and gracefully.

HTH,
--ag

--
Artie Gold -- Austin, Texas
"Topicality is important!"


在文章< 11 ********************** @ o13g2000cwo.googlegroups .com> ;,

< st *** @ stevedurkin.net>写道:
In article <11**********************@o13g2000cwo.googlegroups .com>,
<st***@stevedurkin.net> wrote:
我得到了以下代码,并询问可能的输出是什么。我们正在学习流程和分叉。


fork()不是C语言的一部分。我建议你把

的问题带到一个更合适的新闻组,比如comp.unix.programmer


我到目前为止已经提出了3种可能性:
I was given the following code, and asked what the possible outputs
could be. We''re learning about processes and forking.
fork() is not part of the C language. I suggest you take the
question to a more appropriate newsgroup, such as comp.unix.programmer

I''ve come up with 3 possibilities so far:




哦,有超过3个,特别是因为你的程序

不使用volatile,你的程序没有不要声明printf()。

它也有一些POSIX问题,但是POSIX是关于

comp.lang.c的主题。

-

重要的是要记住,在法律方面,计算机

永远不会复制,只有人类才能复制。计算机给出了

命令,而非许可。只有人才能获得许可。

- Brad Templeton



Oh, there''s more than 3, especially since your program
doesn''t use volatile, and your program doesn''t declare printf().
It also has some POSIX problems, but POSIX is off topic for
comp.lang.c .
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton


st *** @ stevedurkin.net 写道:
我得到了以下代码,并询问可能的输出是什么。我们正在学习过程和分叉。

int value;
int main(){
int pid,number = 1;
value = 2;
pid = fork();
value = value + 2;
number = number + 2;
if(pid){
printf(" 0%d%) d \ n",value,number);
waitpid(pid,NULL,0);
}
else {
printf(" 1%d%d \ n",value,number);
}
返回0;
}
I was given the following code, and asked what the possible outputs
could be. We''re learning about processes and forking.

int value;
int main(){
int pid, number = 1;
value = 2;
pid = fork();
value = value + 2;
number = number + 2;
if (pid) {
printf(" 0 %d %d \n", value, number);
waitpid(pid, NULL, 0);
}
else{
printf(" 1 %d %d \n", value, number);
}
return 0;
}



[snip]


" int main()"是有效的,但是int main(void)是首选。


你错过了一个#include< stdio.h>,如果你是
,这是必需的打算使用printf()。你也错过任何标题声明

fork()和waitpid()。


但事实上,上面的代码甚至都不会编译没有#include

至少有一个定义NULL宏的头文件。请

发布完整的,可编辑的程序。


fork()函数未在标准C中定义。谁可以

在comp.unix.programmer中回答你关于它的问题。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< * GT; < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。


[snip]

"int main()" is valid, but "int main(void)" is preferred.

You''re missing a "#include <stdio.h>", which is required if you''re
going to use printf(). You''re also missing whatever header declares
fork() and waitpid().

But in fact, the code above wouldn''t even compile without a #include
for at least one of the headers that defines the NULL macro. Please
post complete, compilable programs.

The fork() function is not defined in standard C. The people who can
answer your questions about it hang out in comp.unix.programmer.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


这篇关于叉子()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆