Docsity

UNIX File I/O and Pipes: File Descriptors, open(), pipe(), and Implementation in Shell, Slides of Computer Programming

Partial preview of the text

Download UNIX File I/O and Pipes: File Descriptors, open(), pipe(), and Implementation in Shell and more Slides Computer Programming in PDF only on Docsity!

Review

  • 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

Review

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

Pipes

• Named pipe ( not our emphasis )

• Unnamed pipe

• Implementing pipe in a shell

Named and unnamed pipes

  • Named pipe ( not our emphasis )
    • Like a file (create a named pipe ( mknod ), open,

read/write)

  • Can be shared by any process
  • Unnamed pipe
  • An unnamed pipe does not associate with any

physical file

  • It can only be shared by related processes

(descendants of a process that creates the

unnamed pipe)

  • Created using system call pipe()

An example for the use of a pipe

  • example3.c
    • Once the processes can communicate

with each other, their execution order can

be controlled

Implementing pipe in shell

  • Example

/usr/bin/ps –ef | /usr/bin/more

  • How does the shell realize this command?
    • Create a process to run ps -ef
    • Create a process to run more
    • Create a pipe from ps -ef to more
      • The standard output of the process to run ps -ef is redirected to a pipe streaming to the process to run more
      • The standard input of the process to run more is redirected to be the pipe from the process running ps –ef
    • See example4.c