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);
}
}