diff --git a/OS/C/theory/sync/withoutsemaphores.c b/OS/C/theory/sync/withoutsemaphores.c new file mode 100644 index 0000000..673c910 --- /dev/null +++ b/OS/C/theory/sync/withoutsemaphores.c @@ -0,0 +1,21 @@ +#include +#include + +int counter = 0; + +void* increment(void* arg) { + for (int i = 0; i < 100000; i++) { + counter++; + } + return NULL; +} + +int main() { + pthread_t t1, t2; + pthread_create(&t1, NULL, increment, NULL); + pthread_create(&t2, NULL, increment, NULL); + pthread_join(t1, NULL); + pthread_join(t2, NULL); + printf("Final Counter Value (without semaphores): %d\n", counter); + return 0; +} diff --git a/OS/C/theory/sync/withsema.c b/OS/C/theory/sync/withsema.c new file mode 100644 index 0000000..1b3c4dc --- /dev/null +++ b/OS/C/theory/sync/withsema.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include + +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 + sem_post(&semaphore); // unlock + } + return NULL; +} + +int main() { + pthread_t t1, t2; + sem_init(&semaphore, 0, 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); + + printf("Final Counter Value (with semaphores): %d\n", counter); + + return 0; +} diff --git a/OS/C/theory/sync/withsema4.c b/OS/C/theory/sync/withsema4.c new file mode 100644 index 0000000..cd86817 --- /dev/null +++ b/OS/C/theory/sync/withsema4.c @@ -0,0 +1,43 @@ +#include +#include +#include +#include + +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 + sem_post(semaphore); // unlock + } + return NULL; +} + +int main() { + pthread_t t1, t2, t3, t4; + + // MacOS specific semaphore definition. + semaphore = sem_open("/mysem", O_CREAT, 0644, 1); + if (semaphore == SEM_FAILED) { + perror("sem_open failed"); + return 1; + } + + pthread_create(&t1, NULL, increment, NULL); + pthread_create(&t2, NULL, increment, NULL); + pthread_create(&t3, NULL, increment, NULL); + pthread_create(&t4, NULL, increment, NULL); + pthread_join(t1, NULL); + pthread_join(t2, NULL); + pthread_join(t3, NULL); + pthread_join(t4, NULL); + + sem_close(semaphore); + sem_unlink("/mysem"); + + printf("Final Counter Value (with semaphores): %d\n", counter); + + return 0; +} diff --git a/OS/C/theory/sync/wosema b/OS/C/theory/sync/wosema new file mode 100755 index 0000000..d00b728 Binary files /dev/null and b/OS/C/theory/sync/wosema differ diff --git a/OS/C/theory/sync/wsem b/OS/C/theory/sync/wsem new file mode 100755 index 0000000..2473cdb Binary files /dev/null and b/OS/C/theory/sync/wsem differ diff --git a/OS/C/theory/sync/wsem4 b/OS/C/theory/sync/wsem4 new file mode 100755 index 0000000..511dc27 Binary files /dev/null and b/OS/C/theory/sync/wsem4 differ