System Call Error Handling
Prev: Processes
TOC: Exceptional Control Flow
Next: Process Control
- Unix function encountering errors typically returns -1 and set the global integer variable errno to indicate what went wrong.
Example:if((pid = fork()) < 0){ fprintf(std, "fork error: %s\n", strerror(errno)); exit(0); } - We can somehow simplify this by doing this:
void unit_error(char *msg) { fprintf(stderr, "%s: %s\n", msg, strerror(errno)); exit(0); } if((pid = fork()) < 0){ unix_error("fork error"); } - For this book we define error-handling wrappers, for a given function foo, we define a wrapper function Foo with identical args, this checks for error and terminates if there's any problem
- Example:
pid_t Fork(void){
pid_t pid;
if((pid = fork()) < 0)
unix_error("Fork error");
return pid;
}
Prev: Processes
TOC: Exceptional Control Flow
Next: Process Control