From c0cfa7dc9752823fe8bcae4c36eff80767a7abf3 Mon Sep 17 00:00:00 2001 From: ae Date: Mon, 14 Oct 2024 21:15:32 +0300 Subject: [PATCH] extra print statements for live demo --- init.c | 15 +++++++++++---- scheduler.c | 14 +++++++++++++- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/init.c b/init.c index 6f7d283..e6fd317 100644 --- a/init.c +++ b/init.c @@ -16,13 +16,13 @@ struct shared_data { void write_pipes(int pipefds[][2], pid_t pids[]) { for (int i = 0; i < NUM_PROCS; i++) { - // Creation of a pipe (child -> parent) + // Create pipe (child -> parent) if (pipe(pipefds[i]) == -1) { perror("[!] Pipe failed"); exit(EXIT_FAILURE); } - // Creation of a fork + // Create fork pids[i] = fork(); if (pids[i] == -1) { @@ -30,8 +30,11 @@ void write_pipes(int pipefds[][2], pid_t pids[]) { exit(EXIT_FAILURE); } - // If the fork is successfully created + // Executing inside the child process if (pids[i] == 0) { + pid_t child_pid = getpid(); + printf("[P%d] PID: %d\n", i + 1, child_pid); + // Read end of the pipe is closed before writing (sync) close(pipefds[i][0]); @@ -44,6 +47,7 @@ void write_pipes(int pipefds[][2], pid_t pids[]) { close(pipefds[i][1]); printf("[P%d] Wrote %d into pipe\n", i + 1, priority); + printf("[P%d] Exiting...\n", i + 1); exit(0); } @@ -94,6 +98,7 @@ void write_mem_segment(int priorities[]) { shm_ptr->ready = 1; printf("[INIT] Wrote priorities to shared memory\n"); + printf("[INIT] Flipped the signal flag to 1\n"); // Detach if (shmdt(shm_ptr) == -1) { @@ -101,7 +106,7 @@ void write_mem_segment(int priorities[]) { exit(EXIT_FAILURE); } - printf("[INIT] Deteached from shared memory\n"); + printf("[INIT] Detached from shared memory\n"); } int main() { @@ -119,5 +124,7 @@ int main() { // Attach and write priorities to a shared memory segment write_mem_segment(priorities); + printf("[INIT] Exiting...\n"); + return 0; } diff --git a/scheduler.c b/scheduler.c index f9da74e..3ce60a6 100644 --- a/scheduler.c +++ b/scheduler.c @@ -17,6 +17,8 @@ int create_mem_segment(struct shared_data **shm_ptr) { int shmid; key_t key = ftok(PATHNAME, PROJ_ID); + printf("[SCHEDULER] Shared segment key: %d\n", key); + // Create a new shared segment if ((shmid = shmget(key, SHM_SIZE, IPC_CREAT | 0666)) < 0) { perror("[!] Shmget failed"); @@ -73,11 +75,19 @@ int main() { } printf("[SCHEDULER] Shared memory segment %d detached and deleted\n", shmid); + printf("[SCHEDULER] Unsorted priorities: "); + + // Print the priorities before sorting (for comparison) + for (int i = 0; i < NUM_PROCS; i++) { + printf("%d ", priorities[i]); + } + + printf("\n"); // Sort and print the priorities qsort(&priorities, NUM_PROCS, sizeof(int), compare); - printf("[SCHEDULER] Priorities: "); + printf("[SCHEDULER] Sorted priorities: "); for (int i = 0; i < NUM_PROCS; i++) { printf("%d ", priorities[i]); @@ -85,5 +95,7 @@ int main() { printf("\n"); + printf("[SCHEDULER] Exiting...\n"); + return 0; }