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

@ -1,23 +1,28 @@
#include <stdio.h> #include <stdio.h>
#include <pthread.h> #include <pthread.h>
#include <semaphore.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(){ int main(){
sem_init(&my_semaphore, 0, 5); my_semaphore = sem_open("/my_sem", O_CREAT, 0644, atomic_load(&semaphore_value));
int sem_val; int sem_val = atomic_load(&semaphore_value);
sem_getvalue(&my_semaphore, &sem_val); 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); printf("Semaphore value after extra posts: %d\n", sem_val);
sem_post(&my_semaphore); sem_close(my_semaphore);
sem_post(&my_semaphore); sem_unlink("/my_sem");
sem_post(&my_semaphore);
sem_getvalue(&my_semaphore, &sem_val);
printf("Semaphore value after extra posts: %d\n", sem_val);
sem_destroy(&my_semaphore);
return 0; return 0;
} }

BIN
OS/C/theory/sync/semexpos Executable file

Binary file not shown.

View File

@ -4,8 +4,8 @@
int counter = 0; int counter = 0;
void* increment(void* arg) { void* increment(void* arg) {
for (int i = 0; i < 100000; i++) { for (int i = 0; i < 1000000; i++) {
counter++; counter++; // No synchronization, will lead to race condition
} }
return NULL; return NULL;
} }

View File

@ -1,32 +1,33 @@
#include <stdatomic.h>
#include <stdio.h> #include <stdio.h>
#include <pthread.h> #include <pthread.h>
#include <semaphore.h> #include <semaphore.h>
#include <sys/semaphore.h> #include <dispatch/dispatch.h>
int counter = 0; // shared resource atomic_int counter = 0; // shared resource
sem_t semaphore; // semaphore declaration dispatch_semaphore_t semaphore; // semaphore declaration
void* increment(void* arg) { void* increment(void* arg) {
for (int i = 0; i < 100000; i++) { for (int i = 0; i < 100000; i++) {
sem_wait(&semaphore); // lock dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); // lock
counter++; // critical section atomic_fetch_add(&counter, 1); // critical section
sem_post(&semaphore); // unlock dispatch_semaphore_signal(semaphore); // unlock
} }
return NULL; return NULL;
} }
int main() { int main() {
pthread_t t1, t2; pthread_t t1, t2;
sem_init(&semaphore, 0, 1); // initialize semaphore with value 1 semaphore = dispatch_semaphore_create(1); // initialize semaphore with value 1
pthread_create(&t1, NULL, increment, NULL); pthread_create(&t1, NULL, increment, NULL);
pthread_create(&t2, NULL, increment, NULL); pthread_create(&t2, NULL, increment, NULL);
pthread_join(t1, NULL); pthread_join(t1, NULL);
pthread_join(t2, NULL); pthread_join(t2, NULL);
sem_destroy(&semaphore); dispatch_release(semaphore);
printf("Final Counter Value (with semaphores): %d\n", counter); printf("Final Counter Value (with semaphores): %d\n", atomic_load(&counter));
return 0; return 0;
} }

View File

@ -2,14 +2,15 @@
#include <pthread.h> #include <pthread.h>
#include <semaphore.h> #include <semaphore.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdatomic.h>
int counter = 0; // shared resource atomic_int counter = 0; // shared resource
sem_t *semaphore; // semaphore declaration sem_t *semaphore; // semaphore declaration
void* increment(void* arg) { void* increment(void* arg) {
for (int i = 0; i < 100000; i++) { for (int i = 0; i < 100000; i++) {
sem_wait(semaphore); // lock sem_wait(semaphore); // lock
counter++; // critical section atomic_fetch_add(&counter, 1); // critical section
sem_post(semaphore); // unlock sem_post(semaphore); // unlock
} }
return NULL; return NULL;
@ -37,7 +38,7 @@ int main() {
sem_close(semaphore); sem_close(semaphore);
sem_unlink("/mysem"); 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; return 0;
} }

Binary file not shown.

Binary file not shown.

Binary file not shown.