extended code to advanced input functionality
This commit is contained in:
parent
58cf91a06d
commit
0fc6c74996
8 changed files with 34 additions and 27 deletions
|
@ -1,23 +1,28 @@
|
|||
#include <stdio.h>
|
||||
#include <pthread.h>
|
||||
#include <semaphore.h>
|
||||
#include <sys/semaphore.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdatomic.h>
|
||||
|
||||
sem_t my_semaphore;
|
||||
atomic_int semaphore_value = 5;
|
||||
sem_t *my_semaphore;
|
||||
|
||||
int main(){
|
||||
sem_init(&my_semaphore, 0, 5);
|
||||
int sem_val;
|
||||
sem_getvalue(&my_semaphore, &sem_val);
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
sem_val = atomic_load(&semaphore_value);
|
||||
printf("Semaphore value after extra posts: %d\n", sem_val);
|
||||
|
||||
sem_post(&my_semaphore);
|
||||
sem_post(&my_semaphore);
|
||||
sem_post(&my_semaphore);
|
||||
|
||||
sem_getvalue(&my_semaphore, &sem_val);
|
||||
printf("Semaphore value after extra posts: %d\n", sem_val);
|
||||
|
||||
sem_destroy(&my_semaphore);
|
||||
sem_close(my_semaphore);
|
||||
sem_unlink("/my_sem");
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue