From 2313ae0e011eb30abc4dc87e10ba2dc23ae00121 Mon Sep 17 00:00:00 2001 From: hello Date: Sat, 8 Feb 2025 12:08:00 +0530 Subject: [PATCH] added OS theory code --- OS/C/theory/sync/withoutsemaphores.c | 21 +++++++++++++ OS/C/theory/sync/withsema.c | 32 ++++++++++++++++++++ OS/C/theory/sync/withsema4.c | 43 +++++++++++++++++++++++++++ OS/C/theory/sync/wosema | Bin 0 -> 33624 bytes OS/C/theory/sync/wsem | Bin 0 -> 33880 bytes OS/C/theory/sync/wsem4 | Bin 0 -> 33960 bytes 6 files changed, 96 insertions(+) create mode 100644 OS/C/theory/sync/withoutsemaphores.c create mode 100644 OS/C/theory/sync/withsema.c create mode 100644 OS/C/theory/sync/withsema4.c create mode 100755 OS/C/theory/sync/wosema create mode 100755 OS/C/theory/sync/wsem create mode 100755 OS/C/theory/sync/wsem4 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 0000000000000000000000000000000000000000..d00b7283e8206f34328b5960ef58702a47059cec GIT binary patch literal 33624 zcmeI5Yitx%6vyxEwzLa{7HD{hvWY@kV+v8DXaa0sQj|agh!qHzvfa{^?rz;~U6(b}jHbx@gt1(2E2wFoz#F$`>Rzip=5Vb{A)I?bV*8iEAZD*G#CMJIRpXAIv zkC`*){&x1`z4z4BAOFl#%1{s&bR0C1rPL1Pg{9PFXcbiJ>eBg@D=L>PmT0<|52uH2 z@T5*VZq#~NWzDkmwE4U*J*I3w8(~qjWQx|Yz{Z&EV92buYOXbz%l0ke%e;mpOT~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i((~q44RCwS_f@ zx({tXc%(M+>5a|(jxPrWw%RIdb4J>Zw!O6Ts}s9d?0V#bJI%lI?PTY|+}GMKP5tr0 z{KekIr#@R(_w}su^>4o~#=Uj!yL}VSw%mB&jW&;c+re+fzF2d+tmAOOsyF5MOXq^E zMGt>?>Q4Fp>HO?P$D{JOeYaMfC_MPyFTVAAW?bGgcT0tTy2E?^*++fL4OZeev$q&w literal 0 HcmV?d00001 diff --git a/OS/C/theory/sync/wsem4 b/OS/C/theory/sync/wsem4 new file mode 100755 index 0000000000000000000000000000000000000000..511dc2723b7a8b2e6552531196859ecd81d44871 GIT binary patch literal 33960 zcmeI5U2GIp6vyxEwsecsRzSX?D}rf_QX?gxQXINeXb?&YDUBE|Wjkfp?nm9-rqDJp z+7MffN!L`<2P0KUY@uo)B$Nb06O3OkSc0fLm=>Zb1Ru}`yCk*#&&+H)T|Y3I_+ZXS z&YW}Zx%ce7zq$K(?_9Y3Pz1yQoeAwv6XLLN!xLgYbQ4s{ZR|Ur=yt^Q!+%#n6^J=1u&`gmKB+cxnxa>Vp`XP zqzQe(ha+V)R=+no(Ue+mbe&l*VD+HbUe9IK14Bck^hRS5e`w!CJaxU*Pnz|Ltvtnc zvDc<{DeFTG{!q2_hibw`Jhfi8Rj<-2LUC#hD`gmZ51mjcH>}^WUV6&5Za3eCYHb2o zLsG00jiiJ%2P);huNfC(@GCcp%k025#WOn?b60Vco%m;e)C0!)AjFaajO z1pcQ4#?Onvu_i~)Sc`~vwd9VT^W^s9h~eyRkL=I9)Lx!1x*{$mvv%b4kp(`7SWP*c zf9!MJC*mU+A|Vtt(JVyANQRogImy0KMLmYPLwMM&GO1YU0`~HPPi2gOtbl(8;zIv+ZG9+t9lrPW>>Sj$s<-l%o@V%kI(T z6YaZ4ms}E3vSch@eb`dOz-O?Fa zI^(XwlG6Ef64iK4KIok83{=T6DjDr*li3^{j37BW)gw#&$(o&VbUJfPF`e5cn_*d( zVTEoQelh_jzyz286JP>NfC(@GCcp%k025#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