feat: update structure

This commit is contained in:
2024-01-22 14:27:40 +08:00
parent 7836c9185c
commit 3544a28a2e
559 changed files with 120846 additions and 4102 deletions

View File

@@ -0,0 +1,56 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <unistd.h>
#define NUM_PROCESSES 5
int main() {
int i, j, pid;
// We create a new shared variable for our lock
int *lock;
int shmid;
shmid = shmget(IPC_PRIVATE, sizeof(int), IPC_CREAT | 0600);
lock = shmat(shmid, NULL, 0);
// If lock is 1, we get to run our code.
lock[0] = 1;
for (i = 0; i < NUM_PROCESSES; i++) {
if ((pid = fork()) == 0) {
break;
}
}
if (pid == 0) {
// Spin lock until lock is 1
while (lock[0] == 0)
;
// Claim to lock
lock[0] = 0;
printf("I am child %d\n", i);
for (j = i * 10; j < i * 10 + 10; j++) {
printf("%d ", j);
fflush(stdout);
usleep(250000);
}
printf("\n\n");
// Release the lock
lock[0] = 1;
} else {
for (i = 0; i < NUM_PROCESSES; i++)
wait(NULL);
shmdt(lock);
shmctl(shmid, IPC_RMID, 0);
}
}

View File

@@ -0,0 +1,47 @@
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <unistd.h>
#define NUM_PROCESSES 5
int main() {
int shmid;
int i, j, pid;
sem_t* sems[NUM_PROCESSES];
for (int i = 0; i < NUM_PROCESSES; i++) {
shmid = shmget(IPC_PRIVATE, sizeof(sem_t), IPC_CREAT | 0600);
sems[i] = (sem_t*)shmat(shmid, NULL, 0);
}
sem_post(sems[0]);
for (int i = 0; i < NUM_PROCESSES; i++) sem_init(sems[i], 1, 0);
// allow the first process to run
sem_post(sems[0]);
for (i = 0; i < NUM_PROCESSES; i++) {
if ((pid = fork()) == 0) {
break;
}
}
if (pid == 0) {
sem_wait(sems[i]);
printf("I am child %d\n", i);
for (j = i * 10; j < i * 10 + 10; j++) {
printf("%d ", j);
fflush(stdout);
usleep(250000);
}
printf("\n\n");
sem_post(sems[i + 1]);
} else {
for (i = 0; i < NUM_PROCESSES; i++) wait(NULL);
}
}

View File

@@ -0,0 +1,43 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <unistd.h>
#define NUM_PROCESSES 5
int main() {
int i, j, pid;
int *turn;
int shmid;
shmid = shmget(IPC_PRIVATE, sizeof(int), IPC_CREAT | 0600);
turn = shmat(shmid, NULL, 0);
turn[0] = 0;
for (i = 0; i < NUM_PROCESSES; i++) {
if ((pid = fork()) == 0) {
break;
}
}
if (pid == 0) {
while (turn[0] != i)
;
printf("I am child %d\n", i);
for (j = i * 10; j < i * 10 + 10; j++) {
printf("%d ", j);
fflush(stdout);
usleep(250000);
}
printf("\n\n");
turn[0] = i + 1;
} else {
for (i = 0; i < NUM_PROCESSES; i++)
wait(NULL);
}
}

View File

@@ -0,0 +1,33 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <unistd.h>
#define NUM_PROCESSES 5
int main() {
int i, j, pid;
for (i = 0; i < NUM_PROCESSES; i++) {
if ((pid = fork()) == 0) {
break;
}
}
if (pid == 0) {
printf("I am child %d\n", i);
for (j = i * 10; j < i * 10 + 10; j++) {
printf("%d ", j);
fflush(stdout);
usleep(250000);
}
printf("\n\n");
} else {
for (i = 0; i < NUM_PROCESSES; i++)
wait(NULL);
}
}

View File

@@ -0,0 +1,32 @@
//
// Program to create shared semaphores
//
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <unistd.h>
int main() {
int shmid, pid;
sem_t *sem;
shmid = shmget(IPC_PRIVATE, sizeof(sem_t), IPC_CREAT | 0600);
sem = (sem_t *)shmat(shmid, NULL, 0);
sem_init(sem, 1, 0);
if ((pid = fork()) != 0) {
printf("Parent!. Making my child wait for 1 second.\n");
sleep(1);
sem_post(sem);
wait(NULL);
sem_destroy(sem);
shmctl(shmid, IPC_RMID, 0);
} else {
sem_wait(sem);
printf("Child! Waited 1 second for parent.\n");
}
}

View File

@@ -0,0 +1,30 @@
//
// Program to create shared semaphores
//
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
int main() {
sem_t sem;
int pid;
sem_init(&sem, 1, 0);
if ((pid = fork()) != 0) {
printf("Parent! Making my child wait for 1 second.\n");
sleep(1);
sem_post(&sem);
} else {
sem_wait(&sem);
printf("Child! Waited 1 second for parent.\n");
}
if (pid != 0) {
wait(NULL);
sem_destroy(&sem);
}
}

View File

@@ -0,0 +1,55 @@
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h>
int nproc = 0;
// count must be a semaphore
int *count;
sem_t *barrier;
sem_t *countMut;
int barrierShmId, countShmId, countMutShmId;
void init_barrier(int numproc) {
nproc = numproc;
barrierShmId = shmget(IPC_PRIVATE, sizeof(sem_t), IPC_CREAT | 0600);
countMutShmId = shmget(IPC_PRIVATE, sizeof(sem_t), IPC_CREAT | 0600);
countShmId = shmget(IPC_PRIVATE, sizeof(int), IPC_CREAT | 0600);
barrier = (sem_t *)shmat(barrierShmId, NULL, 0);
countMut = (sem_t *)shmat(countMutShmId, NULL, 0);
count = (int *)shmat(countShmId, NULL, 0);
sem_init(barrier, 1, 0);
sem_init(countMut, 1, 1);
}
void reach_barrier() {
sem_wait(countMut);
*count += 1;
sem_post(countMut);
if (*count == nproc) {
sem_post(barrier);
} else {
sem_wait(barrier);
sem_post(barrier);
}
}
void destroy_barrier(int my_pid) {
if (my_pid != 0) {
sem_destroy(barrier);
sem_destroy(countMut);
shmdt(count);
shmdt(barrier);
shmdt(countMut);
shmctl(countShmId, IPC_RMID, 0);
shmctl(barrierShmId, IPC_RMID, 0);
shmctl(countMutShmId, IPC_RMID, 0);
// Destroy the semaphores and detach
// and free any shared memory. Notice
// that we explicity check that it is
// the parent doing it.
}
}

View File

@@ -0,0 +1,3 @@
void init_barrier(int);
void reach_barrier();
void destroy_barrier(int);

View File

@@ -0,0 +1,48 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/wait.h>
#include "barrier.h"
#define NUM_PROCESSES 6
#define MAX_SLEEP 1000000 // Maximum sleep period in microseconds
int main() {
int i, pid;
int sleep_time[NUM_PROCESSES];
init_barrier(NUM_PROCESSES+1);
srand(time(NULL));
for(i=0; i<NUM_PROCESSES; i++) {
// The children will all slip at different amounts of time
sleep_time[i] = (int) (((float) rand() / RAND_MAX) * MAX_SLEEP);
if((pid = fork()) == 0) {
srand(time(NULL));
break;
}
}
if(pid == 0) {
usleep(sleep_time[i]);
// Reach the barrier.
printf("\tChild %d slept for %3.2f seconds and has now reached the barrier\n", i, sleep_time[i]/1000000.0);
reach_barrier();
}
else {
// Parent will just wait at barrier for all children to return
printf("**Parent waiting for children**\n\n");
reach_barrier();
printf("\n**All the children have returned**\n");
// Clean up the process table
for(i=0; i<NUM_PROCESSES; i++)
wait(NULL);
destroy_barrier(pid);
}
}

View File

@@ -0,0 +1,55 @@
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h>
int nproc = 0;
// count must be a semaphore
int *count;
sem_t *barrier;
sem_t *countMut;
int barrierShmId, countShmId, countMutShmId;
void init_barrier(int numproc) {
nproc = numproc;
barrierShmId = shmget(IPC_PRIVATE, sizeof(sem_t), IPC_CREAT | 0600);
countMutShmId = shmget(IPC_PRIVATE, sizeof(sem_t), IPC_CREAT | 0600);
countShmId = shmget(IPC_PRIVATE, sizeof(int), IPC_CREAT | 0600);
barrier = (sem_t *)shmat(barrierShmId, NULL, 0);
countMut = (sem_t *)shmat(countMutShmId, NULL, 0);
count = (int *)shmat(countShmId, NULL, 0);
sem_init(barrier, 1, 0);
sem_init(countMut, 1, 1);
}
void reach_barrier() {
sem_wait(countMut);
*count += 1;
sem_post(countMut);
if (*count == nproc) {
sem_post(barrier);
} else {
sem_wait(barrier);
sem_post(barrier);
}
}
void destroy_barrier(int my_pid) {
if (my_pid != 0) {
sem_destroy(barrier);
sem_destroy(countMut);
shmdt(count);
shmdt(barrier);
shmdt(countMut);
shmctl(countShmId, IPC_RMID, 0);
shmctl(barrierShmId, IPC_RMID, 0);
shmctl(countMutShmId, IPC_RMID, 0);
// Destroy the semaphores and detach
// and free any shared memory. Notice
// that we explicity check that it is
// the parent doing it.
}
}

View File

@@ -0,0 +1,3 @@
void init_barrier(int);
void reach_barrier();
void destroy_barrier(int);

View File

@@ -0,0 +1,101 @@
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include "barrier.h"
#include "config.h"
#define NUM_PROCESSES 8
int main() {
int vect[VECT_SIZE];
int pid;
int largestShmId, smallestShmId;
int *largest, *smallest;
init_barrier(NUM_PROCESSES + 1);
largestShmId =
shmget(IPC_PRIVATE, sizeof(int) * NUM_PROCESSES, IPC_CREAT | 0600);
largest = (int *)shmat(largestShmId, NULL, 0);
smallestShmId =
shmget(IPC_PRIVATE, sizeof(int) * NUM_PROCESSES, IPC_CREAT | 0600);
smallest = (int *)shmat(smallestShmId, NULL, 0);
float per_process_raw = (float)VECT_SIZE / NUM_PROCESSES;
int per_process = (int)per_process_raw;
clock_t start, end;
double time_taken;
if (per_process_raw != (float)per_process) {
printf("Vector size of %d is not divisible by %d processes.\n", VECT_SIZE,
NUM_PROCESSES);
exit(-1);
}
srand(24601);
int i;
for (i = 0; i < VECT_SIZE; i++) {
vect[i] = rand();
}
for (i = 0; i < NUM_PROCESSES; i++) {
pid = fork();
if (pid == 0)
break;
}
int j;
int big = -INT_MAX;
int small = INT_MAX;
if (pid == 0) {
int start = i * per_process;
int end = i * per_process + per_process;
for (j = start; j < end; j++) {
if (vect[j] > big)
big = vect[j];
if (vect[j] < small)
small = vect[j];
}
largest[i] = big;
smallest[i] = small;
reach_barrier();
} else {
reach_barrier();
start = clock();
for (j = 0; j < NUM_PROCESSES; j++) {
if (largest[j] > big)
big = largest[j];
if (smallest[j] < small)
small = smallest[j];
}
end = clock();
time_taken = ((double)end - start) / CLOCKS_PER_SEC;
printf("\nNumber of items: %d\n", VECT_SIZE);
printf("Smallest element is %d\n", small);
printf("Largest element is %d\n", big);
printf("Time taken is %3.2fs\n\n", time_taken);
// Clean up process table
for (j = 0; j < NUM_PROCESSES; j++)
wait(NULL);
destroy_barrier(pid);
shmdt(largest);
shmdt(smallest);
shmctl(largestShmId, IPC_RMID, 0);
shmctl(smallestShmId, IPC_RMID, 0);
}
}

View File

@@ -0,0 +1,41 @@
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <time.h>
#include "config.h"
// Sort through an array of VECT_SIZE integers
int main() {
int vect[VECT_SIZE], i;
clock_t start, end;
double time_taken;
srand(24601);
for(i=0; i<VECT_SIZE; i++) {
vect[i] = rand();
}
// Now find smallest and largest
int smallest = INT_MAX;
int largest = -INT_MAX;
start = clock();
for(i=0; i<VECT_SIZE; i++) {
if(vect[i] < smallest) {
smallest = vect[i];
}
if(vect[i] > largest) {
largest = vect[i];
}
}
end = clock();
time_taken = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("\nNumber of items: %d\n", VECT_SIZE);
printf("Smallest element is %d\n", smallest);
printf("Largest element is %d\n", largest);
printf("Time taken is %3.2f seconds\n\n", time_taken);
}

View File

@@ -0,0 +1,3 @@
#define VECT_SIZE 2000000
#define VECT_NAME "vector.dat"