-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiping.c
59 lines (57 loc) · 1.49 KB
/
piping.c
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
#include "header.h"
void processPipe(char* input)
{
// Basic Idea
// Create an array of pipes
// n processes => n pipes (n-1 used)
// Pipe input of one to ouput of the other
// Output of last is piped to cout
// Important => Pipe wont read until all write ends are closed
// Didnt create child process (Can create, no difference, MUST CLOSE IN BOTH PROCESSES)
char** instructions = malloc(1000*sizeof(char));
int i=0;
char* tok = strtok(input,"|");
while(tok!=NULL)
{
instructions[i] = malloc(100000*sizeof(char));
instructions[i] = tok;
i++;
tok = strtok(NULL,"|");
}
int j;
int cin_temp = dup(0); // Copy of cin
int cout_temp = dup(1); // Copy of cout
int fd[i][2];
for(j=0;j<i;j++)
{
if(j==(i-1))
{
dup2(cout_temp,1); // Last output is set to cout
output_redirection(instructions[j]);
dup2(cin_temp,0);
if((j-1)!=0)
{
close(fd[j-1][0]);
}
}
else
{
if(pipe(fd[j]) < 0)
{
perror("Pipe creation");
status = 0;
break;
}
dup2(fd[j][1],1);
output_redirection(instructions[j]);
close(fd[j][1]);
dup2(fd[j][0],0);
if(j!=0)
{
close(fd[j-1][0]);
}
}
}
dup2(cout_temp,1);
dup2(cin_temp,0);
}