e-fólio A 2020-21 UC:21111 - Sistemas Operativos
Aluno: 2000027 - Hélio Sousa
dit.c
Ir para a documentação deste ficheiro.
1
/*
2
* UC:21111 - Sistemas Operativos
3
* e-fólio A 2020-21
4
*
5
* Aluno: 2000027 - Hélio Sousa
6
*/
7
8
#include <stdio.h>
9
#include <stdlib.h>
10
#include <string.h>
11
#include <dirent.h>
12
#include <errno.h>
13
#include <unistd.h>
14
#include <sys/types.h>
15
#include <sys/wait.h>
16
20
extern
char
**
environ
;
21
30
int
valida_args
(
int
argc ) {
31
32
if
(argc != 2) {
33
printf (
"ERRO: número de argumentos inválido.\n"
);
34
return
-1;
35
}
36
else
{
37
return
0;
38
}
39
}
40
49
int
valida_dir
(
char
*argv[]) {
50
51
DIR *dirptr = opendir(argv[1]);
52
53
if
(ENOENT == errno) {
54
printf (
"Diretório inválido ou inexistente: %s\n"
,argv[1]);
55
return
-1;
56
}
57
else
{
58
closedir(dirptr);
59
return
0;
60
}
61
}
62
68
int
fork_check
(pid_t pid) {
69
if
( pid < 0 ) {
70
printf (
"Erro na funcao fork()"
);
71
return
-1;
72
}
73
else
{
74
return
0;
75
}
76
}
77
83
void
msg_processo
(
char
ch) {
84
printf(
"Processo %c: PID=%5d PPID=%5d\n"
, ch, (
int
) getpid(), (
int
) getppid());
85
}
86
92
void
msg_erro_processo
(
char
ch) {
93
printf(
"Se esta mensagem aparecer ocorreu um erro no processo%c!"
,ch);
94
}
95
101
void
processo_Bls
(
char
*argv[]) {
102
freopen(
"tmp1.txt"
,
"w+"
, stdout);
103
execl(
"/bin/ls"
,
"ls"
,
"-1"
,argv[1], NULL);
104
msg_erro_processo
(
'B'
);
105
}
106
111
void
processo_Cecho
() {
112
freopen(
"tmp1.txt"
,
"a+"
, stdout);
113
execlp(
"echo"
,
"echo"
,
"-n"
,
"Itens encontrados: "
, NULL);
114
msg_erro_processo
(
'C'
);
115
}
116
124
void
processo_Dwc
() {
125
freopen(
"tmp1.txt"
,
"r"
, stdin);
126
freopen(
"tmp2.txt"
,
"w+"
, stdout);
127
char
*arguments[1];
128
arguments[0] =
"wc"
;
129
arguments[1] =
"-l"
;
130
arguments[2] = NULL;
131
execv(
"/bin/wc"
, arguments);
132
msg_erro_processo
(
'D'
);
133
}
134
140
void
processo_Ecat
() {
141
char
*arguments[3];
142
arguments[0] =
"cat"
;
143
arguments[1] =
"tmp1.txt"
;
144
arguments[2] =
"tmp2.txt"
;
145
arguments[3] = NULL;
146
execvp(
"cat"
, arguments);
147
msg_erro_processo
(
'E'
);
148
}
149
150
161
int
main
(
int
argc,
char
** argv) {
162
163
pid_t pidB,pidC,pidD,pidE;
164
int
status;
165
166
// Processo A
167
if
(
valida_args
(argc))
return
(EXIT_FAILURE);
168
if
(
valida_dir
(argv))
return
(EXIT_FAILURE);
169
170
pidB=fork();
171
if
(
fork_check
(pidB))
return
(EXIT_FAILURE);
172
173
if
( pidB == 0 ) {
174
// Processo B
175
msg_processo
(
'B'
);
176
processo_Bls
(argv);
177
}
178
else
{
179
msg_processo
(
'A'
);
180
waitpid(pidB,&status,0);
181
pidC=fork();
182
if
(
fork_check
(pidC))
return
(EXIT_FAILURE);
183
if
(pidC == 0) {
184
// Processo C
185
msg_processo
(
'C'
);
186
processo_Cecho
();
187
}
188
else
{
189
waitpid(pidC,&status,0);
190
pidD=fork();
191
if
(
fork_check
(pidD))
return
(EXIT_FAILURE);
192
if
(pidD == 0){
193
// Processo D
194
msg_processo
(
'D'
);
195
processo_Dwc
();
196
}
197
else
{
198
waitpid(pidD,&status,0);
199
pidE=fork();
200
if
(
fork_check
(pidE))
return
(EXIT_FAILURE);
201
if
(pidE == 0) {
202
// Processo E
203
msg_processo
(
'E'
);
204
processo_Ecat
();
205
}
206
else
{
207
waitpid(pidE,&status,0);
208
//termina processoA(dit);
209
}
210
}
211
}
212
}
213
return
(EXIT_SUCCESS);
214
}
processo_Ecat
void processo_Ecat()
Definition:
dit.c:140
processo_Bls
void processo_Bls(char *argv[])
Definition:
dit.c:101
processo_Dwc
void processo_Dwc()
Definition:
dit.c:124
main
int main(int argc, char **argv)
Definition:
dit.c:161
msg_processo
void msg_processo(char ch)
Definition:
dit.c:83
fork_check
int fork_check(pid_t pid)
Definition:
dit.c:68
environ
char ** environ
valida_dir
int valida_dir(char *argv[])
Definition:
dit.c:49
processo_Cecho
void processo_Cecho()
Definition:
dit.c:111
msg_erro_processo
void msg_erro_processo(char ch)
Definition:
dit.c:92
valida_args
int valida_args(int argc)
Definition:
dit.c:30
dit.c
Gerado por
1.8.17