« first day (4026 days earlier)      last day (41 days later) » 

9:20 AM
I'm testing what the return value of a fork looks like. In both Lua and Python I get similar results.
Here is the Python version.
import os
print(os.fork())
The fork man page says:
> On success, the PID of the child process is returned in the parent, and 0 is returned in the child.
What I get from the code above is:
524362
0
I'm not exactly sure how to deal with this. Am I getting print values from two different processes? And if so, which do I treat as the return value?
Presumably if I wrote a C version, that would also get similar results.
Oh, never mind. This was already answered on SO.
I should have checked first, sorry.
This is possibly the best reply:
6
A: Why does return value of fork() have 2 pid value in C?

Charlie MartinYou're actually seeing the output of two copies of the executable. When you call fork(), the process clones itself, giving two processes. The parent gets the child process's PID as the return value, and the child gets 0 as a return value. The trick is, the clones share STDIN, STDOUT, and STDER...

 
 
7 hours later…
4:03 PM
Can anyone suggest a standard Unix command/utility which has a variety of easily triggered error codes, along with unique error messages?
For testing purposes.
 
 
2 hours later…
5:53 PM
@FaheemMitha I thought it was easier, but I see that most POSIX utilities are only required to use 0 for "no error" and > 0 for "something went wrong".
You may use tput. Examples: tput >/dev/null (status == 2, usage error), TERM=foo tput >/dev/null (status == 3, no information for the terminal type), tput foo >/dev/null (status == 4, unknown capability). It can also return 1, which POSIX says is "unspecified" and historically used to tell the terminal doesn't have the specified capability (e.g. TERM=vt100 tput setaf >/dev/null); notably, in this case the exit status is ≠ 0, but no error message is printed out.
 
 
1 hour later…
7:16 PM
@fra-san Thank you. That's very helpful. Honestly, I don't even know what tput is.
 
sh -c 'exit 3'
add whatever printouts you want
 
@MichaelHomer Thanks for the suggestion. Not sure how to induce a failure mode here.
 
 
2 hours later…
8:59 PM
@FaheemMitha the only reason that comes to mind is that they're using the first pipe in the opposite direction of the others, and so look at the values they'll eventually return from the function. But you're right, that's wrong, only the first return value will be nil on error
 
@ilkkachu That occurred to me too, but it's irrelevant.
Also, that function is returning 1 on success for some reason. Though I don't know if the Unix 0 is universal, or even if there is a universal.
 

« first day (4026 days earlier)      last day (41 days later) »