feat: signal flag (data readiness)

This commit is contained in:
ae 2024-10-14 12:27:14 +03:00
parent 259a47dc11
commit 03d136aba0
Signed by: ae
GPG Key ID: 995EFD5C1B532B3E
2 changed files with 64 additions and 34 deletions

36
init.c
View File

@ -9,6 +9,11 @@
#define PATHNAME "."
#define PROJ_ID 65
struct shared_data {
int ready;
int priorities[NUM_PROCS];
};
void write_pipes(int pipefds[][2], pid_t pids[]) {
for (int i = 0; i < NUM_PROCS; i++) {
// Creation of a pipe (child -> parent)
@ -34,7 +39,7 @@ void write_pipes(int pipefds[][2], pid_t pids[]) {
srand(time(NULL) ^ getpid());
int priority = rand() % 20;
// Writing to the pipe and closing it afterwards (sync)
// Write to the pipe and close it afterwards (sync)
write(pipefds[i][1], &priority, sizeof(priority));
close(pipefds[i][1]);
@ -50,7 +55,7 @@ void read_pipes(int pipefds[][2], int priorities[]) {
// Write end of the pipe is closed before reading (sync)
close(pipefds[i][1]);
// Reading from the pipe and closing it afterwards (sync)
// Read from the pipe and close it afterwards (sync)
read(pipefds[i][0], &priorities[i], sizeof(priorities[i]));
close(pipefds[i][0]);
@ -59,7 +64,7 @@ void read_pipes(int pipefds[][2], int priorities[]) {
}
void write_mem_segment(int priorities[]) {
int *shm_array;
struct shared_data *shm_ptr;
int shmid;
key_t key = ftok(PATHNAME, PROJ_ID);
@ -71,38 +76,47 @@ void write_mem_segment(int priorities[]) {
}
// Attach (system chooses a suitable address)
if ((shm_array = (int *)shmat(shmid, NULL, 0)) == (int *)-1) {
if ((shm_ptr = (struct shared_data *)shmat(shmid, NULL, 0)) ==
(struct shared_data *)-1) {
perror("[!] Shmat failed");
exit(EXIT_FAILURE);
}
printf("[INIT] Connected to shared segment %d (%p)\n", shmid,
(void *)&shm_array);
(void *)&shm_ptr);
// Write
for (int i = 0; i < NUM_PROCS; i++) {
shm_array[i] = priorities[i];
shm_ptr->priorities[i] = priorities[i];
}
// Detach
shmdt(shm_array);
// Signal that data is ready
shm_ptr->ready = 1;
printf("[INIT] Wrote priorities to shared memory\n");
// Detach
if (shmdt(shm_ptr) == -1) {
perror("[!] Shmdt failed");
exit(EXIT_FAILURE);
}
printf("[INIT] Deteached from shared memory\n");
}
int main() {
int pipefds[NUM_PROCS][2];
pid_t pids[NUM_PROCS];
// Creation of forks, their respective pipes, and random priorities
// Create forks, their respective pipes, and random priorities
write_pipes(pipefds, pids);
int priorities[NUM_PROCS];
// Reading priorities from the pipes
// Read priorities from the pipes
read_pipes(pipefds, priorities);
// Attaching and writing priorities to a shared memory segment
// Attach and write priorities to a shared memory segment
write_mem_segment(priorities);
return 0;

View File

@ -8,7 +8,12 @@
#define PATHNAME "."
#define PROJ_ID 65
int create_mem_segment(int **shm_array) {
struct shared_data {
int ready;
int priorities[NUM_PROCS];
};
int create_mem_segment(struct shared_data **shm_ptr) {
int shmid;
key_t key = ftok(PATHNAME, PROJ_ID);
@ -19,7 +24,8 @@ int create_mem_segment(int **shm_array) {
}
// Attach (system chooses a suitable address)
if ((*shm_array = (int *)shmat(shmid, NULL, 0)) == (int *)-1) {
if ((*shm_ptr = (struct shared_data *)shmat(shmid, NULL, 0)) ==
(struct shared_data *)-1) {
perror("[!] Shmat failed");
exit(EXIT_FAILURE);
}
@ -30,35 +36,45 @@ int create_mem_segment(int **shm_array) {
int compare(const void *a, const void *b) { return (*(int *)a - *(int *)b); }
int main() {
int *shm_array;
struct shared_data *shm_ptr;
int delay = 5;
// Creation and attaching into a new shared segment
int shmid = create_mem_segment(&shm_array);
// Create and attach into a new shared segment
int shmid = create_mem_segment(&shm_ptr);
printf("[SCHEDULER] Shared segment %d created (%p)\n", shmid,
(void *)shm_array);
(void *)shm_ptr);
// Initialize signal flag to 0 (not ready)
shm_ptr->ready = 0;
printf("[SCHEDULER] Signal flag initialized to 0\n");
printf("[SCHEDULER] Waiting for init to write data...\n");
// Wait until init has finished writing into the segment
printf("[SCHEDULER] Sleeping for %d seconds before reading\n", delay);
sleep(delay);
// Copying array to a local variable
int priorities[NUM_PROCS];
for (int i = 0; i < NUM_PROCS; i++) {
priorities[i] = shm_array[i];
while (shm_ptr->ready == 0) {
usleep(100000); // 100 ms (to reduce CPU usage)
}
// TODO: "After scheduler prints the data, init should detach from shared
// memory" -> Some mechanism to indicate when init has detached must be put in
// place
int priorities[NUM_PROCS];
// Detaching and deleting the segment
shmdt(shm_array);
shmctl(shmid, IPC_RMID, NULL);
// Copy array to a local variable
for (int i = 0; i < NUM_PROCS; i++) {
priorities[i] = shm_ptr->priorities[i];
}
// Sorting and printing the priorities
// Detach
if (shmdt(shm_ptr) == -1) {
perror("[!] Shmdt failed");
exit(EXIT_FAILURE);
}
// Delete
if (shmctl(shmid, IPC_RMID, NULL) == -1) {
perror("[!] Shmctl failed");
exit(EXIT_FAILURE);
}
printf("[SCHEDULER] Shared memory segment %d detached and deleted\n", shmid);
// Sort and print the priorities
qsort(&priorities, NUM_PROCS, sizeof(int), compare);
printf("[SCHEDULER] Priorities: ");