sysprog - Computer Science 322 Operating Systems Mount...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Computer Science 322 Operating Systems Mount Holyoke College Spring 2008 Topic Notes: Unix Systems Programming Quote: UNIX system calls, reading about those can be about as interesting as reading the phone book. .. – George Williams, 3/12/91 We will consider several aspects of Unix systems programming, focusing first on those things you will need for the shell project. Error checking/reporting Most Unix system calls may fail for a variety of reasons. You should always check the return value of system calls that may fail. The reason for a failure in the errno variable. A list of errors can be found in intro(2) . The system calls perror(3) and strerror(3) allow you to print out (hopefully) meaningful error messages when you detect a failed system call. See Example: /cluster/examples/perror With Unix system calls, there are a lot of good reasons that something can fail. It’s worth your trouble to check these return conditions and print meaningful error messages. Process Management You need to use a number of Unix system calls related to process management to implement the shell. We have seen a few of these: getpid() – get current process ID getppid() – get parent’s process ID fork() – duplicate process. Child is a copy of the parent - in execution at the same point, the statement after the return from fork() . The return value indicates if you are child or parent. 0 is child, > 0 means parent, -1 means failure (limit reached, permission denied) Example: pid=fork(); if (pid) { parent stuff;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CS 322 Operating Systems Spring 2008 } else { child stuff; } exit() – terminate a process. If it’s a child, it waits for its parent to accept its return code. If this doesn’t happen, the child is called a “zombie” process. To avoid this – call wait() (or waitpid() ) from the parent – parent stops and waits for the child to terminate (call exit() or exit() ). Returns PID of child, and in its argument, the status includes the value the child passed to exit() . Recall example from earlier:
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 6

sysprog - Computer Science 322 Operating Systems Mount...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online