-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpt.c
87 lines (73 loc) · 1.95 KB
/
gpt.c
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <semaphore.h>
#include <string.h>
#include <errno.h>
#define SHM_SIZE sizeof(tampon_t)
#define TAMP_SIZE 3
typedef struct {
int counter;
} tampon_t;
sem_t mutex;
sem_t nvide;
void producer(tampon_t *shared_memory) {
while (1) {
// Produce: increase the counter
sem_wait(&nvide);
sem_wait(&mutex);
shared_memory->counter++;
printf("Produced. Counter: %d\n", shared_memory->counter);
sem_post(&mutex);
}
}
void consumer(tampon_t *shared_memory) {
while (1) {
// Consume: decrease the counter
sem_wait(&mutex);
if(shared_memory->counter){
shared_memory->counter--;
printf("Consumed. Counter: %d\n", shared_memory->counter);
sem_post(&nvide);
}
sem_post(&mutex);
sleep(1); // Simulate some work
}
}
int main() {
// Create shared memory
key_t key = ftok("/tmp", 'A');
int shmid = shmget(key, SHM_SIZE, IPC_CREAT | 0666);
tampon_t *shared_memory = (tampon_t *)shmat(shmid, NULL, 0);
shared_memory->counter = 0;
// Initialize semaphores
if(sem_init(&mutex, 1, 1)){
printf("%s", strerror(errno));
system("pause");
}
sem_init(&nvide, 1, 3);
// Fork producer and consumer processes
pid_t producer_pid = fork();
if (producer_pid == 0) {
producer(shared_memory);
exit(0);
}
pid_t consumer_pid = fork();
if (consumer_pid == 0) {
consumer(shared_memory);
exit(0);
}
// Wait for child processes to finish
wait(NULL);
wait(NULL);
// Cleanup: Destroy semaphores and detach shared memory
sem_destroy(&mutex);
sem_destroy(&nvide);
shmdt(shared_memory);
shmctl(shmid, IPC_RMID, NULL);
return 0;
}