-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathex2.c
139 lines (122 loc) · 3.53 KB
/
ex2.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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/******************************************
* Student name: Atara Razin
* Student: 327348934
* Course Exercise Group: 8923106
* Exercise name: Exercise 2
******************************************/
#include <termio.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
void exitElegantly(int sig);
pid_t firstSecond;
pid_t secondSon;
/***********************************************************************
* function name: main
* input: arguments to main. gets the amout of time we want the program to run
* output: returns 0 if it gets to the end
* operation:forks and runs both executable files and sends the output
* of one as the input of the other
*************************************************************************/
int main(int argc, char *argv[]){
int runtTimeOfProgram = atoi(argv[1]);
int stat, waited;
struct sigaction usr_action;
sigset_t block_mask;
sigfillset (&block_mask);
usr_action.sa_handler = exitElegantly;
usr_action.sa_mask = block_mask;
usr_action.sa_flags = 0;
sigaction (SIGALRM, &usr_action, NULL);
alarm(runtTimeOfProgram);
int fd[2];
pipe(fd);
char inputFile[] = "./ex2_inp.out";
pid_t firstSon = fork();
if(firstSon < 0){
perror("error forking");
exit(-1);
}
else if (firstSon == 0) { //son
if(dup2(fd[0],0)<0){
perror("error duping!");
exit(-1);
}
if(close(fd[0])<0){
perror("error closing");
exit(-1);
}
if(close(fd[1])<0){
perror("error closing");
exit(-1);
}
char *args2[] = {inputFile, 0};
int ret_code = execvp(inputFile, args2);
if (ret_code == -1) {
perror("exec failed! ");
return -1;
}
} else { //father
sleep(1);
char updateFile[] = "./ex2_upd.out";
pid_t secondSon = fork();
if(secondSon<0){
perror("error forking");
exit(-1);
}
else if(secondSon == 0)
{//son
char arg1[10000] = {0};
sprintf(arg1, "%d", firstSon);
char *args3[] = {updateFile, arg1,0};
if(dup2(fd[1],1)<0){
perror("error!");
exit(-1);
}
if(close(fd[0])<0){
perror("error closing");
exit(-1);
}
if(close(fd[1])<0){
perror("error");
exit(-1);
}
int ret_code = execvp(updateFile, args3);
if (ret_code == -1) {
perror("exec failed second son! ");
return -1;
}
}
else {//father
int a;
waitpid(firstSon, &a, 0);
waitpid(secondSon, &a, 0);
if(close(fd[0])<0){
perror("eroor closing");
exit(-1);
}
if(close(fd[1])<0){
perror("eroor closing");
exit(-1);
}
}
}
}
/***********************************************************************
* function name: exitElegantly
* input: int sig
* output: exits with 1
* operation:kills both sons and then exits
*************************************************************************/
void exitElegantly(int sig){
if(kill(firstSecond,SIGINT)<0){
perror("trouble killing\n");
exit(-1);
}
if (kill(secondSon,SIGINT)<0){
perror("cant killing\n");
exit(-1);
}
exit(1);
}