dup, dup2 - PowerPoint PPT Presentation

About This Presentation
Title:

dup, dup2

Description:

Title: Chap 16. Abstract Data Types Author: R5me Last modified by: Bong-Soo Sohn Created Date: 11/7/2000 6:20:08 AM Document presentation format: ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 11
Provided by: R5m9
Category:
Tags: abstract | dup | dup2 | writing

less

Transcript and Presenter's Notes

Title: dup, dup2


1
dup, dup2
include ltunistd.hgt int dup(int filedes) int
dup2(int filedes, int filedes2) Both return
new file descriptor if OK, -1 on error
  • An existing file descriptor (filedes) is
    duplicated
  • The new file descriptor returned by dup is
    guaranteed to be the lowest numered available
    file descriptor
  • With dup2, we specify the value of the new
    descriptor with the filedes2 argument
  • If filedes2 is already open, it is first closed.
  • Ex) dup2(fd,STDOUT_FILENO)

2
example
  • dup(1)

File descriptor table
File table entry
fd 0
fd 1
fd 2
fd 3
fd 4

3
include ltfcntl.hgt // redirection.c includ
e ltunistd.hgt include ltstdio.hgt include
lterrno.hgt include ltsys/wait.hgt int main(int
argc , char argv) char path
"/bin/ls" char arg0 "ls" pid_t pid
int fd int status if (argc!2)
printf("wrong command. ex) a.out
filename\n") exit(0) pid
fork() if (pid 0) fd
open(argv1, O_WRONLYO_CREATO_TRUNC,
S_IRWXU) dup2(fd, STDOUT_FILENO)
close(fd) if (execl(path, arg0, NULL)
-1) perror("execl") else
close(fd) wait(status)
4
Pipe
who sort
5
pipe
include ltunistd.hgt int pipe(int
filedes2) return 0 if OK, -1 on error
  • Two file descriptors are returned through the fd
    argument
  • fd0 can be used to read from the pipe, and
  • fd1 can be used to write to the pipe
  • Anything that is written on fd1 may be read by
    fd0.
  • This is of no use in a single process.
  • However, between processes, it gives a method of
    communication
  • The pipe() system call gives parent-child
    processes a way to communicate with each other.

6
pipe
  • filedes0 is open for reading and filedes1 is
    open for writing.
  • Output of filedes1 is the input for filedes0

7
pipe after fork
user process
child process
parent process
fd0
fd1
fd0
fd1
fd0
fd1
pipe
pipe
kernel
kernel
8
Pipes
parent ? child parent closes fd1 child
closes fd0
parent ? child parent closes fd0 child
closes fd1
parent
parent
fd1
fd0
9
Pipe example
include ltstdio.hgt // pipe2.c define READ
0 define WRITE 1 char phrase "Stuff this in
your pipe and smoke it" main( ) int
fd2, bytesRead char message100
pipe(fd) if (fork() 0) // child
close(fdREAD) write(fdWRITE,
phrase, strlen(phrase)1)
fprintf(stdout, "d, child write completed.\n",
getpid()) close(fdWRITE)
else // parent close(fdWRITE)
bytesRead read(fdREAD, message, 100)
fprintf(stdout, "d, parent read
completed.\n", getpid()) printf("Read d
bytes s\n", bytesRead,message)
close(fdREAD)
10
include ltsys/types.hgt // pipe.c include
ltunistd.hgt include ltstdio.hgt include
lterrno.hgt include ltsys/wait.hgt int main(int
argc, char argv) char path
"/bin/ls" char arg0 "ls" pid_t pid
int pipefd2 int status
pipe(pipefd) pid fork() if (pid 0)
dup2(pipefd1, STDOUT_FILENO)
close(pipefd0) close(pipefd1)
if (execl(path, arg0, NULL) -1)
perror("execl") else if (fork()
0) dup2(pipefd0,
STDIN_FILENO) close(pipefd0)
close(pipefd1) if
(execl("/bin/cat", "cat", NULL) -1)
perror("execl cat") else
close(pipefd0)
close(pipefd1) wait(status)
wait(status)
Another Example
Write a Comment
User Comments (0)
About PowerShow.com