Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<stdio.h>
#include<sys/mman.h>
#include<stdlib.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/wait.h>
#include<string.h>
#define SIZE 10000
#define SCRATCH_FILE "./shared-file"
void swap(int *xp, int *yp);
void sort(int arr[], int size);
int main() {
int arr[SIZE];
for(int i = SIZE - 1; i >= 0; i--)
arr[(SIZE - 1) - i] = i % 2;
// TODO: Fork another process here
// TODO: One process should calculate the median
// TODO: The other process should calculate the mean
// TODO: Use shm_open, ftruncate, mmap, and munmap to communicate over mapped files
// TODO: Print both calculated results and involved PIDs
return 0;
}
void swap(int *xp, int *yp) {
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void sort(int *arr, int size) {
for (int i = 0; i < size - 1; i++)
for (int j = 0; j < size - i - 1; j++)
if (arr[j] > arr[j + 1])
swap(&arr[j], &arr[j + 1]);
}