Skip to content
This repository was archived by the owner on Jun 15, 2024. It is now read-only.

Commit 401c0c5

Browse files
committed
feat: final modularity
1 parent 5088db2 commit 401c0c5

File tree

10 files changed

+436
-339
lines changed

10 files changed

+436
-339
lines changed

project2/Makefile

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
11
CC=gcc
2-
# TODO: Remove -g ?
3-
CFLAGS=-g -std=gnu99 -Wall -Wextra -Werror -pedantic
2+
CFLAGS=-std=gnu99 -Wall -Wextra -Werror -pedantic
43
LDFLAGS=-pthread
54

6-
.PHONY=zip clean
5+
.PHONY=zip clean clean all
76

8-
proj2: proj2.o
7+
source_files = memory.c processes.c semaphores.c util.c proj2.c
8+
obj_files = $(source_files:.c=.o)
9+
10+
all: proj2
11+
12+
proj2: $(obj_files)
913
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^
1014

11-
proj2.o: proj2.c proj2.h
15+
proj2.o: proj2.c
1216
$(CC) $(CFLAGS) -c $< -o $@
1317

18+
memory.o: memory.c memory.h
19+
$(CC) $(CFLAGS) -c $< -o $@
20+
21+
processes.o: processes.c processes.h
22+
$(CC) $(CFLAGS) -c $< -o $@
23+
24+
semaphores.o: semaphores.c semaphores.h
25+
$(CC) $(CFLAGS) -c $< -o $@
26+
27+
util.o: util.c util.h
28+
$(CC) $(CFLAGS) -c $< -o $@
1429

1530
clear: clean
1631

1732
clean:
18-
rm -f *.o *.out proj2 xspacpe00.zip
33+
rm -f *.o proj2 xspacpe00.zip
1934

2035
zip:
21-
zip xspacpe00.zip *.c *.h makefile
36+
zip xspacpe00.zip *.c *.h Makefile

project2/memory.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <sys/mman.h>
4+
5+
FILE *oFile;
6+
7+
// shared memory for data
8+
int *line;
9+
int *passengers;
10+
int *curr_stop;
11+
int *riders_at_stop;
12+
int *coming;
13+
14+
int initialize_shared_memory(int skier_count, int stops_count) {
15+
oFile = fopen("proj2.out", "w");
16+
if (oFile == NULL) {
17+
fprintf(stderr, "File open failed\n");
18+
return 1;
19+
}
20+
21+
passengers = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE,
22+
MAP_SHARED | MAP_ANONYMOUS, 0, 0);
23+
if (passengers == MAP_FAILED) {
24+
perror("mmap/bus_cap");
25+
return 1;
26+
}
27+
*passengers = 0;
28+
29+
line = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE,
30+
MAP_SHARED | MAP_ANONYMOUS, 0, 0);
31+
if (line == MAP_FAILED) {
32+
perror("mmap/line");
33+
return 1;
34+
}
35+
*line = 1;
36+
37+
curr_stop = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE,
38+
MAP_SHARED | MAP_ANONYMOUS, 0, 0);
39+
if (curr_stop == MAP_FAILED) {
40+
perror("mmap/curr_stop");
41+
return 1;
42+
}
43+
*curr_stop = 1;
44+
45+
// NOTE: I do not whant to recalculate the zero index all the time
46+
riders_at_stop =
47+
mmap(NULL, sizeof(int) * stops_count + 1, PROT_READ | PROT_WRITE,
48+
MAP_SHARED | MAP_ANONYMOUS, 0, 0);
49+
if (riders_at_stop == MAP_FAILED) {
50+
perror("mmap/riders_at_stop");
51+
return 1;
52+
}
53+
for (int i = 1; i < stops_count; i++) {
54+
riders_at_stop[i] = 0;
55+
}
56+
57+
coming = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE,
58+
MAP_SHARED | MAP_ANONYMOUS, 0, 0);
59+
if (coming == MAP_FAILED) {
60+
perror("mmap/comming");
61+
return 1;
62+
}
63+
*coming = skier_count;
64+
65+
return 0;
66+
}
67+
68+
void cleanup_shared_memory(int stops_count) {
69+
munmap(passengers, sizeof(int));
70+
munmap(line, sizeof(int));
71+
munmap(curr_stop, sizeof(int));
72+
munmap(coming, sizeof(int));
73+
munmap(riders_at_stop, sizeof(int) * stops_count);
74+
}

project2/memory.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef MEM_H
2+
#define MEM_H
3+
4+
#include <stdio.h>
5+
6+
extern FILE *oFile;
7+
8+
// shared memory for data
9+
extern int *line;
10+
extern int *passengers;
11+
extern int *curr_stop;
12+
extern int *riders_at_stop;
13+
extern int *coming;
14+
15+
int initialize_shared_memory(int skier_count, int stops_count);
16+
void cleanup_shared_memory(int stops_count);
17+
18+
#endif

project2/processes.c

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#include "processes.h"
2+
#include "memory.h"
3+
#include "semaphores.h"
4+
#include "util.h"
5+
#include <stdbool.h>
6+
#include <unistd.h>
7+
8+
void process_bus(int stops_count, int tb) {
9+
nice_print("BUS: started\n");
10+
11+
while (true) {
12+
(*curr_stop) = 1;
13+
14+
while ((*curr_stop) <= stops_count) {
15+
usleep(random_range(0, tb));
16+
17+
nice_print("BUS: arrived to %d\n", *curr_stop);
18+
19+
// Let skiers board
20+
sem_wait(riders_mutex);
21+
int tmp_riders = riders_at_stop[*curr_stop];
22+
sem_post(riders_mutex);
23+
24+
if (tmp_riders > 0) {
25+
sem_post(bus);
26+
sem_wait(allBoard);
27+
}
28+
29+
nice_print("BUS: leaving %d\n", *curr_stop);
30+
// Depart
31+
(*curr_stop)++;
32+
}
33+
34+
usleep(random_range(0, tb));
35+
36+
nice_print("BUS: arrived to final\n");
37+
38+
sem_wait(pass_mutex);
39+
int tmp = *passengers;
40+
sem_post(pass_mutex);
41+
42+
for (int i = 0; i < tmp; i++) {
43+
sem_post(final_stop);
44+
sem_wait(get_of);
45+
}
46+
47+
nice_print("BUS: leaving final\n");
48+
49+
bool still_waiting = false;
50+
for (int i = 1; i < stops_count + 1; i++) {
51+
sem_wait(riders_mutex);
52+
if (riders_at_stop[i] > 0) {
53+
still_waiting = true;
54+
}
55+
sem_post(riders_mutex);
56+
}
57+
58+
if (!still_waiting && *coming <= 0) {
59+
// If there are still waiting skiers or coming skiers, restart the bus
60+
break;
61+
}
62+
}
63+
64+
DEBUG_PRINT("exiting bus\n");
65+
nice_print("BUS: finish\n");
66+
exit(0);
67+
}
68+
69+
void process_skier(int skier_id, int stop_id, int tl, int bus_cap) {
70+
nice_print("L %d: started\n", skier_id);
71+
72+
usleep(random_range(0, tl));
73+
74+
sem_wait(riders_mutex);
75+
riders_at_stop[stop_id] += 1;
76+
(*coming) -= 1;
77+
sem_post(riders_mutex);
78+
79+
nice_print("L %d: arrived to %d\n", skier_id, stop_id);
80+
81+
// Waiting on BUS
82+
while (true) {
83+
sem_wait(bus);
84+
85+
// BoardBUS()
86+
if (riders_at_stop[*curr_stop] > 0 && *passengers < bus_cap) {
87+
88+
nice_print("L %d: boarding\n", skier_id);
89+
90+
sem_wait(pass_mutex);
91+
sem_wait(riders_mutex);
92+
(*passengers)++;
93+
riders_at_stop[*curr_stop] -= 1;
94+
sem_post(riders_mutex);
95+
sem_post(pass_mutex);
96+
97+
sem_wait(riders_mutex);
98+
int tmp = riders_at_stop[*curr_stop];
99+
sem_post(riders_mutex);
100+
101+
if (tmp == 0) {
102+
sem_post(allBoard);
103+
} else {
104+
sem_post(bus);
105+
}
106+
107+
break;
108+
} else {
109+
sem_post(allBoard);
110+
}
111+
}
112+
113+
sem_wait(final_stop);
114+
sem_wait(pass_mutex);
115+
(*passengers)--;
116+
sem_post(pass_mutex);
117+
sem_post(get_of);
118+
119+
nice_print("L %d: going to ski\n", skier_id);
120+
exit(0);
121+
}

project2/processes.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef PROCESSES_H
2+
#define PROCESSES_H
3+
4+
void process_bus(int stops_count, int tb);
5+
void process_skier(int skier_id, int stop_id, int tl, int bus_cap);
6+
7+
#endif

0 commit comments

Comments
 (0)