Skip to content

Commit db6e047

Browse files
committed
Corrected lab04
1 parent 6f2811a commit db6e047

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

lab04/average.c

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
#include <stdio.h>
22
#include <stdlib.h>
3-
//#include <unistd.h>
4-
//#include <time.h>
5-
//#include <sys/types.h>
6-
//#include <sys/wait.h>
73
#include <errno.h>
84

95
int main (int argc, char* argv[]) {
@@ -15,11 +11,11 @@ int main (int argc, char* argv[]) {
1511

1612
FILE *f;
1713

18-
i=0;
19-
average=0;
14+
i = 0;
15+
average = 0;
2016

2117
if (argc == 2) {
22-
f=fopen(argv[1], "r");
18+
f = fopen(argv[1], "r");
2319

2420
if (f == NULL) {
2521
perror("ERROR during FOPEN execution: ");
@@ -32,7 +28,7 @@ int main (int argc, char* argv[]) {
3228
}
3329

3430
printf("The average is %d\n", average/i);
35-
exit (0);
31+
exit(0);
3632
} else {
3733
printf("USAGE: ./average <path>\n");
3834
}

lab04/ex03.c

+11-11
Original file line numberDiff line numberDiff line change
@@ -22,48 +22,48 @@ int main() {
2222
perror("ERROR during FORK #1 execution: ");
2323
exit(errno);
2424
}
25-
if (pid==0) {
26-
f=fopen(RFILE,"w");
25+
if (pid == 0) {
26+
f = fopen(RFILE,"w");
2727
/* Generate 100 random numbers */
28-
for (i=0; i<N; i++){
29-
fprintf(f, "%d ", rand()%100);
28+
for (i = 0; i < N; i++){
29+
fprintf(f, "%d ", rand() % 100);
3030
}
3131
fclose(f);
3232
exit(0);
3333
}
3434

3535
wait(NULL);
36-
pid=fork();
36+
pid = fork();
3737
if (pid < 0) {
3838
perror("ERROR during FORK #2 execution: ");
3939
exit(errno);
4040
}
41-
if (pid==0) {
41+
if (pid == 0) {
4242
/* Number of byte of the file */
4343
execl("/usr/bin/wc", "wc", "-m", RFILE, NULL);
4444
}
4545

46-
pid=fork();
46+
pid = fork();
4747
if (pid < 0) {
4848
perror("ERROR during FORK #3 execution: ");
4949
exit(errno);
5050
}
51-
if (pid==0) {
51+
if (pid == 0) {
5252
/* Number of words of the file */
5353
execl("/usr/bin/wc", "wc", "-w", RFILE, NULL);
5454
}
5555

56-
pid=fork();
56+
pid = fork();
5757
if (pid < 0) {
5858
perror("ERROR during FORK #4 execution: ");
5959
exit(errno);
6060
}
61-
if (pid==0) {
61+
if (pid == 0) {
6262
/* Average of all generated number with a custom program */
6363
execl("./average", "./average", RFILE, NULL);
6464
}
6565

66-
while(wait(NULL)>0){};
66+
while(wait(NULL) > 0){};
6767

6868
return 0;
6969
}

0 commit comments

Comments
 (0)