Minishell 2

8
Minishell 2 InKwan Yu iyu@ cise . ufl . edu

description

Minishell 2. InKwan Yu [email protected]. Topics. Piping dup() dup2() Quiz & QnA. Piping. int pfd[2]; pipe(pfd); // note: called in parent if (fork() == 0) { // first child close(pfd[0]); dup2(pfd[1], STDOUT_FILENO); close(pfd[1]); …. } - PowerPoint PPT Presentation

Transcript of Minishell 2

Page 1: Minishell 2

Minishell 2

InKwan [email protected]

Page 2: Minishell 2

Topics Piping dup() dup2() Quiz & QnA

Page 3: Minishell 2

Piping int pfd[2];

pipe(pfd); // note: called in parent

if (fork() == 0) { // first child

close(pfd[0]); dup2(pfd[1], STDOUT_FILENO); close(pfd[1]);

….

}

if (fork() == 0) { // second child

close(pfd[1]); dup2(pfd[0], STDIN_FILENO); close(pfd[0]);

….

}

close(pfd[0]); close(pfd[1]); // don't forget this

Page 4: Minishell 2

Piping

fd 0fd 1fd 2fd 3fd 4

File pos

refcnt==1

...

File pos

refcnt==1

...

Child 1's table

fd 0fd 1fd 2fd 3fd 4

Child 2's table

File access

...

File size

File type

pipe 0

pipe 1

fd 0fd 1fd 2fd 3fd 4

Parent's table

stdoutstdin

stdoutstdin

Page 5: Minishell 2

dup() dup(fd)

Finds the first empty slot in the process file descriptor table and duplicates fd to it

To duplicate an FD to STDOUT Close STDOUT and make sure it’s the

first empty slot (STDIN should be open) close(1); dup(fd);

Page 6: Minishell 2

dup2() dup(fromfd, tofd)

Closes the target descriptor if it’s already open

Duplicates fromfd to tofd To duplicate an FD to STDOUT

dup2(fd, 1); dup2() is easier than dup()

Page 7: Minishell 2

dup()/dup2() Applications CGIs

Web server redirects a network socket descriptor STDIN and STDOUT of a CGI

Internet Super Daemon (inetd) inetd brings a network server when

needed. ftpd, telnetd, etc support this feature Network socket descriptor is redirected to

STDIN and STDOUT

Page 8: Minishell 2

Quiz & QnA