-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdup2.c
More file actions
67 lines (45 loc) · 1.51 KB
/
dup2.c
File metadata and controls
67 lines (45 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/wait.h>
#include "macros.h"
#define BUFFER_SIZE 1024
// fork(), pipe(), dup2(), execlp()
int main() {
int retvalue, pipe_fd[2];
// Create pipe
SYSC(retvalue, pipe(pipe_fd), "pipe error");
// Create Child Process
pid_t pid;
SYSC(pid, fork(), "fork error");
if (pid == 0) {
// Child Process
// Close pipe file descriptor (read)
SYSC(retvalue, close(pipe_fd[0]), "close error");
// stdout redirection in pipe's write file descriptor
SYSC(retvalue, dup2(pipe_fd[1], STDOUT_FILENO), "dup2 error");
// Close pipe file descriptor (write)
SYSC(retvalue, close(pipe_fd[1]), "close error");
// Exec "ls -la"
execlp("ls", "ls -la", (char *) NULL);
// Exec error
perror("execlp");
exit(EXIT_FAILURE);
} else {
// Father Process
char buffer[BUFFER_SIZE];
// Close pipe file descriptor (write)
SYSC(retvalue, close(pipe_fd[1]), "close error");
// Read from pipe
ssize_t n_read;
SYSC(n_read, read(pipe_fd[0], buffer, BUFFER_SIZE), "read error");
// Write on pipe
SYSC(retvalue, write(STDOUT_FILENO, buffer, n_read), "write error");
// Close pipe file descriptor (read)
SYSC(retvalue, close(pipe_fd[0]), "close error");
// Wait child process
SYSC(retvalue, waitpid(pid, NULL, 0), "waitpid error");
}
return 0;
}