extended code to advanced input functionality

This commit is contained in:
hello 2025-02-09 03:33:42 +05:30
parent 58cf91a06d
commit 0fc6c74996
8 changed files with 34 additions and 27 deletions

View file

@ -2,14 +2,15 @@
#include <pthread.h>
#include <semaphore.h>
#include <fcntl.h>
#include <stdatomic.h>
int counter = 0; // shared resource
atomic_int counter = 0; // shared resource
sem_t *semaphore; // semaphore declaration
void* increment(void* arg) {
for (int i = 0; i < 100000; i++) {
sem_wait(semaphore); // lock
counter++; // critical section
atomic_fetch_add(&counter, 1); // critical section
sem_post(semaphore); // unlock
}
return NULL;
@ -37,7 +38,7 @@ int main() {
sem_close(semaphore);
sem_unlink("/mysem");
printf("Final Counter Value (with semaphores): %d\n", counter);
printf("Final Counter Value (with semaphores): %d\n", atomic_load(&counter));
return 0;
}