-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
120 lines (106 loc) · 2.15 KB
/
main.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
void ftext(const char *url, char ch[40]){
int i=0;
char nome[40] ;
FILE *arq = fopen(url, "r");
printf("PID = %d", getpid());
if(arq == NULL)
printf("Erro, nao foi possivel abrir o arquivo\n");
else
{
while( fscanf(arq, "%s", nome)!=EOF )
{
if(strcmp(nome,ch)==0)
i=i+1;
}
printf("\nTotal da procura neste filho i = %d\n",i);
printf("\n");
fclose(arq);
}
exit(i);
}
int main()
{
char url[]="teste.txt";
char url_1[]="teste_1.txt";
char url_2[]="teste_2.txt";
char url_3[]="teste_3.txt";
char url_4[]="teste_4.txt";
char ch[]="jaqueline";
char ch_1[]="bem";
char ch_2[]="tambem";
char ch_3[]="Ai";
char ch_4[]="La";
FILE *arq;
int i=0;
int total=0;;
int status;
pid_t pid;
// primeiro filho
pid = fork();
if(pid<0)
perror("fork");
else if(pid==0)
{
ftext(url,ch);
}
// 2 filho
pid = fork();
if(pid<0)
perror("fork");
if(pid==0)
{
ftext(url_1,ch_1);
}
// 3 filho
pid = fork();
if(pid<0)
perror("fork");
if(pid==0)
{
ftext(url_2,ch_2);
}
// 4 filho
pid = fork();
if(pid<0)
perror("fork");
if(pid==0)
{
ftext(url_3,ch_3);
}
//5 filho
pid = fork();
if(pid<0)
perror("fork");
if(pid==0)
{
ftext(url_4,ch_4);
}
printf("PID = %d\n", getpid());
pid = wait(&status);
i = WEXITSTATUS(status);
total += i;
printf("PID = %d\n", getpid());
pid = wait(&status);
i = WEXITSTATUS(status);
total += i;
printf("PID = %d\n", getpid());
pid = wait(&status);
i = WEXITSTATUS(status);
total += i;
printf("PID = %d\n", getpid());
pid = wait(&status);
i = WEXITSTATUS(status);
total += i;
printf("PID = %d\n", getpid());
pid = wait(&status);
i = WEXITSTATUS(status);
total += i;
printf("\nTotal %d\n", total);
return 0;
}