extra print statements for live demo

This commit is contained in:
ae 2024-10-14 21:15:32 +03:00
parent 03d136aba0
commit c0cfa7dc97
Signed by: ae
GPG Key ID: 995EFD5C1B532B3E
2 changed files with 24 additions and 5 deletions

15
init.c
View File

@ -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;
}

View File

@ -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;
}