-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path017.c
More file actions
101 lines (89 loc) · 2.57 KB
/
017.c
File metadata and controls
101 lines (89 loc) · 2.57 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
..........................................................................................................................................
Name : 014.c
Author : SHRUTI VERMA
Description : Write a program to execute ls -l | wc.
a. use dup
b. use dup2
c. use fcntl
Date : 10 Sep 2025
..........................................................................................................................................
*/
#include<unistd.h>
#include<stdlib.h>
#include<fcntl.h>
#include<stdio.h>
void byDup() {
int fd[2];
pipe(fd);
if(!fork()) {
close(1);
dup(fd[1]);
close(fd[0]);
execlp("ls", "ls", "-l", NULL);
}
else {
close(0);
dup(fd[0]);
close(fd[1]);
execlp("wc", "wc", NULL);
}
}
void byDup2() {
int fd[2];
pipe(fd);
if(!fork()) {
dup2(fd[1], STDOUT_FILENO);
close(fd[0]);
close(fd[1]);
execlp("ls", "ls", "-l", NULL);
}
else {
dup2(fd[0], STDIN_FILENO);
close(fd[1]);
close(fd[0]);
execlp("wc", "wc", NULL);
}
}
void byFcntl() {
int fd[2];
pipe(fd);
if(!fork()) {
close(1);
fcntl(fd[1], F_DUPFD, STDOUT_FILENO);
close(fd[1]);
close(fd[0]);
execlp("ls", "ls", "-l", NULL);
} else {
close(0);
fcntl(fd[0], F_DUPFD, STDIN_FILENO);
close(fd[0]);
close(fd[1]);
execlp("wc", "wc", NULL);
}
}
int main() {
int n;
printf("1 : dup \t 2 : dup2 \t 3 : fcntl \n enter a choice : ");
scanf("%d/n", &n);
if(n==1) byDup();
if(n==2) byDup2();
if(n==3) byFcntl();
}
/*---------------------------------------------------OUTPUT------------------------------------------------------
vumma@vumma-VivoBook-15-ASUS-Laptop-X507UF:~/Desktop/SS/HOL2$ gcc 017.c -o 017.out
vumma@vumma-VivoBook-15-ASUS-Laptop-X507UF:~/Desktop/SS/HOL2$ ./017.out
1 : dup 2 : dup2 3 : fcntl
enter a choice : 2
9 74 417
vumma@vumma-VivoBook-15-ASUS-Laptop-X507UF:~/Desktop/SS/HOL2$ gcc 017.c -o 017.out
vumma@vumma-VivoBook-15-ASUS-Laptop-X507UF:~/Desktop/SS/HOL2$ ./017.out
1 : dup 2 : dup2 3 : fcntl
enter a choice : 1
9 74 417
vumma@vumma-VivoBook-15-ASUS-Laptop-X507UF:~/Desktop/SS/HOL2$ gcc 017.c -o 017.out
vumma@vumma-VivoBook-15-ASUS-Laptop-X507UF:~/Desktop/SS/HOL2$ ./017.out
1 : dup 2 : dup2 3 : fcntl
enter a choice : 3
9 74 417
*/