124 lines
2.8 KiB
C
124 lines
2.8 KiB
C
#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
|
|
|
|
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++) {
|
|
// 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;
|
|
|
|
// 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);
|
|
|
|
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]);
|
|
|
|
// 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[]) {
|
|
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)
|
|
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,
|
|
(void *)&shm_ptr);
|
|
|
|
// Write
|
|
for (int i = 0; i < NUM_PROCS; i++) {
|
|
shm_ptr->priorities[i] = priorities[i];
|
|
}
|
|
|
|
// Signal that data is ready
|
|
shm_ptr->ready = 1;
|
|
|
|
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 pipefds[NUM_PROCS][2];
|
|
pid_t pids[NUM_PROCS];
|
|
|
|
// Create forks, their respective pipes, and random priorities
|
|
write_pipes(pipefds, pids);
|
|
|
|
int priorities[NUM_PROCS];
|
|
|
|
// Read priorities from the pipes
|
|
read_pipes(pipefds, priorities);
|
|
|
|
// Attach and write priorities to a shared memory segment
|
|
write_mem_segment(priorities);
|
|
|
|
return 0;
|
|
}
|