2025-02-08 12:16:29 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <semaphore.h>
|
2025-02-09 03:33:42 +05:30
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdatomic.h>
|
2025-02-08 12:16:29 +05:30
|
|
|
|
2025-02-09 03:33:42 +05:30
|
|
|
atomic_int semaphore_value = 5;
|
|
|
|
sem_t *my_semaphore;
|
2025-02-08 12:16:29 +05:30
|
|
|
|
|
|
|
int main(){
|
2025-02-09 03:33:42 +05:30
|
|
|
my_semaphore = sem_open("/my_sem", O_CREAT, 0644, atomic_load(&semaphore_value));
|
|
|
|
int sem_val = atomic_load(&semaphore_value);
|
|
|
|
printf("Initial semaphore value: %d\n", sem_val);
|
2025-02-08 12:16:29 +05:30
|
|
|
|
2025-02-09 03:33:42 +05:30
|
|
|
sem_post(my_semaphore);
|
|
|
|
atomic_fetch_add(&semaphore_value, 1);
|
|
|
|
sem_post(my_semaphore);
|
|
|
|
atomic_fetch_add(&semaphore_value, 1);
|
|
|
|
sem_post(my_semaphore);
|
|
|
|
atomic_fetch_add(&semaphore_value, 1);
|
2025-02-08 12:16:29 +05:30
|
|
|
|
2025-02-09 03:33:42 +05:30
|
|
|
sem_val = atomic_load(&semaphore_value);
|
2025-02-08 12:16:29 +05:30
|
|
|
printf("Semaphore value after extra posts: %d\n", sem_val);
|
|
|
|
|
2025-02-09 03:33:42 +05:30
|
|
|
sem_close(my_semaphore);
|
|
|
|
sem_unlink("/my_sem");
|
2025-02-08 12:16:29 +05:30
|
|
|
return 0;
|
|
|
|
}
|