diff --git a/OS/C/theory/sync/semaextraposts.c b/OS/C/theory/sync/semaextraposts.c index 5196b8e..b171bdf 100644 --- a/OS/C/theory/sync/semaextraposts.c +++ b/OS/C/theory/sync/semaextraposts.c @@ -1,23 +1,28 @@ #include #include #include -#include +#include +#include -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; } diff --git a/OS/C/theory/sync/semexpos b/OS/C/theory/sync/semexpos new file mode 100755 index 0000000..361e656 Binary files /dev/null and b/OS/C/theory/sync/semexpos differ diff --git a/OS/C/theory/sync/withoutsemaphores.c b/OS/C/theory/sync/withoutsemaphores.c index 673c910..8b56760 100644 --- a/OS/C/theory/sync/withoutsemaphores.c +++ b/OS/C/theory/sync/withoutsemaphores.c @@ -4,8 +4,8 @@ int counter = 0; void* increment(void* arg) { - for (int i = 0; i < 100000; i++) { - counter++; + for (int i = 0; i < 1000000; i++) { + counter++; // No synchronization, will lead to race condition } return NULL; } diff --git a/OS/C/theory/sync/withsema.c b/OS/C/theory/sync/withsema.c index 1b3c4dc..8c1c54b 100644 --- a/OS/C/theory/sync/withsema.c +++ b/OS/C/theory/sync/withsema.c @@ -1,32 +1,33 @@ +#include #include #include #include -#include +#include -int counter = 0; // shared resource -sem_t semaphore; // semaphore declaration +atomic_int counter = 0; // shared resource +dispatch_semaphore_t semaphore; // semaphore declaration void* increment(void* arg) { for (int i = 0; i < 100000; i++) { - sem_wait(&semaphore); // lock - counter++; // critical section - sem_post(&semaphore); // unlock + dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); // lock + atomic_fetch_add(&counter, 1); // critical section + dispatch_semaphore_signal(semaphore); // unlock } return NULL; } int main() { 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(&t2, NULL, increment, NULL); pthread_join(t1, 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; } diff --git a/OS/C/theory/sync/withsema4.c b/OS/C/theory/sync/withsema4.c index cd86817..49595b4 100644 --- a/OS/C/theory/sync/withsema4.c +++ b/OS/C/theory/sync/withsema4.c @@ -2,14 +2,15 @@ #include #include #include +#include -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; } diff --git a/OS/C/theory/sync/wosema b/OS/C/theory/sync/wosema index d00b728..333aa55 100755 Binary files a/OS/C/theory/sync/wosema and b/OS/C/theory/sync/wosema differ diff --git a/OS/C/theory/sync/wsem b/OS/C/theory/sync/wsem index 2473cdb..c25a95a 100755 Binary files a/OS/C/theory/sync/wsem and b/OS/C/theory/sync/wsem differ diff --git a/OS/C/theory/sync/wsem4 b/OS/C/theory/sync/wsem4 index 511dc27..36262dd 100755 Binary files a/OS/C/theory/sync/wsem4 and b/OS/C/theory/sync/wsem4 differ