2024-10-14 11:05:38 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/shm.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#define NUM_PROCS 4
|
|
|
|
#define SHM_SIZE 1024
|
|
|
|
#define PATHNAME "."
|
|
|
|
#define PROJ_ID 65
|
|
|
|
|
2024-10-14 11:27:14 +02:00
|
|
|
struct shared_data {
|
|
|
|
int ready;
|
|
|
|
int priorities[NUM_PROCS];
|
|
|
|
};
|
|
|
|
|
|
|
|
int create_mem_segment(struct shared_data **shm_ptr) {
|
2024-10-14 11:05:38 +02:00
|
|
|
int shmid;
|
|
|
|
key_t key = ftok(PATHNAME, PROJ_ID);
|
|
|
|
|
|
|
|
// Create a new shared segment
|
|
|
|
if ((shmid = shmget(key, SHM_SIZE, IPC_CREAT | 0666)) < 0) {
|
|
|
|
perror("[!] Shmget failed");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attach (system chooses a suitable address)
|
2024-10-14 11:27:14 +02:00
|
|
|
if ((*shm_ptr = (struct shared_data *)shmat(shmid, NULL, 0)) ==
|
|
|
|
(struct shared_data *)-1) {
|
2024-10-14 11:05:38 +02:00
|
|
|
perror("[!] Shmat failed");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return shmid;
|
|
|
|
}
|
|
|
|
|
|
|
|
int compare(const void *a, const void *b) { return (*(int *)a - *(int *)b); }
|
|
|
|
|
|
|
|
int main() {
|
2024-10-14 11:27:14 +02:00
|
|
|
struct shared_data *shm_ptr;
|
2024-10-14 11:05:38 +02:00
|
|
|
|
2024-10-14 11:27:14 +02:00
|
|
|
// Create and attach into a new shared segment
|
|
|
|
int shmid = create_mem_segment(&shm_ptr);
|
2024-10-14 11:09:08 +02:00
|
|
|
printf("[SCHEDULER] Shared segment %d created (%p)\n", shmid,
|
2024-10-14 11:27:14 +02:00
|
|
|
(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");
|
2024-10-14 11:05:38 +02:00
|
|
|
|
|
|
|
// Wait until init has finished writing into the segment
|
2024-10-14 11:27:14 +02:00
|
|
|
while (shm_ptr->ready == 0) {
|
|
|
|
usleep(100000); // 100 ms (to reduce CPU usage)
|
|
|
|
}
|
2024-10-14 11:05:38 +02:00
|
|
|
|
|
|
|
int priorities[NUM_PROCS];
|
|
|
|
|
2024-10-14 11:27:14 +02:00
|
|
|
// Copy array to a local variable
|
2024-10-14 11:05:38 +02:00
|
|
|
for (int i = 0; i < NUM_PROCS; i++) {
|
2024-10-14 11:27:14 +02:00
|
|
|
priorities[i] = shm_ptr->priorities[i];
|
2024-10-14 11:05:38 +02:00
|
|
|
}
|
|
|
|
|
2024-10-14 11:27:14 +02:00
|
|
|
// 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);
|
|
|
|
}
|
2024-10-14 11:05:38 +02:00
|
|
|
|
2024-10-14 11:27:14 +02:00
|
|
|
printf("[SCHEDULER] Shared memory segment %d detached and deleted\n", shmid);
|
2024-10-14 11:05:38 +02:00
|
|
|
|
2024-10-14 11:27:14 +02:00
|
|
|
// Sort and print the priorities
|
2024-10-14 11:05:38 +02:00
|
|
|
qsort(&priorities, NUM_PROCS, sizeof(int), compare);
|
|
|
|
|
|
|
|
printf("[SCHEDULER] Priorities: ");
|
|
|
|
|
|
|
|
for (int i = 0; i < NUM_PROCS; i++) {
|
|
|
|
printf("%d ", priorities[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|