English 中文(简体)
C. 职务
原标题:forks in C - exercise
  • 时间:2010-03-22 16:12:15
  •  标签:
  • c
  • fork

我试图重复和学习更先进的用途和选择,在把树木与花在C丛林中的树冠混为一谈时,我发现一个非常容易的例子,因为我曾在一些法典之前甚至写过一些法典,但我可以完全理解。

这里是:

main() {
 if (fork() == 0) {
  if (fork() == 0) {
   printf("3");
  }
  else if ((wait(NULL)) > 0) {
   printf("2");
  }
 }
 else {
  if (fork() == 0) {
   printf("1");
   exit(0);
  }
  if (fork() == 0) {
   printf("4");
  }
 }
 printf("0");
 return 0;
}

可能的解决办法是:

  1. 3201040
  2. 3104200
  3. 1040302
  4. 4321000
  5. 4030201
  6. 1403020

其中2、5和6个是正确的答案。

First of all, shouldn t there be four zeroes in the output? Second... How does one come to the solution at all? Been doing this on paper for almost an hour and I m not even close to understanding why the given solution are more correct than the false ones (except for nr3 as it can t end with 2 since a 0 must follow).

任何与他的 for子一起检查谁能提供一些很好的解释?

EDIT:

创立: 参看2009年版。 现在人们能否停止把这项工作作为家庭工作,并实际努力提供帮助? 如果没有,请找到其他专题来花费你的时间。 感谢!

最佳回答

我认为,应该有4个零点,这就是我看一看是你们的法典。

进行这种分析的一个良好办法是,采用这样的图表——我把 for显示为*,与父母保持横向联系。

----*----*----*----0----exit (return from main)
    |    |    |
    |    |    +----4----0----exit (return from main)
    |    |
    |    +----1----exit (explicitly)
    |
    +-----*----wait----2----0----exit (return from main)
          |
          +----3----0----exit (return from main)

现在很容易看到,由于<代码>wait(>>,你必须看到3。 随后,0, before, see 2<> 之后是<代码>0

问题回答

产出中仅有3个零,原因是在节目印本之后的<代码>exit说明。 1. 声明立即结束这一进程。 <><><>>>> 没有,实际上,should为4零;我忽略了原来的程序。 我不知道答案为何没有四点零。

在分析问题答案时,大家必须理解的是:

  1. Every time you call fork() you create a new process, and the old process continues as well, both from the same point
  2. You can tell if you are in the "old process" or the "new process" by examining the return value of fork. It will be 0 (false) if you are in the new process, and non-zero (true) if you are in the old process.
  3. Calling wait(NULL) will suspend the current process execution until one of the processes that forked from it finishes.
  4. (Most important) The statements in different processes can execute in any order relative to each other. But the statements within a single process must, of course, remain sequential.

To<4>,想象你有两个过程:一个是<> > > > ;另一个是“xyz”。 之所以有可能,是因为每一序列的bc和xyz按顺序排列。 然而,不可能有“The >cxy”,因为不可能在X之前出现z,因为两者都来自同一过程,而“它们”的声明则按其他顺序出现。

他们假定,当你要求等待时,整个3座椅将被处决。 这包括在顶端印刷0台。 因此,答案1和4是不正确的,因为它们在2之前没有零。

至于为什么只有4秒,我不知道。

首先,你必须选择的备选办法是而不是所有可能的产出变动。 相反,他们只是其中几个人。 看看来源法,我们可以注意到消除以下可能性:

  1. The branch that prints "1" then does exit(0) without ever going on to print "0". So there are only three 0 s expected in the output. Also for this reason, the last character has t be a 0 or a 1.
  2. After the second fork, we wait(NULL) for the child before printing "2". This means that the child that we wait for will always print a 3 followed by a 0 before we print a 2.

这些问题确实是唯一能够明确表明这一问题的内容。 这意味着任何正确的答案都必须在0或1时结束,必须在2之前有3和0。 达到这些标准的唯一答案是2、5和6。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签