extra print statements for live demo
This commit is contained in:
parent
03d136aba0
commit
c0cfa7dc97
15
init.c
15
init.c
@ -16,13 +16,13 @@ struct shared_data {
|
|||||||
|
|
||||||
void write_pipes(int pipefds[][2], pid_t pids[]) {
|
void write_pipes(int pipefds[][2], pid_t pids[]) {
|
||||||
for (int i = 0; i < NUM_PROCS; i++) {
|
for (int i = 0; i < NUM_PROCS; i++) {
|
||||||
// Creation of a pipe (child -> parent)
|
// Create pipe (child -> parent)
|
||||||
if (pipe(pipefds[i]) == -1) {
|
if (pipe(pipefds[i]) == -1) {
|
||||||
perror("[!] Pipe failed");
|
perror("[!] Pipe failed");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creation of a fork
|
// Create fork
|
||||||
pids[i] = fork();
|
pids[i] = fork();
|
||||||
|
|
||||||
if (pids[i] == -1) {
|
if (pids[i] == -1) {
|
||||||
@ -30,8 +30,11 @@ void write_pipes(int pipefds[][2], pid_t pids[]) {
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the fork is successfully created
|
// Executing inside the child process
|
||||||
if (pids[i] == 0) {
|
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)
|
// Read end of the pipe is closed before writing (sync)
|
||||||
close(pipefds[i][0]);
|
close(pipefds[i][0]);
|
||||||
|
|
||||||
@ -44,6 +47,7 @@ void write_pipes(int pipefds[][2], pid_t pids[]) {
|
|||||||
close(pipefds[i][1]);
|
close(pipefds[i][1]);
|
||||||
|
|
||||||
printf("[P%d] Wrote %d into pipe\n", i + 1, priority);
|
printf("[P%d] Wrote %d into pipe\n", i + 1, priority);
|
||||||
|
printf("[P%d] Exiting...\n", i + 1);
|
||||||
|
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
@ -94,6 +98,7 @@ void write_mem_segment(int priorities[]) {
|
|||||||
shm_ptr->ready = 1;
|
shm_ptr->ready = 1;
|
||||||
|
|
||||||
printf("[INIT] Wrote priorities to shared memory\n");
|
printf("[INIT] Wrote priorities to shared memory\n");
|
||||||
|
printf("[INIT] Flipped the signal flag to 1\n");
|
||||||
|
|
||||||
// Detach
|
// Detach
|
||||||
if (shmdt(shm_ptr) == -1) {
|
if (shmdt(shm_ptr) == -1) {
|
||||||
@ -101,7 +106,7 @@ void write_mem_segment(int priorities[]) {
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("[INIT] Deteached from shared memory\n");
|
printf("[INIT] Detached from shared memory\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
@ -119,5 +124,7 @@ int main() {
|
|||||||
// Attach and write priorities to a shared memory segment
|
// Attach and write priorities to a shared memory segment
|
||||||
write_mem_segment(priorities);
|
write_mem_segment(priorities);
|
||||||
|
|
||||||
|
printf("[INIT] Exiting...\n");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
14
scheduler.c
14
scheduler.c
@ -17,6 +17,8 @@ int create_mem_segment(struct shared_data **shm_ptr) {
|
|||||||
int shmid;
|
int shmid;
|
||||||
key_t key = ftok(PATHNAME, PROJ_ID);
|
key_t key = ftok(PATHNAME, PROJ_ID);
|
||||||
|
|
||||||
|
printf("[SCHEDULER] Shared segment key: %d\n", key);
|
||||||
|
|
||||||
// Create a new shared segment
|
// Create a new shared segment
|
||||||
if ((shmid = shmget(key, SHM_SIZE, IPC_CREAT | 0666)) < 0) {
|
if ((shmid = shmget(key, SHM_SIZE, IPC_CREAT | 0666)) < 0) {
|
||||||
perror("[!] Shmget failed");
|
perror("[!] Shmget failed");
|
||||||
@ -73,11 +75,19 @@ int main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
printf("[SCHEDULER] Shared memory segment %d detached and deleted\n", shmid);
|
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
|
// Sort and print the priorities
|
||||||
qsort(&priorities, NUM_PROCS, sizeof(int), compare);
|
qsort(&priorities, NUM_PROCS, sizeof(int), compare);
|
||||||
|
|
||||||
printf("[SCHEDULER] Priorities: ");
|
printf("[SCHEDULER] Sorted priorities: ");
|
||||||
|
|
||||||
for (int i = 0; i < NUM_PROCS; i++) {
|
for (int i = 0; i < NUM_PROCS; i++) {
|
||||||
printf("%d ", priorities[i]);
|
printf("%d ", priorities[i]);
|
||||||
@ -85,5 +95,7 @@ int main() {
|
|||||||
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
|
printf("[SCHEDULER] Exiting...\n");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user