inter/init.c

131 lines
3.0 KiB
C
Raw Permalink Normal View History

#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h>
#include <time.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];
};
void write_pipes(int pipefds[][2], pid_t pids[]) {
for (int i = 0; i < NUM_PROCS; i++) {
2024-10-14 20:15:32 +02:00
// Create pipe (child -> parent)
if (pipe(pipefds[i]) == -1) {
perror("[!] Pipe failed");
exit(EXIT_FAILURE);
}
2024-10-14 20:15:32 +02:00
// Create fork
pids[i] = fork();
if (pids[i] == -1) {
perror("[!] Fork failed");
exit(EXIT_FAILURE);
}
2024-10-14 20:15:32 +02:00
// Executing inside the child process
if (pids[i] == 0) {
2024-10-14 20:15:32 +02:00
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]);
// Random priority in the range 0-19
srand(time(NULL) ^ getpid());
int priority = rand() % 20;
2024-10-14 11:27:14 +02:00
// Write to the pipe and close it afterwards (sync)
write(pipefds[i][1], &priority, sizeof(priority));
close(pipefds[i][1]);
printf("[P%d] Wrote %d into pipe\n", i + 1, priority);
2024-10-14 20:15:32 +02:00
printf("[P%d] Exiting...\n", i + 1);
exit(0);
}
}
}
void read_pipes(int pipefds[][2], int priorities[]) {
for (int i = 0; i < NUM_PROCS; i++) {
// Write end of the pipe is closed before reading (sync)
close(pipefds[i][1]);
2024-10-14 11:27:14 +02:00
// Read from the pipe and close it afterwards (sync)
read(pipefds[i][0], &priorities[i], sizeof(priorities[i]));
close(pipefds[i][0]);
printf("[INIT] Read %d from P%d\n", priorities[i], i + 1);
}
}
void write_mem_segment(int priorities[]) {
2024-10-14 11:27:14 +02:00
struct shared_data *shm_ptr;
int shmid;
key_t key = ftok(PATHNAME, PROJ_ID);
// Get the segment identifier
if ((shmid = shmget(key, SHM_SIZE, 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) {
perror("[!] Shmat failed");
exit(EXIT_FAILURE);
}
printf("[INIT] Connected to shared segment %d (%p)\n", shmid,
2024-10-14 11:27:14 +02:00
(void *)&shm_ptr);
// Write
for (int i = 0; i < NUM_PROCS; i++) {
2024-10-14 11:27:14 +02:00
shm_ptr->priorities[i] = priorities[i];
}
2024-10-14 11:27:14 +02:00
// Signal that data is ready
shm_ptr->ready = 1;
printf("[INIT] Wrote priorities to shared memory\n");
2024-10-14 20:15:32 +02:00
printf("[INIT] Flipped the signal flag to 1\n");
2024-10-14 11:27:14 +02:00
// Detach
if (shmdt(shm_ptr) == -1) {
perror("[!] Shmdt failed");
exit(EXIT_FAILURE);
}
2024-10-14 20:15:32 +02:00
printf("[INIT] Detached from shared memory\n");
}
int main() {
int pipefds[NUM_PROCS][2];
pid_t pids[NUM_PROCS];
2024-10-14 11:27:14 +02:00
// Create forks, their respective pipes, and random priorities
write_pipes(pipefds, pids);
int priorities[NUM_PROCS];
2024-10-14 11:27:14 +02:00
// Read priorities from the pipes
read_pipes(pipefds, priorities);
2024-10-14 11:27:14 +02:00
// Attach and write priorities to a shared memory segment
write_mem_segment(priorities);
2024-10-14 20:15:32 +02:00
printf("[INIT] Exiting...\n");
return 0;
}