getline() vs. fgets(): Control memory allocation

My question is: Isn’t that dangerous? What if by accident or malicious intent someone creates a 100GB file with no ‘\n’ byte in it – won’t that make my getline() call allocate an insane amount of memory? Yes, what you describe is a plausible risk. However, if the program requires loading an entire line into … Read more

What can lead to “IOError: [Errno 9] Bad file descriptor” during os.system()?

You get this error message if a Python file was closed from “the outside”, i.e. not from the file object’s close() method: The line del f deletes the last reference to the file object, causing its destructor file.__del__ to be called. The internal state of the file object indicates the file is still open since f.close() was never called, so the destructor tries … Read more

waitpid, wnohang, wuntraced. How do I use these

If you pass -1 and WNOHANG, waitpid() will check if any zombie-children exist. If yes, one of them is reaped and its exit status returned. If not, either 0 is returned (if unterminated children exist) or -1 is returned (if not) and ERRNO is set to ECHILD (No child processes). This is useful if you want to find out if any of your children recently died without having … Read more

wait(null) and wait(&status) C language and Status

If you call wait(NULL) (wait(2)), you only wait for any child to terminate. With wait(&status) you wait for a child to terminate but you want to know some information about it’s termination. You can know if the child terminate normally with WIFEXITED(status) for example. status contains information about processes that you can check with some already defined MACRO.

What does WEXITSTATUS(status) return?

WEXITSTATUS(stat_val) is a macro (so in fact it does not “return” something, but “evaluates” to something). For how it works you might like to look it up in the headers (which should be #included via <sys/wait.h>) that come with the C-compiler you use. The implementation of this macro might differ from one C-implementation to the other. Please note, … Read more