叉,等待,退出等的正确使用 [英] Correct usage of fork, wait, exit, etc

查看:149
本文介绍了叉,等待,退出等的正确使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个问题解决了,我不知道该怎么做,因为只有少数的系统调用,我们可以用它来解决它,我不知道他们是如何的局面有所帮助。

I have this problem to solve that I have no idea how to do it because there's only a few system calls we can use to solve it and I don't see how they are helpful for the situation.

的练习:

我有大小[10] [百万]与整数矩阵和每行创建与叉一个新的进程()。每个过程的想法是要经过所有的数字为该特定行,并找到然后一个具体的数字打印有关它的消息。这是问题的第一步,它的完成。第二步是按订单以打印总量,这个数字的出现在每一行的。而在最后,共展宏图,这个数字出现的。

The Exercise:
I have matrix with size [10][1000000] with integers and for each line I create a new process with fork(). The idea of each process is to go through all the numbers for that specific line and find a specific number then print a message about it. This was the first step of the problem and it's done. The second step is to print the total of occurrences of that number on each line by order. And in the end, the grand total of occurrences of that number.

来电:

系统调用我可以用都是这样被描述的文档在这个练习中:

The Calls:
The system calls I can use are described like this in the document for this exercise:


  • 将为pid_t叉(无效);

  • 无效退出(INT的地位);

  • 将为pid_t等待(INT *状态);

  • 将为pid_t waitpid函数(PID将为pid_t,INT *状态,INT选项);

  • pid_t fork(void);
  • void exit(int status);
  • pid_t wait(int *status);
  • pid_t waitpid(pid_t pid, int *status, int options);

问题:

我不知道该怎么做,因为退出()通话只允许我通过下面256多家,如果出现的次数比这个数字更大?我怎么回这样一个数字?

The Problem:
I have no idea how to do it because the exit() call only allows me to pass a number below 256, what if the number of occurrences is larger than this number? How shall I return such a number?

另一个问题:

我完全不明白之间的差额等待() waitpid函数()和如何/在哪里过度使用一个另一个。除了手册页,还有没有更多的文档,我可以看到code例子,更好地这样理解呢?或者,有人可以解释我的区别,并提供了一​​个基本的例子证明这种差异?

Another Problem:
I don't exactly understand the difference between wait() and waitpid() and how/where to use one over the other. Besides the man pages, are there any more documentation where I can see code examples and such to understand them better? Or can someone explain me the differences and provide a basic example demonstrating such differences?

推荐答案

使用 waitpid函数()来争取序列中的子进程的退出状态;使用的wait()不作任何在孩子的尸体将被检索顺序的保证。

Use waitpid() to garner the exit statuses of the child processes in sequence; using wait() makes no guarantee about the sequence in which the child corpses will be retrieved.

在Unix上,退出状态被限制为8位,可作为签署或由程序检索数据符号来处理。您还可以得到一个8位的值确定终止子的信号数量和核心转储的状态。据我所知,无论是状态或信号位始终为零(且经常发生 - 当进程成功退出)。

On Unix, the exit status is limited to 8 bits, which can be treated as signed or unsigned by the program retrieving the data. You also get an 8 bit value identifying the signal number and core dump status of the terminated child. AFAIK, either the status or the signal bits are always zero (and often both - when the process exits successfully).

如果你不知道要返回的数字比256小,则退出状态是不是要走的路。正如其他人所说,你必须使用一些其他的IPC在这种情况下。如果允许的唯一的系统调用是这些,那么你不得不得出结论,该值将小于255,或溢出并不重要。无论是作为一门功课外运动的结论令人满意,但在现实世界,你不仅限于4系统调用无论是。

If you don't know that the numbers to be returned are smaller than 256, then exit status is not the way to go. As others have said, you have to use some other IPC in that case. If the only system calls permitted are those, then you have to conclude that the values will be less than 255, or that overflows don't matter. Neither is satisfactory as a conclusion outside a homework exercise, but in the 'real world', you are not limited to just 4 system calls either.

另请参阅退出codeS大于255? 。需要注意的是在Windows上,退出$范围C $ CS大得多 - 但你不使用的问题列出的系统调用

See also Exit codes bigger than 255?. Note that on Windows, the range of exit codes is much larger - but you don't use the system calls listed in the question.


观察:当我做退出(1),地位从值等待()为256;有一个原因?

Observation: when I do exit(1), the value in status from wait() is 256; is there a reason for that?

答:是的。状态字EN code信号数量等的低8位;高8位(16位)状态字EN code退出状态的。

Answer: yes. The low 8 bits of the status word encode the signal number and so on; the high 8 bits of the (16-bit) status word encode the exit status.

请参阅< SYS / wait.h方式> 和宏WIFEXITED(),WEXITSTATUS()等

See <sys/wait.h> and macros WIFEXITED(), WEXITSTATUS(), etc.

这篇关于叉,等待,退出等的正确使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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