feat: signal flag (data readiness)
This commit is contained in:
parent
259a47dc11
commit
03d136aba0
36
init.c
36
init.c
@ -9,6 +9,11 @@
|
|||||||
#define PATHNAME "."
|
#define PATHNAME "."
|
||||||
#define PROJ_ID 65
|
#define PROJ_ID 65
|
||||||
|
|
||||||
|
struct shared_data {
|
||||||
|
int ready;
|
||||||
|
int priorities[NUM_PROCS];
|
||||||
|
};
|
||||||
|
|
||||||
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)
|
// Creation of a pipe (child -> parent)
|
||||||
@ -34,7 +39,7 @@ void write_pipes(int pipefds[][2], pid_t pids[]) {
|
|||||||
srand(time(NULL) ^ getpid());
|
srand(time(NULL) ^ getpid());
|
||||||
int priority = rand() % 20;
|
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));
|
write(pipefds[i][1], &priority, sizeof(priority));
|
||||||
close(pipefds[i][1]);
|
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)
|
// Write end of the pipe is closed before reading (sync)
|
||||||
close(pipefds[i][1]);
|
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]));
|
read(pipefds[i][0], &priorities[i], sizeof(priorities[i]));
|
||||||
close(pipefds[i][0]);
|
close(pipefds[i][0]);
|
||||||
|
|
||||||
@ -59,7 +64,7 @@ void read_pipes(int pipefds[][2], int priorities[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void write_mem_segment(int priorities[]) {
|
void write_mem_segment(int priorities[]) {
|
||||||
int *shm_array;
|
struct shared_data *shm_ptr;
|
||||||
int shmid;
|
int shmid;
|
||||||
|
|
||||||
key_t key = ftok(PATHNAME, PROJ_ID);
|
key_t key = ftok(PATHNAME, PROJ_ID);
|
||||||
@ -71,38 +76,47 @@ void write_mem_segment(int priorities[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Attach (system chooses a suitable address)
|
// 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");
|
perror("[!] Shmat failed");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("[INIT] Connected to shared segment %d (%p)\n", shmid,
|
printf("[INIT] Connected to shared segment %d (%p)\n", shmid,
|
||||||
(void *)&shm_array);
|
(void *)&shm_ptr);
|
||||||
|
|
||||||
// Write
|
// Write
|
||||||
for (int i = 0; i < NUM_PROCS; i++) {
|
for (int i = 0; i < NUM_PROCS; i++) {
|
||||||
shm_array[i] = priorities[i];
|
shm_ptr->priorities[i] = priorities[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Detach
|
// Signal that data is ready
|
||||||
shmdt(shm_array);
|
shm_ptr->ready = 1;
|
||||||
|
|
||||||
printf("[INIT] Wrote priorities to shared memory\n");
|
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 main() {
|
||||||
int pipefds[NUM_PROCS][2];
|
int pipefds[NUM_PROCS][2];
|
||||||
pid_t pids[NUM_PROCS];
|
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);
|
write_pipes(pipefds, pids);
|
||||||
|
|
||||||
int priorities[NUM_PROCS];
|
int priorities[NUM_PROCS];
|
||||||
|
|
||||||
// Reading priorities from the pipes
|
// Read priorities from the pipes
|
||||||
read_pipes(pipefds, priorities);
|
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);
|
write_mem_segment(priorities);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
62
scheduler.c
62
scheduler.c
@ -8,7 +8,12 @@
|
|||||||
#define PATHNAME "."
|
#define PATHNAME "."
|
||||||
#define PROJ_ID 65
|
#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;
|
int shmid;
|
||||||
key_t key = ftok(PATHNAME, PROJ_ID);
|
key_t key = ftok(PATHNAME, PROJ_ID);
|
||||||
|
|
||||||
@ -19,7 +24,8 @@ int create_mem_segment(int **shm_array) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Attach (system chooses a suitable address)
|
// 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");
|
perror("[!] Shmat failed");
|
||||||
exit(EXIT_FAILURE);
|
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 compare(const void *a, const void *b) { return (*(int *)a - *(int *)b); }
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
int *shm_array;
|
struct shared_data *shm_ptr;
|
||||||
|
|
||||||
int delay = 5;
|
// Create and attach into a new shared segment
|
||||||
|
int shmid = create_mem_segment(&shm_ptr);
|
||||||
// Creation and attaching into a new shared segment
|
|
||||||
int shmid = create_mem_segment(&shm_array);
|
|
||||||
printf("[SCHEDULER] Shared segment %d created (%p)\n", shmid,
|
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
|
// Wait until init has finished writing into the segment
|
||||||
printf("[SCHEDULER] Sleeping for %d seconds before reading\n", delay);
|
while (shm_ptr->ready == 0) {
|
||||||
sleep(delay);
|
usleep(100000); // 100 ms (to reduce CPU usage)
|
||||||
|
|
||||||
// Copying array to a local variable
|
|
||||||
int priorities[NUM_PROCS];
|
|
||||||
|
|
||||||
for (int i = 0; i < NUM_PROCS; i++) {
|
|
||||||
priorities[i] = shm_array[i];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: "After scheduler prints the data, init should detach from shared
|
int priorities[NUM_PROCS];
|
||||||
// memory" -> Some mechanism to indicate when init has detached must be put in
|
|
||||||
// place
|
|
||||||
|
|
||||||
// Detaching and deleting the segment
|
// Copy array to a local variable
|
||||||
shmdt(shm_array);
|
for (int i = 0; i < NUM_PROCS; i++) {
|
||||||
shmctl(shmid, IPC_RMID, NULL);
|
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);
|
qsort(&priorities, NUM_PROCS, sizeof(int), compare);
|
||||||
|
|
||||||
printf("[SCHEDULER] Priorities: ");
|
printf("[SCHEDULER] Priorities: ");
|
||||||
|
Loading…
Reference in New Issue
Block a user