#include #include #include #include #include #define NUM_PROCS 4 #define SHM_SIZE 1024 #define PATHNAME "." #define PROJ_ID 65 void write_pipes(int pipefds[][2], pid_t pids[]) { for (int i = 0; i < NUM_PROCS; i++) { // Creation of a pipe (child -> parent) if (pipe(pipefds[i]) == -1) { perror("[!] Pipe failed"); exit(EXIT_FAILURE); } // Creation of a fork pids[i] = fork(); if (pids[i] == -1) { perror("[!] Fork failed"); exit(EXIT_FAILURE); } // If the fork is successfully created if (pids[i] == 0) { // 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; // Writing to the pipe and closing 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); 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]); // Reading from the pipe and closing 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[]) { int *shm_array; 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) if ((shm_array = (int *)shmat(shmid, NULL, 0)) == (int *)-1) { perror("[!] Shmat failed"); exit(EXIT_FAILURE); } printf("[INIT] Connected to shared segment %d (%p)\n", shmid, (void *)&shm_array); // Write for (int i = 0; i < NUM_PROCS; i++) { shm_array[i] = priorities[i]; } // Detach shmdt(shm_array); printf("[INIT] Wrote priorities to shared memory\n"); } int main() { int pipefds[NUM_PROCS][2]; pid_t pids[NUM_PROCS]; // Creation of forks, their respective pipes, and random priorities write_pipes(pipefds, pids); int priorities[NUM_PROCS]; // Reading priorities from the pipes read_pipes(pipefds, priorities); // Attaching and writing priorities to a shared memory segment write_mem_segment(priorities); return 0; }