作者:万宝盛华猎头 | 来源:互联网 | 2023-05-16 12:27
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.
我有这个问题要解决,我不知道怎么做,因为只有少数系统调用,我们可以用它来解决它,我不知道它们如何对这种情况有所帮助。
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.
练习:我的矩阵大小为[10] [1000000],带有整数,对于每一行,我用fork()创建一个新进程。每个过程的想法是遍历该特定行的所有数字并找到特定的数字然后打印有关它的消息。这是问题的第一步,已经完成了。第二步是按顺序打印每行的出现次数。最后,这个数字的总发生次数。
The Calls:
The system calls I can use are described like this in the document for this exercise:
呼叫:我可以使用的系统调用在本练习的文档中描述如下:
pid_t fork(void);
void exit(int status);
void exit(int status);
pid_t wait(int *status);
pid_t wait(int * status);
pid_t waitpid(pid_t pid, int *status, int options);
pid_t waitpid(pid_t pid,int * status,int options);
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?
问题:我不知道怎么做,因为exit()调用只允许我传递一个低于256的数字,如果出现次数大于这个数字怎么办?我该怎么回复这个号码?
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?
另一个问题:我并不完全理解wait()和waitpid()之间的区别以及如何/在哪里使用一个而不是另一个。除了手册页,还有更多文档,我可以看到代码示例,以便更好地理解它们吗?或者,有人可以解释我的差异,并提供一个证明这种差异的基本例子吗?
2 个解决方案
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.
使用waitpid()按顺序获取子进程的退出状态;使用wait()不能保证检索子尸体的顺序。
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).
在Unix上,退出状态限制为8位,可以通过检索数据的程序将其视为有符号或无符号。您还可以获得一个8位值,用于标识已终止子项的信号编号和核心转储状态。 AFAIK,状态或信号位始终为零(通常两者都是 - 当进程成功退出时)。
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.
如果您不知道要返回的数字小于256,则退出状态不是可行的方法。正如其他人所说,在这种情况下你必须使用其他一些IPC。如果允许的唯一系统调用是那些,那么你必须得出结论,这些值将小于255,或者溢出无关紧要。作为家庭作业之外的结论,两者都不令人满意,但在“现实世界”中,您不仅限于4个系统调用。
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.
另请参阅大于255的退出代码?请注意,在Windows上,退出代码的范围要大得多 - 但您不使用问题中列出的系统调用。
Observation: when I do exit(1)
, the value in status from wait()
is 256; is there a reason for that?
观察:当我退出(1)时,来自wait()的状态值为256;这是有原因的吗?
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.
答:是的。状态字的低8位编码信号编号,依此类推; (16位)状态字的高8位编码退出状态。
See
and macros WIFEXITED(), WEXITSTATUS(), etc.
请参阅
和宏WIFEXITED(),WEXITSTATUS()等。