advanced unix progamming
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
Advanced UNIX progamming
Fall 2002
Instructor: Ashok SrinivasanLecture 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• 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
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
Week 3 Topics ... continued• File I/OFile I/O
– File descriptorsFile descriptors• open, creat, close, dup, dup2open, creat, close, dup, dup2
– I/O redirectionI/O redirection
• Process managementProcess management– fork, exit, wait, waitpid, execvfork, exit, wait, waitpid, execv
• Pipes– Named and unnamed pipes– Implementing pipe in a shell
File I/O• File descriptors
– open, creat, close, dup, dup2
• I/O redirection
• 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?
File descriptors
• 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
I/O redirection
Process management
• fork
• exit
• wait and waitpid
• execv
• 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
fork()
• 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
exit() and wait()
• 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
execv()