0

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.

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);
  • pid_t wait(int *status);
  • 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?

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?

inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
rfgamaral
  • 16,546
  • 57
  • 163
  • 275
  • Sample input and output please. Take an array of dimensions 3x5, where 3 = no of lines, 5 = no of integers for brevity. – dirkgently Apr 21 '09 at 18:55
  • If you need to communicate between processes ad can't use the exit codes you'll have to investigate some other form of IPC. Consider pipes (perhaps via popen). – dmckee --- ex-moderator kitten Apr 21 '09 at 18:59
  • Can you write the numbers to a file, or can you communicate between processes using STDIN/OUT? – Terry Apr 21 '09 at 18:59
  • 1) exit() takes an int (i.e. not limited to 0-255). Does that get reflected properly in your master process when you call wait(int *status) and your subprocess exits normally ? – Brian Agnew Apr 21 '09 at 19:01
  • 2) can you take it as a documented 'limitation' that this will only work for < 256 occurrences ? After all, using exit()/wait() isn't an ideal IPC mechanism :-) (as you've discovered!) – Brian Agnew Apr 21 '09 at 19:02
  • @dirkgently: That serves no porpuse because it will be very quick, I need a huge input like 1000000 so that the first process will probably not finish first and I need to account for that print everything in order. @dmckee: I'm not sure I can do that. @Terry: No. @Brian Agnew: I don't know, I only remember the teacher saying the exit() would only allow values from 0 to 255 (or something like that). Not sure what it means... – rfgamaral Apr 21 '09 at 19:09
  • The reason I asked you to provide a small sample output was so that we understand your problem better and provide an appropriate response. I understand your threading concerns, but am not very clear on the input/output. – dirkgently Apr 21 '09 at 19:14
  • 1
    @Brian Agnew: on Unix, the exit status is limited to 8 bits. You get 16-bits of status from wait() or waitpid(), but the other 8 bits encode signal number and core dump status. One half of the 16 bit word is always zero bits. – Jonathan Leffler Apr 21 '09 at 19:49
  • @Jonathan: I wondered if that was the case, but it's been years since I did it seriously. Thx – Brian Agnew Apr 21 '09 at 19:53

2 Answers2

4

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.

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).

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.

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.


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

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.

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

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    I'll have to ask the teacher what he wants to do then... One thing though, like I said in another comment if I do exit(1), exit(2), exit(4) the status value from wait(int status) will actually be 256, 512, 1024 respectively, any reason for that? – rfgamaral Apr 21 '09 at 20:11
1

I think what you are doing should work fine - just return the number of occurrences as the exit code from the process.

You mention that exit() will only allow numbers below 256. I highly doubt if this is the case, but it would be simple enough for you to write a test program to find out for sure.

Sounds like this is really just a simplified version of Map-Reduce. You might want to have a look at that algorithm as well for some ideas on how you could further parallelize the program - and maybe get some extra credit :)

As for the difference between wait() and waitpid() - if you just want to wait for any of your child processes to complete, you would use wait(). If you want to wait only for a specific child process or if you wanted to just check if a child process has exited without hanging, you would use waitpid().

Eric Petroelje
  • 59,820
  • 9
  • 127
  • 177
  • On Unix, the questioner is correct - the exit status is an 8-bit value (which is sometimes treated as signed and sometimes treated as unsigned, depending on the program). – Jonathan Leffler Apr 21 '09 at 19:46
  • Yes, just searched on Google and various pages reported the same, I can only return values from 0-255 and I also just tested this and for some reason if I was returning 1 it actually returned 256, 2 = 512, 4 = 1024, don't know why... Also, there are no credits at all to earn on this, I need to know and understand how this works for the final exam in few weeks that will have similar questions. – rfgamaral Apr 21 '09 at 19:49
  • @Jonathan - interesting about the 8 bit status codes. Assuming that limitation, I can't think of any other way to pass the info from child to parent process within the constraints of the problem. – Eric Petroelje Apr 21 '09 at 20:52