1 / 11

Advanced UNIX progamming

Advanced UNIX progamming. Fall 2002 Instructor: Ashok Srinivasan Lecture 8. Acknowledgements: The syllabus and power point presentations are modified versions of those by T. Baker and X. Yuan. Announcements. Reading assignment APUE Chapter 7 Section 7.3 is important

newton
Download Presentation

Advanced UNIX progamming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Advanced UNIX progamming Fall 2002 Instructor: Ashok Srinivasan Lecture 8 Acknowledgements: The syllabus and power point presentations are modified versions of those by T. Baker and X. Yuan

  2. Announcements • Reading assignment • APUE Chapter 7 • Section 7.3 is important • You should know the material from 7.1, 7.2, 7.4 – 7.9 from previous courses and classes • APUE Chapter 8 • Sections 8.1-8.3, 8.5-8.6, 8.9-8.10 • You should also understand the idea behind race conditions • APUE Chapter 14 • Section 14.2

  3. Week 3 Topics • UNIX file system • File system abstraction • Directories • File descriptors • Unix API Programming Examples and Techniques • Example with direct IO • open, close, fdopen, lseek, unlink • Variable argument list

  4. Week 3 Topics ... continued • File I/O • File descriptors • open, creat, close, dup, dup2 • I/O redirection • Process management • fork, exit, wait, waitpid, execv • Pipes • Named and unnamed pipes • Implementing pipe in a shell

  5. File I/O • File descriptors • open, creat, close, dup, dup2 • I/O redirection

  6. File descriptors • Implication of the file descriptor/open file descriptions/inode table organization in UNIX • open and creat • search for the first empty slot in the process file descriptor table • allocate an open file description in the file table, which has a pointer to the inode table • See example1.c • dup and dup2 • Duplicate the file descriptor in the process file descriptor table • See example1b.c • Where is the current file position stored?

  7. I/O redirection • All UNIX processes have three predefined files open • stdin -- STDIN_FILENO (0) • stdout -- STDOUT_FILENO (1) • stderr -- STDERR_FILENO (2) • We can redirect the I/O by manipulating these file descriptors • See example2.c and example3.c

  8. Process management • fork • exit • wait and waitpid • execv

  9. fork() • fork() • Create a new process by duplicating the context of the calling process • The calling process is called the parent, and the new process the child • The return value of fork() distinguishes the two processes • See example4.c and example5.c

  10. exit() and wait() • exit (int status) • Clean up the process (for example, close all files) • Tell its parent that it is dying (SIGCHLD) • Tell child processes that it is dying (SIGHUP) • status can be accessed by the parent • wait, waitpid • Wait for a child process to die • pid_t wait(int *stat_loc) • Suspend the calling process to wait for a child process to die • Return the pid and status of the child process • See example6.c

  11. execv() • exec family system calls • Execute a command • Wipes out most of the context • The file descriptor table is kept • We can manipulate the I/O of the command by manipulating the file descriptor table • Anything after this system call will not be executed if the system call is successful • Example • int execv(const char *path, char *argv[]) • See example7.c

More Related