feat: update structure
This commit is contained in:
56
cs2106/labs/lab3/progs/part1/lab3p1-lock.c
Normal file
56
cs2106/labs/lab3/progs/part1/lab3p1-lock.c
Normal 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);
|
||||
}
|
||||
}
|
||||
47
cs2106/labs/lab3/progs/part1/lab3p1-sem.c
Normal file
47
cs2106/labs/lab3/progs/part1/lab3p1-sem.c
Normal 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);
|
||||
}
|
||||
}
|
||||
43
cs2106/labs/lab3/progs/part1/lab3p1-shm.c
Normal file
43
cs2106/labs/lab3/progs/part1/lab3p1-shm.c
Normal 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);
|
||||
}
|
||||
}
|
||||
33
cs2106/labs/lab3/progs/part1/lab3p1.c
Normal file
33
cs2106/labs/lab3/progs/part1/lab3p1.c
Normal 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);
|
||||
}
|
||||
}
|
||||
32
cs2106/labs/lab3/progs/part1/sema-right.c
Normal file
32
cs2106/labs/lab3/progs/part1/sema-right.c
Normal 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");
|
||||
}
|
||||
}
|
||||
30
cs2106/labs/lab3/progs/part1/sema-wrong.c
Normal file
30
cs2106/labs/lab3/progs/part1/sema-wrong.c
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user