From 644926a36114b7c7c129af287f379e5e875fa1aa Mon Sep 17 00:00:00 2001 From: sherlock Date: Mon, 24 Mar 2025 03:41:40 +0530 Subject: [PATCH 1/8] feat: add SMTP server and mail sender scripts Implement an SMTP server using aiosmtpd and a mail sender script. The server handles incoming messages and prints their content, while the mail sender constructs and sends an email message. These changes enable local email testing for the CNP assignment, improving development efficiency. --- CNP/Wireshark/SMTP/mail-sender.py | 14 +++++++++++++ CNP/Wireshark/SMTP/smtp-server.py | 33 +++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 CNP/Wireshark/SMTP/mail-sender.py create mode 100644 CNP/Wireshark/SMTP/smtp-server.py diff --git a/CNP/Wireshark/SMTP/mail-sender.py b/CNP/Wireshark/SMTP/mail-sender.py new file mode 100644 index 0000000..186f05e --- /dev/null +++ b/CNP/Wireshark/SMTP/mail-sender.py @@ -0,0 +1,14 @@ +import smtplib +from email.message import EmailMessage + +# the email message +msg = EmailMessage() +msg['Subject'] = 'CNP Assignment Email' +msg['From'] = 'aadit@example.com' +msg['To'] = 'shreyas@example.com' # used example.com just to not have a DNS error. +msg.set_content('Hello World. This is a message that is to be captured for the CNP FISAC II.') + +#SMTP server running on localhost on port 25 +with smtplib.SMTP('127.0.0.1', 25) as server: + server.set_debuglevel(1) # debug output to see the SMTP conversation (for dev log) + server.send_message(msg) diff --git a/CNP/Wireshark/SMTP/smtp-server.py b/CNP/Wireshark/SMTP/smtp-server.py new file mode 100644 index 0000000..1868743 --- /dev/null +++ b/CNP/Wireshark/SMTP/smtp-server.py @@ -0,0 +1,33 @@ +import asyncio +import uvloop +from aiosmtpd.controller import Controller + +# handler that just prints the message content +class PrintHandler: + async def handle_DATA(self, server, session, envelope): + print("Message from:", envelope.mail_from) + print("Recipients:", envelope.rcpt_tos) + print("Message data:") + print(envelope.content.decode('utf8', errors='replace')) + print("End of message") + # Respond with a success code + return '250 Message accepted for delivery' + +# uvloop for better performance +asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) + +def run_server(): + # SMTP server controller, binding to localhost on port 25 + controller = Controller(PrintHandler(), hostname='127.0.0.1', port=25) + controller.start() + print("SMTP server running on port 25. Press Ctrl+C to stop.") + try: + # Run forever + asyncio.get_event_loop().run_forever() + except KeyboardInterrupt: + pass + finally: + controller.stop() + +if __name__ == '__main__': + run_server() From 0fc003326595b70c08e7d255338442579d9b46f4 Mon Sep 17 00:00:00 2001 From: sherlock Date: Thu, 3 Apr 2025 19:01:46 +0530 Subject: [PATCH 2/8] added optimised OS codes --- CNP/Wireshark/SMTP/mail-sender.py | 14 +++++ CNP/Wireshark/SMTP/smtp-server.py | 33 +++++++++++ OS/C/Week8/aq1 | Bin 0 -> 33680 bytes OS/C/Week8/aq1.c | 95 ++++++++++++++++++++++++++++++ 4 files changed, 142 insertions(+) create mode 100644 CNP/Wireshark/SMTP/mail-sender.py create mode 100644 CNP/Wireshark/SMTP/smtp-server.py create mode 100755 OS/C/Week8/aq1 diff --git a/CNP/Wireshark/SMTP/mail-sender.py b/CNP/Wireshark/SMTP/mail-sender.py new file mode 100644 index 0000000..186f05e --- /dev/null +++ b/CNP/Wireshark/SMTP/mail-sender.py @@ -0,0 +1,14 @@ +import smtplib +from email.message import EmailMessage + +# the email message +msg = EmailMessage() +msg['Subject'] = 'CNP Assignment Email' +msg['From'] = 'aadit@example.com' +msg['To'] = 'shreyas@example.com' # used example.com just to not have a DNS error. +msg.set_content('Hello World. This is a message that is to be captured for the CNP FISAC II.') + +#SMTP server running on localhost on port 25 +with smtplib.SMTP('127.0.0.1', 25) as server: + server.set_debuglevel(1) # debug output to see the SMTP conversation (for dev log) + server.send_message(msg) diff --git a/CNP/Wireshark/SMTP/smtp-server.py b/CNP/Wireshark/SMTP/smtp-server.py new file mode 100644 index 0000000..1868743 --- /dev/null +++ b/CNP/Wireshark/SMTP/smtp-server.py @@ -0,0 +1,33 @@ +import asyncio +import uvloop +from aiosmtpd.controller import Controller + +# handler that just prints the message content +class PrintHandler: + async def handle_DATA(self, server, session, envelope): + print("Message from:", envelope.mail_from) + print("Recipients:", envelope.rcpt_tos) + print("Message data:") + print(envelope.content.decode('utf8', errors='replace')) + print("End of message") + # Respond with a success code + return '250 Message accepted for delivery' + +# uvloop for better performance +asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) + +def run_server(): + # SMTP server controller, binding to localhost on port 25 + controller = Controller(PrintHandler(), hostname='127.0.0.1', port=25) + controller.start() + print("SMTP server running on port 25. Press Ctrl+C to stop.") + try: + # Run forever + asyncio.get_event_loop().run_forever() + except KeyboardInterrupt: + pass + finally: + controller.stop() + +if __name__ == '__main__': + run_server() diff --git a/OS/C/Week8/aq1 b/OS/C/Week8/aq1 new file mode 100755 index 0000000000000000000000000000000000000000..901915ed1adafa94b59a1b06309a15b0b0bbc2df GIT binary patch literal 33680 zcmeI5e{2-T6~||N81DQ78*qW}Bb-X9eEcwwRw6|<*qj|~lYp_EV5Pv;oX^&u=mtKLMdiAfGlY`UjLa*m**s@9g()h0fc<8`(%wFZ-v=|>UMcfQ=X{~@_b!^&gS-= zX8&Y)S!)e>-#4FA-Ja~Ssa;jO+xIrNH>l0+jUBR{$WJ%r)tN%5o-D(BBnw)HlgO%C zT2fo0dN*yXF;=74bOMwzC6x{|Ql_=aMOM|F9kyjiDbM02ijDCYD$`_Ktu>{FaX)*y zV&jIg(hAE*dXwxzozihK28ryE{v&Hbd|ilMvX53dg5qmtJ%T#fLTqLu-$LDq^5nOL zXhNBZT!m~;)R!{ZnkwzW_>2i2PDPs=nfzjTcUR}~*5)0{8+uwBP^UB(ne15FbnvzG zORGxGez5wJ>%aNM#V63_Mplr8$bkVl@?P?v^36$$(JJRBp9_(X`N{df&+UoyK8Z}v zXbwWG$2L;NZGk;CJzW8RTfw>l*wws4b`oDjLGp)t4L%;TPP{11N0$0u#u^w#%67gm z0Vco%m;e)C0!)AjFaajO1egF5U;<2l2`~XBzyz286JP>NfC(@GCcp%k025#WOn?b6 z0Vco%m;e)C0!)AjFaajO1en01C2*(XSE0N6mC#+U(8hx4QTz@3+fG;Us8cB()q{`6 z&Uqh?I7RPtJvc7{&Bw)xLT#l(oEYMJwF0A$rVnk4 zP`{94Ud(m-(EFYup?Pi|_-;6;L@Sis82)zr?cB8DQIWRx!w}jQD^)S-?@=;ttW|^- z_8I;3;M|z**bZ6Fu4Ki%V{u&}hc%}ZdKONg z2W`fj66X^R(p(@rI9qbEvky6@C2$WrHYYF-1*gHLiN;M&8dse(ZaVbMlJiN_HzR3W zP13j-7&FnhyrglpN#pXMZ)QyTTwS5omnKe}^Ujap7($-z-4+SKKb(rIW%!q;=ai?3 zRkGcuM4jXV-cu2$%hhN3G5N5R(^ROv1UYEeEIH0}F-?cu8)IX$WS>Bxwu^KHOgF$h-PA~b=aIiC2Y>7@`^EsL9Y`1 z4(fy8-hXf^xM%eZl3(<+MvS)Xfl9Nj5^YW;qa}>Kl?O8iJg$tsF#6{AEROq|*05>T={XlHKXRCR z*pV17E?9dMYY>9}Pz(>lf9&>1A8?$CEQTE+lrHSse5Lp-{3H|cIInzjU&;74Sg5T~ zQpdk|td(;G7x==k!xg?K)_zo|Jq|hXSZkY8Jl2jBYO~P?ht=|=?puW#Ae=zZH|kkM zTDBp>_&)N!@qNUKDbR9>}UC1g>+(1RZ{H5o{}*b_VlCv7HnAF z(-xufm@B()5c{cL$!QtHexlgCjAAnwQM|Td^V5jYN71iR$<92Ae#BDlN%Zp|R$HH^ zw*A=>-=8*aQOrZKeWysLuD zsZv_Dv7)Tho!{L~o#+UzJOezBZjkIHo$aMF?!_o6)A@8t#qv6Sq4UF8kdiAU$uqpR zNfC(@GCcp%k025#WOn?b60Vco%m;e)C0!)AjFaajO1egF5 zU;<2l2`~XBzyz286JP>NfC(@GCcp%kz#}K%hQumlyi-B`uXE#F0H`3@UJ@Yxm$=bt z{9kcVj5qHTTP?;r3KYCRK=}%CIdVJlA>^azqpGT}siiB>qBhib;y>C`Lsy{Q*Fw!| zBfTCWp=D=xeP@GEcXc+m2O3d!2fBpX<*RRRG!!bTm`-=}!UBi5<-?uY)};FP`F-61 zzuM%lZ}4{tS6e+)W#b(Q=s=0O=O}nH1QN}2CUOpL=p71Fr*|u$q+6Mm;cBn1O{!NX z)t^tQe=Dg@?^U38DS({7yDLod4bJv8Y4Ts&8!h*}W{sGSmHOQ)#ho`V-94PKV5Dr;j#r+Tx$dJy zZO^&)hUc$e6 + +// C program for Banker's Algorithm (Safety & Resource Request) +// Optimized for minimal code size (e.g., for writing on paper) + +int main() { + int p, r, i, j, k, pid, req_pid = -1; // p=procs, r=res; req_pid: -1=initial, >=0 processing req + printf("P R:"); scanf("%d%d", &p, &r); // Input num processes and resources + int av[r], max[p][r], al[p][r], nd[p][r], req[r]; // av=avail, al=alloc, nd=need + int w[r], fin[p], seq[p]; // w=work, fin=finish, seq=safe sequence + + // Input available, max, allocation matrices + printf("Av:"); for(j=0; j w[j]) { possible = 0; break; } + if(possible) { // If need <= work + for(k=0; k nd[pid][j]) { puts("Err:Req>Need"); goto end; } + // Check 2: Request <= Available + for(j=0; j av[j]) { puts("Wait:Req>Avail"); goto end; } + + // Tentatively allocate resources + for(j=0; j Date: Thu, 3 Apr 2025 19:02:05 +0530 Subject: [PATCH 3/8] added optimised OS codes --- OS/C/Week7/q2 | Bin 0 -> 34448 bytes OS/C/Week7/q2.c | 84 ++++++++++++++++++++++-------------------------- 2 files changed, 39 insertions(+), 45 deletions(-) create mode 100755 OS/C/Week7/q2 diff --git a/OS/C/Week7/q2 b/OS/C/Week7/q2 new file mode 100755 index 0000000000000000000000000000000000000000..7d6f5c42f5be08fcfe991c0a9c200e1f470808c0 GIT binary patch literal 34448 zcmeI5e{5S<701u>kHt=ZBn@TV3L{nvBg;w(8FZzjsoOMb3Z*S=(V|Uoo7hfc65FYN zC<(2EE3aax!1APMOlZ?OiHW4$MwuclmV%UlK%H1&*&iTas!9>yuG%yKlajI!zURJY z``K+*2_*jET;=$lbAOz7?&sc@|L%FezV_C?mJ=C-;(|0lraeR_DFBV=PDmF-sr}n` zbw1R2U>~=A4`{rPNvV5o~yp;S5`^Y*+iRyL83-@B~Duo!bv>R~`HI z9<+8NXdeR9x>VJSlZE%H9-`Edgku|S>Mx72x&*EHwneu=sVf?&E8}u>WUJzGbg}A; zi*4{v#A1m!>s+jLTDtztTK@Jpn(U*ZTl86U^p(&5p51%D(7AJW@$Al4#g(-tjq$=z z*pJiKr-`!6jZfbQI_|UWkrPDyXy!e;15$vk>l|u~TZXp%5NGkhVA1}Z9S*%8^BWy@ zQC$fo0O9kxF_TJe9E|pE437?mVRQKqgrDEqvhN?Sv~Qa|+hRsvj&`-RqAdVHNGRAj zSDn^1|C)zA&1iMT(}eZ0pUrJNzFhdZn<3n1ozq=-y^V4#i09yFDjkWn>}Ww;(Ozrr z-Kg0Ar4!cL@3C#>#pN!De*Q_KF)K*jE=C3d|h#1~NeW@o#=UuN0q+c=g&1%1`Qb_|N!nwg%r;^C`^ZHO!#qHB6x{ za11$S@10!iFianqg5v_%FHB$`d>CJFQ+{a~<%{eM%D(ELU=5-Y}Ne zTFm`7i{*}Xv%`)BSBh^Pi_HahsP!4;bx(i+jYj8r?Vr7YkH6hyJx(75#9s7)vw2+{ z?4!7c>vg==yRiZKB)@Ntz4saZtIWLLW{!)U;{?`=$2uo*Ud&my`VH=Ao~w5S7s%CU z>9r1hr9*FU=(jucwGRCw4t$=%bxf_QUXO%t-;o_|M4lUnt529w_`{`VQ`nUYU_jH%TOTG4_+lyD~Vbo{YKiBM! zYaY+FHK;zzcktP^t~b|bYit|)uiO20U2E5TZ<)vQeP6Cu*frljX4}5|oz zc)Aa@l}@+nPi42u!8H$kq!}&Rn4_DbZMit{3q;r0@%KmPZ{cM$;ud**x*?+3C->I^% zRoNaZn9}-?cc>yh&Xd8pF*q*<=fvQA7@P}(^I-6mm)=IZ>HR7<{_E*t9BrjT&-{JoA71y(4DIN8`GNDPw>SLg z;tOx&cHj1H-;bLfJDz*?`ENY;Vt3*fSNdi>znz;KaP8~%E*+mb{`Hf;|H-MYZ*BPD zt@<-NF5Vyi_+Ku6`SVRLJfBZ*Zv1)k!FJEy3qR=k{@KpM2i|_J>(${kr+&Kr_#2OZ z@7^yC{4>?~lKJ4K4fT5$-#d1$x2^3~{{P$lWv5@PAD{W=h1_@EUBB(Gb61 #include -#define NUM_READERS 3 -#define NUM_WRITERS 2 +#define NR 3 // NUM_READERS +#define NW 2 // NUM_WRITERS // shared data structure - simple integer -int shared_data = 0; +int sd = 0; // shared_data // sync variables -pthread_mutex_t mutex; // mutex for read_count protection -sem_t wrt; // semaphore for writer access control -int read_count = 0; // tracks active readers +pthread_mutex_t m; // mutex for rc protection +sem_t w; // semaphore for writer access control +int rc = 0; // read_count: tracks active readers -void* reader(void* arg) { +void* r(void* arg) { // reader int id = *((int*)arg); while (1) { // read delay sim sleep(1); // critical section entry - pthread_mutex_lock(&mutex); - read_count++; - if (read_count == 1) { + pthread_mutex_lock(&m); + rc++; + if (rc == 1) { // first reader blocks writers - sem_wait(&wrt); + sem_wait(&w); } - pthread_mutex_unlock(&mutex); + pthread_mutex_unlock(&m); // critical section - printf("Reader %d is reading shared_data = %d\n", id, shared_data); + printf("Reader %d is reading sd = %d\n", id, sd); sleep(2); // read time sim // critical section exit - pthread_mutex_lock(&mutex); - read_count--; - if (read_count == 0) { + pthread_mutex_lock(&m); + rc--; + if (rc == 0) { // last reader unblocks writers - sem_post(&wrt); + sem_post(&w); } - pthread_mutex_unlock(&mutex); + pthread_mutex_unlock(&m); } pthread_exit(NULL); } -void* writer(void* arg) { +void* w_func(void* arg) { // writer int id = *((int*)arg); while (1) { // write delay sim sleep(3); // critical section entry - sem_wait(&wrt); // exclusive access control + sem_wait(&w); // exclusive access control // critical section - shared_data += 1; - printf("Writer %d is writing: shared_data becomes %d\n", id, shared_data); + sd++; // shared_data modification + printf("Writer %d is writing: sd becomes %d\n", id, sd); sleep(2); // write time sim // critical section exit - sem_post(&wrt); + sem_post(&w); } pthread_exit(NULL); } int main(void) { - pthread_t readers[NUM_READERS], writers[NUM_WRITERS]; - int reader_ids[NUM_READERS], writer_ids[NUM_WRITERS]; + pthread_t rts[NR], wts[NW]; // reader/writer threads + int rids[NR], wids[NW]; // reader/writer ids int i; // sync init - pthread_mutex_init(&mutex, NULL); - sem_init(&wrt, 0, 1); // binary semaphore init + pthread_mutex_init(&m, NULL); + sem_init(&w, 0, 1); // binary semaphore init // reader thread creation - for (i = 0; i < NUM_READERS; i++) { - reader_ids[i] = i + 1; - if (pthread_create(&readers[i], NULL, reader, &reader_ids[i]) != 0) { - perror("Failed to create reader thread"); - exit(EXIT_FAILURE); - } + for (i = 0; i < NR; i++) { + rids[i] = i + 1; + pthread_create(&rts[i], NULL, r, &rids[i]); } // writer thread creation - for (i = 0; i < NUM_WRITERS; i++) { - writer_ids[i] = i + 1; - if (pthread_create(&writers[i], NULL, writer, &writer_ids[i]) != 0) { - perror("Failed to create writer thread"); - exit(EXIT_FAILURE); - } + for (i = 0; i < NW; i++) { + wids[i] = i + 1; + pthread_create(&wts[i], NULL, w_func, &wids[i]); } // thread joining - for (i = 0; i < NUM_READERS; i++) { - pthread_join(readers[i], NULL); + for (i = 0; i < NR; i++) { + pthread_join(rts[i], NULL); } - for (i = 0; i < NUM_WRITERS; i++) { - pthread_join(writers[i], NULL); + for (i = 0; i < NW; i++) { + pthread_join(wts[i], NULL); } // cleanup - pthread_mutex_destroy(&mutex); - sem_destroy(&wrt); + pthread_mutex_destroy(&m); + sem_destroy(&w); return 0; } From 86ff7c0f353e64c5b40f89ccdfd1361fc4017f7f Mon Sep 17 00:00:00 2001 From: sherlock Date: Fri, 4 Apr 2025 08:57:29 +0530 Subject: [PATCH 4/8] added optimised OS codes --- OS/C/Week10/aq1 | Bin 0 -> 33584 bytes OS/C/Week10/aq1.c | 50 +++++++++ OS/C/Week10/aq2 | Bin 0 -> 33552 bytes OS/C/Week10/aq2.c | 78 +++++++++++++ OS/C/Week10/q1.c | 198 +++++++++++++++++++++++++++++++++ OS/C/Week10/q2.c | 0 OS/C/Week9/aq1 | Bin 0 -> 50432 bytes OS/C/Week9/aq1.c | 125 +++++++++++++++++++++ OS/C/Week9/q1.c | 272 ++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 723 insertions(+) create mode 100755 OS/C/Week10/aq1 create mode 100644 OS/C/Week10/aq1.c create mode 100755 OS/C/Week10/aq2 create mode 100644 OS/C/Week10/aq2.c create mode 100644 OS/C/Week10/q1.c create mode 100644 OS/C/Week10/q2.c create mode 100755 OS/C/Week9/aq1 create mode 100644 OS/C/Week9/aq1.c create mode 100644 OS/C/Week9/q1.c diff --git a/OS/C/Week10/aq1 b/OS/C/Week10/aq1 new file mode 100755 index 0000000000000000000000000000000000000000..299cf2306608dc5e25a765f8c8632981b97fd079 GIT binary patch literal 33584 zcmeI5Yiv|S6vxl)w)7#Tg*FtZv>OPd5YvKStVrs@LQBJ=O{uYhcq#3c7W$ArkP70G zwF#gfx=CaF0>v2cZUsY#k*(3R%?2?%Vk(Im5)3g>w+2iwQBx4=|IEE@ZtV-8v8kjfx2G0x~z0;x0>%7Ya>gKftdv%siN!6o;}T$f-2JA^{33u)bb=0)_Gqe zC7Qe=Z8UVfskvoa(}*e3-@0c^f6J{AtlRy?S^mIK6(8+Km03<$kMv=6sfy3xZHTU~T2Z<}cNcBgXr6}4 zN&?iXMAQx#S@^7yA-cZ3!FCK2`pd#tRb1wLOQT6>SVyEx9G97A>k2olUA?MsI7hq@ zaY5&@h=W1lcvSuTTW$bZ6$Or=@5>i<&%&1pFjRY_32xu&OLi_ zZNlTQIUyQ^C0m`aqy};6$7nj)(?mvcox0^w-S&9%40zyCt2mv7=1cZPP z5CTF#2nYcoAOwVf5D)@FKnMr{As_^VfDjM@LO=)z0U;m+gn$qb0zyCt2mv7=1cZPP z5CTF#2nYcoAOwVf5D)@FKnVOz0z)CK|L)#NfxAA<-xJCVVtP9{^Zc7N>KJg4-CS%ziHB5#R-wU=s?s{F6X^FIwAJ&w#E&oLZ&FyPjMaa#P&@LBlMEMM>+ zaC8Ra;^W`oSUFBLzbwzc81skc#>LQ71HOJ98cI|Bm3jX8?BAUc>PRn-aA` zE1XmQXUydTb46@NN)^Yf?jD`Y(k8Pv!oA54pG{9$yw;iKk|=r_P+dg zoB2OHJ~Gce56E-Gga1C)U_94&mT+gdga1d_$FYzeyt_9)fd5#3uS5R|_hL^cN4W+m zD${IFvb7Uz?J2hQG_#%W+^~%1RnDuVl8xM;{KEVq%3n6GGJ%SUOVu6VNAbXMvDbXJ zm+$_%P_yv;bS^ISJNm=-hbO~Jvht+t(WAY|UhD@Y*~|X8Hm_GFd#yoUWtJZyAOwVf z5D)@FKnMr{As_^VfDjM@LO=)z0U;m+gn$qb0zyCt2mv7=1cZPP5CTF#2nYcoAOwVf z5D)@FKnMr{As_^VfDjM@LO=)z0U;m+gn$qb0zyCt2mv7=1cZPP5CTHrKO^9T=VC}T zguiuez6$^i6x&Mz)VIVxKCDsiM5T${eWaG>u#QA_50pZ$eA2tJXbxJQXd?9=+OAUg>G1 z_`33%dP>B*4={iW4^Ptg%j8%SG98kH2EQADb$(|8YHq4B2fucmzpHN7`8(`(oxi1S z*Uj)uKFkaxtdPgroTQHvJ}@Bszk=lUx08FZa=W=ae(~N3*s#2|kD_ooQEJ&2U@ud> zM0i!jfn&}n_33%b^q?xH*6P*DcP%F4xBRYm)|cp)_q%onoQW4M{r+xh^p)1y({JUp z9KV>6aO$;_XUZBr8>s4y{<^<^XH-#H%+S6M_r20~_0u=j9D3}%hxNyJeeW+S=svvV z`>^ZewZ`$bhG*o_l}Oldw0@04i&b|Je+$rySMMeH{X4* z{6ycE<%Mkz^Z!r#x3@a3ojTf=JN;bWoc!p5t--muKi(<28k*65`J?0`D^vaiMcXNU literal 0 HcmV?d00001 diff --git a/OS/C/Week10/aq1.c b/OS/C/Week10/aq1.c new file mode 100644 index 0000000..21da790 --- /dev/null +++ b/OS/C/Week10/aq1.c @@ -0,0 +1,50 @@ +#include +#include + +// Page Replacement: FIFO & Optimal (Minimal Code for Paper) +int main() { + int nf, np, i, j, k, pf, hit, idx, max_f, vic; // nf=frames, np=pages, pf=faults, idx=fifo_idx, max_f=max_future, vic=victim + int *rs, *f; // rs=ref_string, f=frames + + printf("F N:"); scanf("%d%d", &nf, &np); // Frames, NumPages + rs = malloc(np * sizeof(int)); + f = malloc(nf * sizeof(int)); + if(!rs || !f) return 1; // Basic alloc check + printf("RS:"); for(i=0; imax_f) {max_f=fut; vic=k;} // f[k] used later than current max? Update victim. + } + f[vic]=rs[i]; // Replace victim frame + } + } + } + printf("F:%d\n", pf); // Print Faults + + free(rs); free(f); // Free memory + return 0; +} diff --git a/OS/C/Week10/aq2 b/OS/C/Week10/aq2 new file mode 100755 index 0000000000000000000000000000000000000000..e47ee896210fc38efb492ab632d6d91069b5bc6a GIT binary patch literal 33552 zcmeI5ZETZO6vuDdZFI0VU@{zT6mdGhfPjgBV;0KBP-K&LAKeS-S7v7N~CNFg@b|4t!X8g{kjbg)vZ~VIwO&+xL}i{_Lt&O z=SQ^)ajy^&ijQ^~e`2X&M_|w%5}TROBy5Ud*)AcRD5pT(P_xijQgfW>{WGO6OuxcPO~1JKCFK`H>$w=VqaGmqDqIau8xI{*5dXu^^tN-cZ=zv2gW5#1&{( zgT$RE$p1h@&x_6}*~E*a0!nrSdH3rNfC(@GCcp%k z025#WOn?b60Vco%m;e)C0!)AjFaajO1egF5U;<2l2`~XBzyz286JP>NfC(@GCcp%k z025#WOn?b60VeQ2Auv8<8NRwbFL5<$i46|TP7V%LBnP7vNrcxgep?xPUx@vqHWAb2 zis5RD7}J)b%oSs*zq&Sd3jJC%o)njMe0dV}@5?a8um{MV4%?;opCG%~m|IgDI}Ur$ zHam$ch^3lMCKE21v|IAGjl3H#|HvZlqxO+v=UBoPPyTGnKlWg*n0gH3?fLn8DJF_X z#o?)q9fGf4$H&Vu{6#1HwHf|K%ba7!;Lk21)2e&tr`NW_w+7$8XZtY4WG~E&$=(}C zYSBvNOLJ9%U8U}CXiG9-TD!q&%){D=p~Fd!tH?VJ&0tQn8u)XnH!8yq|8@}2Y?!)6;I>+*b>3V;m`_4m2Wcr zyCZIK8&x3YlJ54>-Crk4Qo5f`;#9vAKXiY%0A5OzC)IAc)?5-H|G3#3A%9fTct}M3 z@(&YW0!)AjFaajO1egF5U;<2l2`~XBzyz286JP>NfC(@GCcp%k025#WOn?b60Vco% zm;e)C0!)AjFaajO1egF5U;<2l2`~XBzyz286JP>NfC(@GCcp%k025#WOn?b60VeRD z6L4S!H}nZ8Jqn<|1%L{~^hkjEmbl)ke^*>M@!Etl)uPX{-~j;(K7MYggf2v%Yz_MT zLUwrD+q+tXd@>m547UmyYVmfq(o~BS)2O6}2W;ZK9@Jz*476jgcO|hBL=lh@DApbb>>6*5W7r(Z9bCKH@{QStE`|Qc%7hn3Kt0sOk r|Npk%_2I)+v$h@Hq22XUuj9Gqedo`tbYHk*s=a*Afqi0c!_mJ1XHj7o literal 0 HcmV?d00001 diff --git a/OS/C/Week10/aq2.c b/OS/C/Week10/aq2.c new file mode 100644 index 0000000..a5dadf7 --- /dev/null +++ b/OS/C/Week10/aq2.c @@ -0,0 +1,78 @@ +#include +#include // For malloc, free + +// C program for LRU Page Replacement Simulation +// Optimized for minimal code size (e.g., for writing on paper) + +int main() { + int nf, np, i, j, pg, idx, lru, pf = 0, time = 0, min_t; + // nf=num frames, np=num pages, pf=page faults, time=logical clock + // idx=found index, lru=least recently used index, pg=current page + + // Input frame count (nf) and page reference string length (np) + printf("Frames Pages:"); scanf("%d%d", &nf, &np); + + // Dynamic Allocation + int *f = malloc(nf * sizeof(int)); // f: frames array + int *c = malloc(nf * sizeof(int)); // c: counter/lru time array + int *p = malloc(np * sizeof(int)); // p: page reference string array + + // Input page reference string + printf("Pages:"); + for(i=0; i +#include +#include // For INT_MAX in optimal + +// Function to check if a page is present in frames +int isPresent(int page, int *frames, int num_frames) { + for (int i = 0; i < num_frames; i++) { + if (frames[i] == page) { + return 1; // Present + } + } + return 0; // Not present +} + +// Function to print the current state of frames (for debugging/visualization) +void printFrames(int *frames, int num_frames) { + printf("Frames: "); + for (int i = 0; i < num_frames; i++) { + if (frames[i] == -1) { + printf("[ ] "); + } else { + printf("[%d] ", frames[i]); + } + } + printf("\n"); +} + +// FIFO Page Replacement Simulation +int simulateFIFO(int *ref_string, int num_refs, int num_frames) { + int *frames = (int *)malloc(num_frames * sizeof(int)); + if (frames == NULL) { + perror("Failed to allocate memory for frames"); + exit(EXIT_FAILURE); + } + for (int i = 0; i < num_frames; i++) { + frames[i] = -1; // Initialize frames as empty (-1 indicates empty) + } + + int page_faults = 0; + int frame_index = 0; // Points to the next frame to be replaced (FIFO queue head) + + printf("\n--- FIFO Simulation ---\n"); + for (int i = 0; i < num_refs; i++) { + int current_page = ref_string[i]; + printf("Ref: %d -> ", current_page); + + if (!isPresent(current_page, frames, num_frames)) { + page_faults++; + frames[frame_index] = current_page; + frame_index = (frame_index + 1) % num_frames; // Move to next frame in circular fashion + printf("Fault! "); + printFrames(frames, num_frames); + } else { + printf("Hit. "); + printFrames(frames, num_frames); + } + } + + free(frames); + return page_faults; +} + +// Function to find the optimal page to replace +int findOptimalVictim(int *frames, int num_frames, int *ref_string, int num_refs, int current_index) { + int victim_frame = -1; + int farthest_use = -1; // Index of the farthest future use + + for (int i = 0; i < num_frames; i++) { + int page_in_frame = frames[i]; + int next_use = INT_MAX; // Assume page is never used again initially + + // Look for the next occurrence of this page in the reference string + for (int j = current_index + 1; j < num_refs; j++) { + if (ref_string[j] == page_in_frame) { + next_use = j; + break; // Found the *next* use + } + } + + // If this page is never used again, it's the best victim + if (next_use == INT_MAX) { + return i; // Return the index of the frame holding this page + } + + // Otherwise, track the page whose next use is farthest away + if (next_use > farthest_use) { + farthest_use = next_use; + victim_frame = i; // This frame holds the current best candidate for victim + } + } + // Should always find a victim if frames are full, defaults to first if logic error/all used soon + return (victim_frame == -1) ? 0 : victim_frame; +} + + +// Optimal Page Replacement Simulation +int simulateOptimal(int *ref_string, int num_refs, int num_frames) { + int *frames = (int *)malloc(num_frames * sizeof(int)); + if (frames == NULL) { + perror("Failed to allocate memory for frames"); + exit(EXIT_FAILURE); + } + for (int i = 0; i < num_frames; i++) { + frames[i] = -1; // Initialize frames as empty + } + + int page_faults = 0; + int current_frame_count = 0; + + printf("\n--- Optimal Simulation ---\n"); + for (int i = 0; i < num_refs; i++) { + int current_page = ref_string[i]; + printf("Ref: %d -> ", current_page); + + if (!isPresent(current_page, frames, num_frames)) { + page_faults++; + printf("Fault! "); + + // Check if there are empty frames first + if (current_frame_count < num_frames) { + frames[current_frame_count] = current_page; + current_frame_count++; + } else { + // Frames are full, need to find the optimal victim + int victim_index = findOptimalVictim(frames, num_frames, ref_string, num_refs, i); + frames[victim_index] = current_page; // Replace victim + } + printFrames(frames, num_frames); + } else { + printf("Hit. "); + printFrames(frames, num_frames); + } + } + + free(frames); + return page_faults; +} + + +int main() { + int num_frames; + int num_refs; + int *ref_string; + + // Get number of frames + printf("Enter the number of page frames: "); + scanf("%d", &num_frames); + if (num_frames <= 0) { + printf("Number of frames must be positive.\n"); + return 1; + } + + // Get number of page references + printf("Enter the number of page references in the sequence: "); + scanf("%d", &num_refs); + if (num_refs <= 0) { + printf("Number of references must be positive.\n"); + return 1; + } + + // Allocate memory for reference string + ref_string = (int *)malloc(num_refs * sizeof(int)); + if (ref_string == NULL) { + perror("Failed to allocate memory for reference string"); + return 1; + } + + // Get the reference string + printf("Enter the page reference sequence (e.g., 7 0 1 2 0 ...):\n"); + for (int i = 0; i < num_refs; i++) { + if (scanf("%d", &ref_string[i]) != 1) { + printf("Invalid input for reference sequence.\n"); + free(ref_string); + return 1; + } + if (ref_string[i] < 0) { + printf("Page numbers cannot be negative.\n"); + free(ref_string); + return 1; + } + } + + // --- Run Simulations --- + int fifo_faults = simulateFIFO(ref_string, num_refs, num_frames); + int optimal_faults = simulateOptimal(ref_string, num_refs, num_frames); + + // --- Print Results --- + printf("\n--- Results ---\n"); + printf("Reference String Length: %d\n", num_refs); + printf("Number of Frames: %d\n", num_frames); + printf("FIFO Page Faults: %d\n", fifo_faults); + printf("Optimal Page Faults: %d\n", optimal_faults); + + // --- Cleanup --- + free(ref_string); + + return 0; +} diff --git a/OS/C/Week10/q2.c b/OS/C/Week10/q2.c new file mode 100644 index 0000000..e69de29 diff --git a/OS/C/Week9/aq1 b/OS/C/Week9/aq1 new file mode 100755 index 0000000000000000000000000000000000000000..a1dd58143a1290059b15597040b22b1ddbb556f3 GIT binary patch literal 50432 zcmeI44{TM{9mmgo@0GsS7TO{k2udGrCGXXNfcUR1^+5}?uu-JV!u)sl+V^T-+t<>* zlA*|aOGg4$7v9qNXH6`diM}FkY%%k7$wsl8bIXzi;&d+AbUNHaz-ZzUiq`J?JLjJE z_F5HV%(CQnlJm~}ojIq@;iL9vhUl}8Hr@44%MQ5$q$bp zn`X`W#`R`({k8_>jgS=tC}nb#Zd6hxUlk#n=3QMjH+-zV0Q$-lQsXU2%)tZsRVT75 zi>&O@wJ4JgyCBOHQiZlluh}#s;oh)nqu-Cyr{jtA>#@_Lrk~)?u6VqwQ`b24c`=@t zEYKZLl1)0v*wzBd>HYNex@}uG*3>EbLX0}r30Wo!66vw)S_?Ad2O4(>#kH1v2r~JX z*0c!u#uGvmqulkJ5PMMOBX38xCyHIlc7NF0>A4o;^PH8C=OO!%Y5X<26Ft{-#G0;& z+}#m@Olb)++41ti6;D0%n~ISqA8Wg#yJ-Kvu`T5|wWdNCTqxHk{h524rwr|KeJCj* zj*_IOS8qgFtLD8C&(x;;xEaeZrfq>e4RH1WlNz`j1?dlf50ybnCSH`T zM5g+8uNfC(@G zCcp%k025#WOn?b60Vco%m;e)C0!)AjFaajO1paRcOuCJe6MNmm6JsKEc&sGzXx-F@ z=6Z8-a-m!obb8Tucx)(xv8RIj@&wGIeIpSO= zji#@aGx-M%BrPeYk4`%niT1#?0pZ=qhZ4x%19`^i6ELv)|Z z9a`*z4Z+hDDKARz-z!oN7-BwhkGJ^XkIuM7>S}N=%Ms&P!??6JQ;|x*UTrIJNyGM2 zShH-;R;1d|KCySh@Fe0|`Gn%!Gduww={Wa_>VmJG>u&YTT(?|@dpc(9 zYsq3QwT`%*wLh2)BhDmS?5DI)b5N|2J@~d}fwo7k4fef)wNXq$UY%VywAkyL#ja|H zUG{mfrvlbmNDn?1Dc1V{@jgy|d&(Mrq;KhkcI<~>tUdD->|AE#=gW1Vo%V@bv!&aG z_;8Elj<-LYeE7^|(9w721=>SF=sSY`danj@)IOj+uIHt_N^?>SwIeRJ82SBmh*RoA zu|$lfVn@zhFzAcLGFi0E8W$HTQs)gZfNxoFp3qpY)S>&+xd1y<423FFucI$p_u>qj zI**_)4gFK+5oE+?F~!1Z@_V1{JesHCiT2%D>^r^3_1SWyPwn%gI9ueq0q={cy^S{g zUhpBd^?v=xxnHy{^&f$I1ble}=Zsv79IMWx5}X~DO~pa^9&1)Mo4RMSDebU{&TQgpzblYE@?J#q;GRKxd@F*3cAE~P?PbfJ36wtUYx2M5 zPWK%79^JVv^(c6g-816Qc4wqJgY5gjy3`NQm(FE#UY`e6=dI9&?cm*=BHq-Ps0|`W;nMmBQ!11Nx3DHN301SOfJy!EjTmC4qSxfo!UEB&HI9N-YdUWrM}{rSJuN%R8P82Ll5bd^&M3y z=#z0kx@8?0;#>29V>k!2Uj+P;i#mOeJdBvK-;41*=CL9d{te(Nc}YIcu|4P6p6A(~ z=d0&$MC0YR34dv%yfjjp5X*${-;pOaZSvP|^=~hYtf(rDgi0f_W>YxUL3M%BNH9-q z_(&Jj%G@Glc!0!)AjFaajO1egF5U;<2l2`~XBzyz28 z6JP>NfC(@GCcp%k025#WOn?b60Vco%m;e)C0!)AjFaajO1egF5U;<2l2`~XBzyz28 z6JP>NfC(@GCcp%k025#WOn?b60VeQ85U{@Q{~}bgPA0$vm;e)C0!)AjFaajO1egF5 zU;<2l2`~XBzyz286JP>NfC(@GCcp%k025#WOn?b60Vco%m;e)C0!)AjFaajO1egF5 zU;<2l2`~XBzyz286JP>NfC(@GCcp%k025#WOyDyR@T2c`SH`)0H&Na3Rk_UW&XDS+|=lJ<+HzWOvswg|H)YE>kf!<{Xpz!cvi zmCz&H;>Awk5r>j4qeaa1h-A`*x^oHqq}%W_8o|$dd(h<(vah*pT zkiDO2MW2DYgvc}Ft!8vjw0U=L)NGB0Bhen=YZCLD!@I6{0Aq9Ig206CSf)q8pszW zd||>Drt}2@22M5O%Bv>aV!|yZx#dzL3YJW#i~lGXh4;ujz4isDgFB56gbV*0hD*Bk zcC_!bMqydcz2kSuogZnA;HFcHJM= z3>>;5@qYYQbx+@S^cOq3oUsI($#z%$&Ya16pSky*{eSy)f9=7qJ^5+vImc>$ z`0B+fSHYq!2OkWyUe&mD+0FIl>eB4)f}c0Hoa#&%d3>o*4Y*vH8jK&0TMtzUg?`8_Nq;UB9d?_FMOy yr_X%%^XmV!@vHmiJv*^J|B0G6+y7#msz1B?-A9+4>$!EqOOJo)_Ws9T_52q+M-0UP literal 0 HcmV?d00001 diff --git a/OS/C/Week9/aq1.c b/OS/C/Week9/aq1.c new file mode 100644 index 0000000..e7235d9 --- /dev/null +++ b/OS/C/Week9/aq1.c @@ -0,0 +1,125 @@ +#include +#include + +// Dynamic Storage Allocation: First Fit & Best Fit +// Optimized for minimal code size (e.g., for writing on paper) + +// Block structure: s=start, z=size, p=process_id (0 if free) +struct B {int s, z, p;} *b; +int nb = 0, cap = 4, mem_sz; // num_blocks, capacity, total_memory_size + +// insb: insert block at index idx, maintain order by start address 's' +void insb(int idx, int s, int z, int p) { + if (nb >= cap) { + cap *= 2; + b = realloc(b, cap * sizeof(struct B)); + // Note: Real-world code checks realloc failure. Skipped for brevity. + } + for (int k = nb; k > idx; k--) b[k] = b[k-1]; // Shift right + b[idx] = (struct B){s, z, p}; + nb++; +} + +// rmb: remove block at index idx +void rmb(int idx) { + for (int k = idx; k < nb - 1; k++) b[k] = b[k+1]; // Shift left + nb--; +} + +// pb: print current blocks state +void pb() { + printf("Mem:["); + // Print each block: start:size! (allocated) or start:size (free) + for(int i=0; i= sz) { f = i; break; } + } + if (f != -1) { // Block found + if (b[f].z > sz) { // Need to split block + // Insert new free block for the remainder after the allocated part + insb(f + 1, b[f].s + sz, b[f].z - sz, 0); + b[f].z = sz; // Adjust size of the now-allocated block + } + b[f].p = pid; // Mark block as allocated to pid + printf("FF OK P%d->%d@%d\n", pid, sz, b[f].s); + } else printf("FF Fail P%d(%d)\n", pid, sz); // Allocation failed + pb(); // Show memory state +} + +// bf: Best Fit allocation +void bf(int pid, int sz) { + int bi = -1, min_z = mem_sz + 1; // best_index, min_suitable_size + for (int i = 0; i < nb; i++) { // Find smallest free block large enough + if (!b[i].p && b[i].z >= sz && b[i].z < min_z) { + min_z = b[i].z; // Update best size found + bi = i; // Update best index found + } + } + if (bi != -1) { // Best fit block found + if (b[bi].z > sz) { // Need to split block + // Insert new free block for the remainder + insb(bi + 1, b[bi].s + sz, b[bi].z - sz, 0); + b[bi].z = sz; // Adjust size of the allocated block + } + b[bi].p = pid; // Mark block allocated + printf("BF OK P%d->%d@%d\n", pid, sz, b[bi].s); + } else printf("BF Fail P%d(%d)\n", pid, sz); // Allocation failed + pb(); // Show memory state +} + +// de: Deallocate block associated with pid +void de(int pid) { + int f = -1; // found index + for (int i = 0; i < nb; i++) if (b[i].p == pid) { f = i; break; } // Find block by pid + + if (f != -1) { // Block found + printf("De OK P%d@%d(%d)\n", pid, b[f].s, b[f].z); + b[f].p = 0; // Mark block as free + + // Try merging with the *next* block if it exists and is free + if (f + 1 < nb && !b[f+1].p) { + b[f].z += b[f+1].z; // Absorb next block's size + rmb(f + 1); // Remove the next block entry + } + // Try merging with the *previous* block if it exists and is free + if (f > 0 && !b[f-1].p) { + b[f-1].z += b[f].z; // Add current block's size to previous + rmb(f); // Remove the current block entry (now merged) + // f = f-1; // If index 'f' were needed after merge, adjust it + } + pb(); // Show memory state + } else printf("De Fail P%d\n", pid); // Deallocation failed (pid not found) +} + +// Main driver loop +int main() { + printf("MemSz:"); scanf("%d", &mem_sz); // Get total memory size + b = malloc(cap * sizeof(struct B)); // Allocate initial block array + if (!b) return 1; // Handle malloc failure + b[0] = (struct B){0, mem_sz, 0}; // Create the first block (all memory, free) + nb = 1; + pb(); // Show initial state + + int choice, pid, sz; + printf("1:FF 2:BF 3:Dealloc 0:Exit\n"); + // Loop until user enters 0 + while(scanf("%d", &choice) == 1 && choice) { + if (choice == 1 || choice == 2) { // Allocate request + printf("PID Sz:"); scanf("%d%d", &pid, &sz); + if (choice == 1) ff(pid, sz); else bf(pid, sz); + } else if (choice == 3) { // Deallocate request + printf("PID:"); scanf("%d", &pid); + de(pid); + } else printf("?\n"); // Invalid choice + printf("1:FF 2:BF 3:Dealloc 0:Exit\n"); // Prompt again + } + + free(b); // Free the block array memory + return 0; +} diff --git a/OS/C/Week9/q1.c b/OS/C/Week9/q1.c new file mode 100644 index 0000000..cb48a66 --- /dev/null +++ b/OS/C/Week9/q1.c @@ -0,0 +1,272 @@ +#include +#include +#include // For INT_MAX + +// Structure for a memory block +typedef struct Block { + int id; // Block ID (optional, can use address for uniqueness) + int size; // Size of the block + int allocated; // 0 if free, 1 if allocated + int process_id; // ID of process allocated to this block (-1 if free) + struct Block *next; // Pointer to the next block in the list + struct Block *prev; // Pointer to the previous block in the list (for potential merging) +} Block; + +// Global head of the memory block linked list +Block *memory_head = NULL; + +// Function to create a new block node +Block* create_block(int id, int size, int allocated, int process_id) { + Block *new_block = (Block*)malloc(sizeof(Block)); + if (!new_block) { + perror("Failed to allocate memory for block"); + exit(EXIT_FAILURE); + } + new_block->id = id; + new_block->size = size; + new_block->allocated = allocated; + new_block->process_id = process_id; + new_block->next = NULL; + new_block->prev = NULL; + return new_block; +} + +// Function to initialize the memory list with one large free block +void initialize_memory(int total_size) { + if (memory_head != NULL) { + // Simple cleanup for re-initialization (more robust needed for general use) + Block *current = memory_head; + Block *next_node; + while(current != NULL) { + next_node = current->next; + free(current); + current = next_node; + } + memory_head = NULL; // Reset head + } + memory_head = create_block(0, total_size, 0, -1); // ID 0, size, free, no process +} + +// Function to display the current state of memory blocks +void display_memory() { + Block *current = memory_head; + printf("Memory Blocks:\n"); + printf("----------------------------------------------------\n"); + printf("| ID | Size | Status | Process ID |\n"); + printf("----------------------------------------------------\n"); + while (current != NULL) { + printf("| %-2d | %-9d | %-9s | %-10d |\n", + current->id, + current->size, + current->allocated ? "Allocated" : "Free", + current->allocated ? current->process_id : -1); + current = current->next; + } + printf("----------------------------------------------------\n\n"); +} + +// Function to allocate memory using First Fit strategy +int allocate_first_fit(int process_id, int required_size) { + Block *current = memory_head; + Block *best_block = NULL; + + // Find the first free block that is large enough + while (current != NULL) { + if (!current->allocated && current->size >= required_size) { + best_block = current; + break; // First fit found + } + current = current->next; + } + + // If a suitable block is found + if (best_block != NULL) { + // Check if splitting is necessary (and worthwhile, e.g., remaining > 0) + if (best_block->size > required_size) { + // Create a new block for the remaining free space + int remaining_size = best_block->size - required_size; + // For simplicity, assigning next available ID - needs better management in real system + int new_block_id = best_block->id + 1; // simplistic ID assignment + Block *new_free_block = create_block(new_block_id, remaining_size, 0, -1); + + // Update the allocated block + best_block->size = required_size; + best_block->allocated = 1; + best_block->process_id = process_id; + + // Insert the new free block into the list + new_free_block->next = best_block->next; + new_free_block->prev = best_block; + if (best_block->next != NULL) { + best_block->next->prev = new_free_block; + } + best_block->next = new_free_block; + + // Renumber subsequent block IDs (basic approach) + Block* temp = new_free_block->next; + int current_id = new_block_id + 1; + while (temp != NULL) { + temp->id = current_id++; + temp = temp->next; + } + + } else { // Exact fit or minimal leftover space (allocate the whole block) + best_block->allocated = 1; + best_block->process_id = process_id; + } + printf("Process %d allocated %d units using First Fit in Block %d.\n", process_id, required_size, best_block->id); + return 1; // Allocation successful + } else { + printf("Process %d (size %d) could not be allocated using First Fit.\n", process_id, required_size); + return 0; // Allocation failed + } +} + + +// Function to allocate memory using Best Fit strategy +int allocate_best_fit(int process_id, int required_size) { + Block *current = memory_head; + Block *best_block = NULL; + int min_waste = INT_MAX; + + // Find the smallest free block that is large enough + while (current != NULL) { + if (!current->allocated && current->size >= required_size) { + int waste = current->size - required_size; + if (waste < min_waste) { + min_waste = waste; + best_block = current; + } + } + current = current->next; + } + + // If a suitable block is found + if (best_block != NULL) { + // Check if splitting is necessary (and worthwhile, e.g., remaining > 0) + if (best_block->size > required_size) { + // Create a new block for the remaining free space + int remaining_size = best_block->size - required_size; + int new_block_id = best_block->id + 1; // simplistic ID assignment + Block *new_free_block = create_block(new_block_id, remaining_size, 0, -1); + + // Update the allocated block + best_block->size = required_size; + best_block->allocated = 1; + best_block->process_id = process_id; + + // Insert the new free block into the list + new_free_block->next = best_block->next; + new_free_block->prev = best_block; + if (best_block->next != NULL) { + best_block->next->prev = new_free_block; + } + best_block->next = new_free_block; + + // Renumber subsequent block IDs (basic approach) + Block* temp = new_free_block->next; + int current_id = new_block_id + 1; + while (temp != NULL) { + temp->id = current_id++; + temp = temp->next; + } + + } else { // Exact fit (allocate the whole block) + best_block->allocated = 1; + best_block->process_id = process_id; + } + printf("Process %d allocated %d units using Best Fit in Block %d.\n", process_id, required_size, best_block->id); + return 1; // Allocation successful + } else { + printf("Process %d (size %d) could not be allocated using Best Fit.\n", process_id, required_size); + return 0; // Allocation failed + } +} + +// Function to free all allocated memory for the linked list +void cleanup_memory() { + Block *current = memory_head; + Block *next_node; + while (current != NULL) { + next_node = current->next; + free(current); + current = next_node; + } + memory_head = NULL; +} + + +int main() { + int total_memory; + int num_processes; + int *process_sizes = NULL; // Dynamically allocated array for process sizes + int i; + + // --- Input --- + printf("Enter the total size of memory: "); + scanf("%d", &total_memory); + if (total_memory <= 0) { + printf("Invalid memory size.\n"); + return 1; + } + + printf("Enter the number of processes: "); + scanf("%d", &num_processes); + if (num_processes <= 0) { + printf("Invalid number of processes.\n"); + return 1; + } + + // Dynamically allocate array for process sizes + process_sizes = (int*)malloc(num_processes * sizeof(int)); + if (!process_sizes) { + perror("Failed to allocate memory for process sizes"); + return 1; + } + + printf("Enter the size required for each process:\n"); + for (i = 0; i < num_processes; i++) { + printf("Process %d size: ", i + 1); + scanf("%d", &process_sizes[i]); + if (process_sizes[i] <= 0) { + printf("Invalid process size. Please enter a positive value.\n"); + free(process_sizes); + return 1; + } + } + printf("\n"); + + + // --- First Fit Simulation --- + printf("--- First Fit Allocation ---\n"); + initialize_memory(total_memory); + printf("Initial Memory State:\n"); + display_memory(); + + for (i = 0; i < num_processes; i++) { + allocate_first_fit(i + 1, process_sizes[i]); // Process IDs starting from 1 + display_memory(); // Show state after each allocation attempt + } + printf("Final Memory State after First Fit:\n"); + display_memory(); + + + // --- Best Fit Simulation --- + printf("\n--- Best Fit Allocation ---\n"); + initialize_memory(total_memory); // Re-initialize memory for a fresh start + printf("Initial Memory State:\n"); + display_memory(); + + for (i = 0; i < num_processes; i++) { + allocate_best_fit(i + 1, process_sizes[i]); // Process IDs starting from 1 + display_memory(); // Show state after each allocation attempt + } + printf("Final Memory State after Best Fit:\n"); + display_memory(); + + // --- Cleanup --- + free(process_sizes); // Free the dynamically allocated process sizes array + cleanup_memory(); // Free the memory blocks linked list + + return 0; +} From 44e766514ea2bb8f22eb7c3ff0ac8ac7eb0b0139 Mon Sep 17 00:00:00 2001 From: sherlock Date: Fri, 11 Apr 2025 08:30:01 +0530 Subject: [PATCH 5/8] modified and updated --- OS/C/Week10/q2.c | 169 ++++++++++++++++++++++++++++++++++++++++++++ OS/C/Week10/qq1.c | 176 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 345 insertions(+) create mode 100644 OS/C/Week10/qq1.c diff --git a/OS/C/Week10/q2.c b/OS/C/Week10/q2.c index e69de29..45d1079 100644 --- a/OS/C/Week10/q2.c +++ b/OS/C/Week10/q2.c @@ -0,0 +1,169 @@ +#include +#include +#include // For INT_MAX + +// Structure to represent a frame and its last used time +typedef struct { + int page_number; + int last_used_time; +} Frame; + +// Function to find if a page exists in frames and return its index +// Also updates the last_used_time if found +int find_page(Frame frames[], int n_frames, int page, int current_time) { + for (int i = 0; i < n_frames; i++) { + if (frames[i].page_number == page) { + frames[i].last_used_time = current_time; // Update time on hit + return i; // Page found (Hit) + } + } + return -1; // Page not found (Fault) +} + +// Function to find the index of the Least Recently Used (LRU) page +int find_lru_index(Frame frames[], int n_frames) { + int min_time = INT_MAX; + int lru_index = 0; + for (int i = 0; i < n_frames; i++) { + // Frame must contain a valid page (not -1) + if (frames[i].page_number != -1 && frames[i].last_used_time < min_time) { + min_time = frames[i].last_used_time; + lru_index = i; + } + } + return lru_index; +} + +// Function to print the current state of frames +void print_frames(Frame frames[], int n_frames) { + printf("["); + for (int i = 0; i < n_frames; i++) { + if (frames[i].page_number == -1) { + printf(" - "); + } else { + printf(" %d ", frames[i].page_number); + } + } + printf("]\n"); +} + +int main() { + int n_frames, n_pages; + int *pages = NULL; // Reference string + Frame *frames = NULL; // Frames in memory + + int page_faults = 0; + int page_hits = 0; + int current_time = 0; // Counter to track usage time + int frame_idx = 0; // Index for filling empty frames initially + + // 1. Get Inputs + printf("Enter the number of frames: "); + if (scanf("%d", &n_frames) != 1 || n_frames <= 0) { + fprintf(stderr, "Error: Invalid number of frames.\n"); + return 1; + } + + printf("Enter the number of pages in the reference string: "); + if (scanf("%d", &n_pages) != 1 || n_pages <= 0) { + fprintf(stderr, "Error: Invalid number of pages.\n"); + return 1; + } + + // 2. Allocate Memory + pages = (int *)malloc(n_pages * sizeof(int)); + if (pages == NULL) { + perror("Failed to allocate memory for pages"); + return 1; + } + + frames = (Frame *)malloc(n_frames * sizeof(Frame)); + if (frames == NULL) { + perror("Failed to allocate memory for frames"); + free(pages); // Clean up already allocated memory + return 1; + } + + printf("Enter the page reference string (space-separated %d integers):\n", n_pages); + for (int i = 0; i < n_pages; i++) { + if (scanf("%d", &pages[i]) != 1) { + fprintf(stderr, "Error reading page reference string.\n"); + free(pages); + free(frames); + return 1; + } + } + + // 3. Initialize Frames + for (int i = 0; i < n_frames; i++) { + frames[i].page_number = -1; // -1 indicates empty frame + frames[i].last_used_time = -1; // Initialize time + } + + printf("\n--- LRU Simulation Start ---\n"); + printf("Frames: %d | Reference String Length: %d\n\n", n_frames, n_pages); + + // 4. Process Page References + for (int i = 0; i < n_pages; i++) { + current_time++; // Increment time step for each reference + int current_page = pages[i]; + printf("Ref: %d -> ", current_page); + + int found_index = find_page(frames, n_frames, current_page, current_time); + + if (found_index != -1) { + // Page Hit + page_hits++; + printf("Hit "); + } else { + // Page Fault + page_faults++; + printf("Fault "); + + // Find a place for the new page + int replace_index = -1; + + // Check for an empty frame first + for(int k=0; k < n_frames; k++){ + if(frames[k].page_number == -1){ + replace_index = k; + break; + } + } + + if (replace_index != -1) { + // Use the empty frame + frames[replace_index].page_number = current_page; + frames[replace_index].last_used_time = current_time; + printf("(loaded into empty frame %d) ", replace_index); + } else { + // No empty frames, find LRU page to replace + replace_index = find_lru_index(frames, n_frames); + printf("(replaced P%d in frame %d) ", frames[replace_index].page_number, replace_index); + frames[replace_index].page_number = current_page; + frames[replace_index].last_used_time = current_time; + } + } + print_frames(frames, n_frames); // Show frame status after each step + } + + // 5. Output Results + printf("\n--- LRU Simulation End ---\n"); + printf("Total Page References: %d\n", n_pages); + printf("Total Page Hits: %d\n", page_hits); + printf("Total Page Faults: %d\n", page_faults); + if (n_pages > 0) { + printf("Hit Rate: %.2f%%\n", (double)page_hits / n_pages * 100.0); + printf("Fault Rate: %.2f%%\n", (double)page_faults / n_pages * 100.0); + } else { + printf("Hit Rate: N/A\n"); + printf("Fault Rate: N/A\n"); + } + + + // 6. Free Memory + free(pages); + free(frames); + + return 0; +} diff --git a/OS/C/Week10/qq1.c b/OS/C/Week10/qq1.c new file mode 100644 index 0000000..72d05c3 --- /dev/null +++ b/OS/C/Week10/qq1.c @@ -0,0 +1,176 @@ +#include +#include +#include + +// Function to check if a page exists in frames +bool isPagePresent(int* frames, int num_frames, int page) { + for (int i = 0; i < num_frames; i++) { + if (frames[i] == page) { + return true; + } + } + return false; +} + +// FIFO Page Replacement Algorithm +void fifo(int* reference_string, int num_pages, int num_frames) { + int* frames = (int*)malloc(num_frames * sizeof(int)); + int page_faults = 0; + int frame_index = 0; + + // Initialize frames with -1 (indicating empty) + for (int i = 0; i < num_frames; i++) { + frames[i] = -1; + } + + printf("\nFIFO Page Replacement Algorithm:\n"); + printf("Reference String: "); + for (int i = 0; i < num_pages; i++) { + printf("%d ", reference_string[i]); + } + printf("\n\n"); + + for (int i = 0; i < num_pages; i++) { + printf("Page %d: ", reference_string[i]); + + // Check if page already exists in frames + if (!isPagePresent(frames, num_frames, reference_string[i])) { + // Replace page at current frame_index + frames[frame_index] = reference_string[i]; + frame_index = (frame_index + 1) % num_frames; + page_faults++; + + printf("Page Fault! Frames: "); + } else { + printf("No Page Fault. Frames: "); + } + + // Print current state of frames + for (int j = 0; j < num_frames; j++) { + if (frames[j] == -1) { + printf("[ ] "); + } else { + printf("[%d] ", frames[j]); + } + } + printf("\n"); + } + + printf("\nTotal Page Faults (FIFO): %d\n", page_faults); + free(frames); +} + +// Function to find index of page that will be used farthest in future +int findOptimalPage(int* reference_string, int* frames, int num_frames, int num_pages, int current_position) { + int farthest = -1; + int index = -1; + + for (int i = 0; i < num_frames; i++) { + int j; + for (j = current_position + 1; j < num_pages; j++) { + if (frames[i] == reference_string[j]) { + if (j > farthest) { + farthest = j; + index = i; + } + break; + } + } + + // If page is never used in future + if (j == num_pages) { + return i; + } + } + + // If all pages will be used in future, return the one used farthest + return (index == -1) ? 0 : index; +} + +// Optimal Page Replacement Algorithm +void optimal(int* reference_string, int num_pages, int num_frames) { + int* frames = (int*)malloc(num_frames * sizeof(int)); + int page_faults = 0; + + // Initialize frames with -1 (indicating empty) + for (int i = 0; i < num_frames; i++) { + frames[i] = -1; + } + + printf("\nOptimal Page Replacement Algorithm:\n"); + printf("Reference String: "); + for (int i = 0; i < num_pages; i++) { + printf("%d ", reference_string[i]); + } + printf("\n\n"); + + for (int i = 0; i < num_pages; i++) { + printf("Page %d: ", reference_string[i]); + + // Check if page already exists in frames + if (!isPagePresent(frames, num_frames, reference_string[i])) { + int free_frame = -1; + + // Check if there's an empty frame + for (int j = 0; j < num_frames; j++) { + if (frames[j] == -1) { + free_frame = j; + break; + } + } + + if (free_frame != -1) { + // If empty frame exists, use it + frames[free_frame] = reference_string[i]; + } else { + // Find optimal page to replace + int replace_index = findOptimalPage(reference_string, frames, num_frames, num_pages, i); + frames[replace_index] = reference_string[i]; + } + + page_faults++; + printf("Page Fault! Frames: "); + } else { + printf("No Page Fault. Frames: "); + } + + // Print current state of frames + for (int j = 0; j < num_frames; j++) { + if (frames[j] == -1) { + printf("[ ] "); + } else { + printf("[%d] ", frames[j]); + } + } + printf("\n"); + } + + printf("\nTotal Page Faults (Optimal): %d\n", page_faults); + free(frames); +} + +int main() { + int num_pages, num_frames; + + printf("Enter number of frames: "); + scanf("%d", &num_frames); + + printf("Enter number of pages in reference string: "); + scanf("%d", &num_pages); + + int* reference_string = (int*)malloc(num_pages * sizeof(int)); + + printf("Enter the reference string (page numbers):\n"); + for (int i = 0; i < num_pages; i++) { + scanf("%d", &reference_string[i]); + } + + // Run FIFO algorithm + fifo(reference_string, num_pages, num_frames); + + // Run Optimal algorithm + optimal(reference_string, num_pages, num_frames); + + free(reference_string); + return 0; +} From 6ff189473261af82a49054db58a07ca14febba72 Mon Sep 17 00:00:00 2001 From: sherlock Date: Fri, 11 Apr 2025 08:30:13 +0530 Subject: [PATCH 6/8] modified and updated --- OS/C/Week11/q1.c | 201 ++++++++++++++++++++++++++++++++++++ OS/C/Week11/qq1.c | 234 ++++++++++++++++++++++++++++++++++++++++++ OS/C/Week11/test.c | 0 OS/C/Week12/rtos.c | 153 +++++++++++++++++++++++++++ OS/C/Week9/q1-smol.c | 81 +++++++++++++++ OS/C/practice/test.sh | 12 +-- 6 files changed, 674 insertions(+), 7 deletions(-) create mode 100644 OS/C/Week11/q1.c create mode 100644 OS/C/Week11/qq1.c create mode 100644 OS/C/Week11/test.c create mode 100644 OS/C/Week12/rtos.c create mode 100644 OS/C/Week9/q1-smol.c diff --git a/OS/C/Week11/q1.c b/OS/C/Week11/q1.c new file mode 100644 index 0000000..5a7cbfb --- /dev/null +++ b/OS/C/Week11/q1.c @@ -0,0 +1,201 @@ +#include +#include +#include +#define MAX 100 + +// Function for SSTF (Shortest Seek Time First) +void sstf(int arr[], int n, int head) { + int visited[MAX] = {0}, total_seek = 0, current = head; + printf("\nSSTF Sequence:\n%d ", current); + for (int count = 0; count < n; count++) { + int index = -1, minDist = 1e9; + for (int i = 0; i < n; i++) { + if (!visited[i]) { + int dist = abs(arr[i] - current); + if (dist < minDist) { + minDist = dist; + index = i; + } + } + } + visited[index] = 1; + total_seek += minDist; + current = arr[index]; + printf("-> %d ", current); + } + printf("\nTotal Seek Time: %d\n", total_seek); +} + +// Helper function: Bubble sort in ascending order +void sortAsc(int arr[], int n) { + for (int i = 0; i < n - 1; i++) + for (int j = i + 1; j < n; j++) + if (arr[i] > arr[j]) { + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } +} + +// Helper function: Bubble sort in descending order +void sortDesc(int arr[], int + for (int i = 0; i < n - 1; i++) + for (int j = i + 1; j < n; j++) + if (arr[i] < arr[j]) { + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } +} + +// Function for SCAN (Elevator Algorithm) +// Assumes movement is towards the left first, then reverses. +void scan(int arr[], int n, int head, int disk_size) { + int left[MAX], right[MAX], l = 0, r = 0; + for (int i = 0; i < n; i++) { + if (arr[i] < head) + left[l++] = arr[i]; + else + right[r++] = arr[i]; + } + sortDesc(left, l); + sortAsc(right, r); + int total_seek = 0, current = head; + printf("\nSCAN Sequence:\n%d ", current); + // Service left side (moving toward 0) + for (int i = 0; i < l; i++) { + total_seek += abs(current - left[i]); + current = left[i]; + printf("-> %d ", current); + } + // If not already at 0, move to 0 + if (current != 0) { + total_seek += current; + current = 0; + printf("-> %d ", current); + } + // Then service right side (moving right) + for (int i = 0; i < r; i++) { + total_seek += abs(right[i] - current); + current = right[i]; + printf("-> %d ", current); + } + printf("\nTotal Seek Time: %d\n", total_seek); +} + +// Function for C-SCAN (Circular SCAN) +// Assumes movement to the right; after reaching the end, the head jumps to 0. +void cscan(int arr[], int n, int head, int disk_size) { + int left[MAX], right[MAX], l = 0, r = 0; + for (int i = 0; i < n; i++) { + if (arr[i] < head) + left[l++] = arr[i]; + else + right[r++] = arr[i]; + } + sortAsc(left, l); + sortAsc(right, r); + int total_seek = 0, current = head; + printf("\nC-SCAN Sequence:\n%d ", current); + // Service requests to the right + for (int i = 0; i < r; i++) { + total_seek += abs(right[i] - current); + current = right[i]; + printf("-> %d ", current); + } + // Go to the end if not reached + if (current != disk_size - 1) { + total_seek += abs(disk_size - 1 - current); + current = disk_size - 1; + printf("-> %d ", current); + } + // Jump to beginning (simulate wrap-around) + total_seek += (disk_size - 1); + current = 0; + printf("-> %d ", current); + // Service the left side + for (int i = 0; i < l; i++) { + total_seek += abs(left[i] - current); + current = left[i]; + printf("-> %d ", current); + } + printf("\nTotal Seek Time: %d\n", total_seek); +} + +// Function for C-LOOK +// Assumes movement to the right; after the furthest request, it jumps to the smallest request. +void clook(int arr[], int n, int head) { + int left[MAX], right[MAX], l = 0, r = 0; + for (int i = 0; i < n; i++) { + if (arr[i] < head) + left[l++] = arr[i]; + else + right[r++] = arr[i]; + } + sortAsc(left, l); + sortAsc(right, r); + int total_seek = 0, current = head; + printf("\nC-LOOK Sequence:\n%d ", current); + // Service the right side + for (int i = 0; i < r; i++) { + total_seek += abs(right[i] - current); + current = right[i]; + printf("-> %d ", current); + } + // Jump to the leftmost request if any exist on the left + if (l > 0) { + total_seek += abs(current - left[0]); + current = left[0]; + printf("-> %d ", current); + for (int i = 1; i < l; i++) { + total_seek += abs(left[i] - current); + current = left[i]; + printf("-> %d ", current); + } + } + printf("\nTotal Seek Time: %d\n", total_seek); +} + +int main() { + int choice, n, head, disk_size; + int arr[MAX]; + printf("Menu:\n"); + printf("1. SSTF\n"); + printf("2. SCAN\n"); + printf("3. C-SCAN\n"); + printf("4. C-LOOK\n"); + printf("Enter your choice: "); + scanf("%d", &choice); + + printf("Enter number of requests: "); + scanf("%d", &n); + printf("Enter the request queue (space separated): "); + for (int i = 0; i < n; i++) { + scanf("%d", &arr[i]); + } + printf("Enter initial head position: "); + scanf("%d", &head); + + if (ce == 2 || choice == 3) { // SCAN and C-SCAN require the disk size + printf("Enter disk size: "); + scanf("%d", &disk_size); + } + + switch (choice) { + case 1: + sstf(arr, n, head); + break; + case 2: + scan(arr, n, head, disk_size); + break; + case 3: + cscan(arr, n, head, disk_size); + break; + case 4: + clook(arr, n, head); + break; + default: + printf("Invalid choice!\n"); + } + return 0; +} diff --git a/OS/C/Week11/qq1.c b/OS/C/Week11/qq1.c new file mode 100644 index 0000000..53d3daa --- /dev/null +++ b/OS/C/Week11/qq1.c @@ -0,0 +1,234 @@ +#include +#include +#include + +void sstf(int requests[], int n, int head) { + int total_seek = 0; + int completed = 0; + int visited[100] = {0}; + int current = head; + + printf("\nSSTF Disk Scheduling\n"); + printf("Seek Sequence: %d", head); + + while (completed < n) { + int min_distance = INT_MAX; + int min_index = -1; + + for (int i = 0; i < n; i++) { + if (!visited[i]) { + int distance = abs(requests[i] - current); + if (distance < min_distance) { + min_distance = distance; + min_index = i; + } + } + } + + visited[min_index] = 1; + current = requests[min_index]; + total_seek += min_distance; + completed++; + printf(" -> %d", current); + } + + printf("\nTotal Seek Time: %d\n", total_seek); +} + +void scan(int requests[], int n, int head, int disk_size) { + int total_seek = 0; + int direction = 1; // 1 for moving right, 0 for moving left + int current = head; + + printf("\nSCAN Disk Scheduling\n"); + printf("Seek Sequence: %d", head); + + // Sort requests + for (int i = 0; i < n; i++) { + for (int j = 0; j < n - i - 1; j++) { + if (requests[j] > requests[j + 1]) { + int temp = requests[j]; + requests[j] = requests[j + 1]; + requests[j + 1] = temp; + } + } + } + + // Find position of head in sorted array + int index; + for (index = 0; index < n; index++) { + if (requests[index] >= head) + break; + } + + // Move right + for (int i = index; i < n; i++) { + current = requests[i]; + printf(" -> %d", current); + total_seek += abs(current - head); + head = current; + } + + // Move to the end of disk + printf(" -> %d", disk_size - 1); + total_seek += abs(disk_size - 1 - head); + head = disk_size - 1; + + // Move left + for (int i = index - 1; i >= 0; i--) { + current = requests[i]; + printf(" -> %d", current); + total_seek += abs(current - head); + head = current; + } + + printf("\nTotal Seek Time: %d\n", total_seek); +} + +void cscan(int requests[], int n, int head, int disk_size) { + int total_seek = 0; + int current = head; + + printf("\nC-SCAN Disk Scheduling\n"); + printf("Seek Sequence: %d", head); + + // Sort requests + for (int i = 0; i < n; i++) { + for (int j = 0; j < n - i - 1; j++) { + if (requests[j] > requests[j + 1]) { + int temp = requests[j]; + requests[j] = requests[j + 1]; + requests[j + 1] = temp; + } + } + } + + // Find position of head in sorted array + int index; + for (index = 0; index < n; index++) { + if (requests[index] >= head) + break; + } + + // Move right + for (int i = index; i < n; i++) { + current = requests[i]; + printf(" -> %d", current); + total_seek += abs(current - head); + head = current; + } + + // Move to the end of disk + printf(" -> %d", disk_size - 1); + total_seek += abs(disk_size - 1 - head); + + // Move to the beginning + printf(" -> 0"); + total_seek += disk_size - 1; + head = 0; + + // Move right again + for (int i = 0; i < index; i++) { + current = requests[i]; + printf(" -> %d", current); + total_seek += abs(current - head); + head = current; + } + + printf("\nTotal Seek Time: %d\n", total_seek); +} + +void clook(int requests[], int n, int head) { + int total_seek = 0; + int current = head; + + printf("\nC-LOOK Disk Scheduling\n"); + printf("Seek Sequence: %d", head); + + // Sort requests + for (int i = 0; i < n; i++) { + for (int j = 0; j < n - i - 1; j++) { + if (requests[j] > requests[j + 1]) { + int temp = requests[j]; + requests[j] = requests[j + 1]; + requests[j + 1] = temp; + } + } + } + + // Find position of head in sorted array + int index; + for (index = 0; index < n; index++) { + if (requests[index] >= head) + break; + } + + // Move right + for (int i = index; i < n; i++) { + current = requests[i]; + printf(" -> %d", current); + total_seek += abs(current - head); + head = current; + } + + // Move to first request + for (int i = 0; i < index; i++) { + current = requests[i]; + printf(" -> %d", current); + total_seek += abs(current - head); + head = current; + } + + printf("\nTotal Seek Time: %d\n", total_seek); +} + +int main() { + int requests[100], n, head, disk_size, choice; + + printf("Enter the number of disk requests: "); + scanf("%d", &n); + + printf("Enter the disk requests: "); + for (int i = 0; i < n; i++) { + scanf("%d", &requests[i]); + } + + printf("Enter the initial head position: "); + scanf("%d", &head); + + printf("Enter the disk size (0 to size-1): "); + scanf("%d", &disk_size); + + do { + printf("\n\nDisk Scheduling Algorithms\n"); + printf("1. SSTF (Shortest Seek Time First)\n"); + printf("2. SCAN\n"); + printf("3. C-SCAN\n"); + printf("4. C-LOOK\n"); + printf("5. Exit\n"); + printf("Enter your choice: "); + scanf("%d", &choice); + + switch (choice) { + case 1: + sstf(requests, n, head); + break; + case 2: + scan(requests, n, head, disk_size); + break; + case 3: + cscan(requests, n, head, disk_size); + break; + case 4: + clook(requests, n, head); + break; + case 5: + printf("Exiting program...\n"); + break; + default: + printf("Invalid choice!\n"); + } + } while (choice != 5); + + return 0; +} diff --git a/OS/C/Week11/test.c b/OS/C/Week11/test.c new file mode 100644 index 0000000..e69de29 diff --git a/OS/C/Week12/rtos.c b/OS/C/Week12/rtos.c new file mode 100644 index 0000000..13afc5a --- /dev/null +++ b/OS/C/Week12/rtos.c @@ -0,0 +1,153 @@ +#include + +// Define Task structure (simplified for memorization) +typedef struct { + int id; // Task ID + int period; // Period (also deadline for simplicity) + int execution_time; // Worst-case execution time (WCET) + // --- Simulation State --- + int remaining_execution; // Remaining execution time for current instance + int absolute_deadline; // Absolute deadline for current instance + int time_to_arrival; // Time until the next instance arrives/is released +} Task; + +// --- Global Variables --- +// Define the tasks for the simulation (Example Set) +// Format: {id, Period, ExecutionTime, 0, 0, 0} <-- Initial state values +Task tasks[] = { + {1, 5, 2, 0, 0, 0}, // Task 1: Period=5, Exec Time=2 + {2, 8, 3, 0, 0, 0} // Task 2: Period=8, Exec Time=3 + // Add more tasks here if needed +}; +// Calculate number of tasks automatically +int num_tasks = sizeof(tasks) / sizeof(Task); +// Set simulation duration (e.g., Hyperperiod or a fixed time) +// LCM(5, 8) = 40 +int simulation_time = 40; + +// --- Rate Monotonic (RM) Simulation --- +void simulate_rm() { + printf("--- Rate Monotonic Scheduling ---\n"); + // Reset task states for the simulation run + for (int i = 0; i < num_tasks; i++) { + tasks[i].remaining_execution = 0; + tasks[i].absolute_deadline = 0; + tasks[i].time_to_arrival = 0; // All tasks start at time 0 + } + + // Main simulation loop + for (int time = 0; time < simulation_time; time++) { + // 1. Check for task arrivals (release time) + for (int i = 0; i < num_tasks; i++) { + if (tasks[i].time_to_arrival == 0) { + // Check if the previous instance of this task missed its deadline + if (tasks[i].remaining_execution > 0) { + printf("!!! Time %d: Task %d MISSED DEADLINE !!!\n", time, tasks[i].id); + // Simple handling: Continue with the new instance, old one is lost + } + // Release new instance of the task + tasks[i].remaining_execution = tasks[i].execution_time; + tasks[i].absolute_deadline = time + tasks[i].period; // Deadline = Period + tasks[i].time_to_arrival = tasks[i].period; // Set timer for the *next* arrival + } + tasks[i].time_to_arrival--; // Decrement time until the next arrival for all tasks + } + + // 2. Select highest priority task to run (RM: Shortest Period has highest priority) + int task_to_run = -1; // -1 indicates CPU Idle + int highest_priority = 10000; // Initialize with a low priority (large period) + + for (int i = 0; i < num_tasks; i++) { + // Check if task is ready (has arrived and needs execution) + if (tasks[i].remaining_execution > 0) { + // RM priority check: Lower period value means higher priority + if (tasks[i].period < highest_priority) { + highest_priority = tasks[i].period; + task_to_run = i; // Select this task + } + } + } + + // 3. Execute the selected task (or remain idle) + if (task_to_run != -1) { + // Task selected to run + printf("Time %d: Task %d running\n", time, tasks[task_to_run].id); + tasks[task_to_run].remaining_execution--; // Execute for one time unit + + // Optional: Check if task just finished + // if (tasks[task_to_run].remaining_execution == 0) { + // printf("Time %d: Task %d finished\n", time + 1, tasks[task_to_run].id); + // } + } else { + // No task ready to run + printf("Time %d: CPU Idle\n", time); + } + } + printf("--- RM Simulation Complete ---\n"); +} + +// --- Earliest Deadline First (EDF) Simulation --- +void simulate_edf() { + printf("\n--- Earliest Deadline First Scheduling ---\n"); + // Reset task states + for (int i = 0; i < num_tasks; i++) { + tasks[i].remaining_execution = 0; + tasks[i].absolute_deadline = 0; + tasks[i].time_to_arrival = 0; + } + + // Main simulation loop + for (int time = 0; time < simulation_time; time++) { + // 1. Check for task arrivals (same as RM) + for (int i = 0; i < num_tasks; i++) { + if (tasks[i].time_to_arrival == 0) { + if (tasks[i].remaining_execution > 0) { + printf("!!! Time %d: Task %d MISSED DEADLINE !!!\n", time, tasks[i].id); + } + tasks[i].remaining_execution = tasks[i].execution_time; + tasks[i].absolute_deadline = time + tasks[i].period; + tasks[i].time_to_arrival = tasks[i].period; + } + tasks[i].time_to_arrival--; + } + + // 2. Select highest priority task to run (EDF: Earliest Absolute Deadline has highest priority) + int task_to_run = -1; + int earliest_deadline = 10000; // Initialize with a late deadline + + for (int i = 0; i < num_tasks; i++) { + // Check if task is ready + if (tasks[i].remaining_execution > 0) { + // EDF priority check: Lower deadline value means higher priority (earlier deadline) + if (tasks[i].absolute_deadline < earliest_deadline) { + earliest_deadline = tasks[i].absolute_deadline; + task_to_run = i; // Select this task + } + } + } + + // 3. Execute the selected task (same as RM) + if (task_to_run != -1) { + printf("Time %d: Task %d running\n", time, tasks[task_to_run].id); + tasks[task_to_run].remaining_execution--; + // Optional: Check finish + // if (tasks[task_to_run].remaining_execution == 0) { + // printf("Time %d: Task %d finished\n", time + 1, tasks[task_to_run].id); + // } + } else { + printf("Time %d: CPU Idle\n", time); + } + } + printf("--- EDF Simulation Complete ---\n"); +} + +// --- Main Function --- +int main() { + // Run Rate Monotonic simulation + simulate_rm(); + + // Run Earliest Deadline First simulation + simulate_edf(); + + return 0; // Indicate successful execution +} diff --git a/OS/C/Week9/q1-smol.c b/OS/C/Week9/q1-smol.c new file mode 100644 index 0000000..a3aac36 --- /dev/null +++ b/OS/C/Week9/q1-smol.c @@ -0,0 +1,81 @@ +#include +#include + +#define BLOCKS 4 +#define REQUESTS 5 + +// Memory configuration +int memory[BLOCKS] = {100, 50, 25, 10}; +int allocated[BLOCKS] = {0, 0, 0, 0}; + +// Helper: Reset allocation state +void resetAllocation() { + for(int i = 0; i < BLOCKS; i++) { + allocated[i] = 0; + } +} + +// Print memory status +void printMemory() { + printf("\nMemory Status:\n"); + for(int i = 0; i < BLOCKS; i++) { + printf("[Size: %d, %s] -> ", memory[i], + allocated[i] ? "Allocated" : "Free"); + } + printf("NULL\n\n"); +} + +// First Fit allocation +void firstFit(int size) { + for(int i = 0; i < BLOCKS; i++) { + if (!allocated[i] && memory[i] >= size) { + allocated[i] = 1; + printf("Allocated %d bytes using First Fit\n", size); + return; + } + } + printf("First Fit: No suitable block found for %d bytes\n", size); +} + +// Best Fit allocation +void bestFit(int size) { + int best = -1; + int bestSize = 999999; + + for(int i = 0; i < BLOCKS; i++) { + if(!allocated[i] && memory[i] >= size && memory[i] < bestSize) { + bestSize = memory[i]; + best = i; + } + } + + if(best != -1) { + allocated[best] = 1; + printf("Allocated %d bytes using Best Fit\n", size); + } else { + printf("Best Fit: No suitable block found for %d bytes\n", size); + } +} + +// Main function: run allocation sequence +int main() { + int requests[REQUESTS] = {15, 35, 60, 10, 5}; + + printf("=== FIRST FIT ===\n"); + printMemory(); + for(int i = 0; i < REQUESTS; i++) { + firstFit(requests[i]); + printMemory(); + } + + resetAllocation(); + + printf("=== BEST FIT ===\n"); + printMemory(); + for(int i = 0; i < REQUESTS; i++) { + bestFit(requests[i]); + printMemory(); + } + + return 0; +} diff --git a/OS/C/practice/test.sh b/OS/C/practice/test.sh index 57fa256..e94be5a 100755 --- a/OS/C/practice/test.sh +++ b/OS/C/practice/test.sh @@ -1,8 +1,6 @@ -read -p "Enter a number: " num -if [[ $num -gt 10 ]]; then - echo "Number is greater than 10" -elif [[ $num -eq 10 ]]; then - echo "Number is exactly 10" -else - echo "Number is less than 10" +if [[ $# -lt 3 ]]; then + echo "Usage: $0 " + exit 1 fi + +if From d46358f2dadf3578695a12f461eef52938638bf7 Mon Sep 17 00:00:00 2001 From: sherlock Date: Fri, 11 Apr 2025 15:22:22 +0530 Subject: [PATCH 7/8] Added Endsem Questions --- OS/endsem/bashq.sh | 48 ++++++++++++++++++ OS/endsem/cq.c | 121 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 169 insertions(+) create mode 100644 OS/endsem/bashq.sh create mode 100644 OS/endsem/cq.c diff --git a/OS/endsem/bashq.sh b/OS/endsem/bashq.sh new file mode 100644 index 0000000..2d81ab7 --- /dev/null +++ b/OS/endsem/bashq.sh @@ -0,0 +1,48 @@ +#!/bin/bash + +# Ask for the directory +echo "Enter directory path:" +read dir + +while true; do + echo "" + echo "Menu:" + echo "1. Convert .txt files to .py in $dir" + echo "2. List ownership properties (ls -l) of all files in $dir" + echo "3. Count .sh files and subdirectories in $dir" + echo "4. Exit" + echo -n "Choose an option: " + read option + + case $option in + 1) + for file in "$dir"/*.txt; do + if [ -f "$file" ]; then + new="${file%.txt}.py" + mv "$file" "$new" + echo "Renamed: $file -> $new" + fi + done + ;; + 2) + ls -l "$dir" + ;; + 3) + # Count .sh files in the given directory (errors hidden if none found) + sh_count=$(ls -1 "$dir"/*.sh 2>/dev/null | wc -l) + # Count subdirectories (using a trailing slash pattern) + dir_count=$(ls -d "$dir"/*/ 2>/dev/null | wc -w) + echo ".sh file count: $sh_count" + echo "Subdirectory count: $dir_count" + ;; + 4) + echo "Exiting..." + break + ;; + *) + echo "Invalid option. Please try again." + ;; + esac +done + +echo "Bye!" diff --git a/OS/endsem/cq.c b/OS/endsem/cq.c new file mode 100644 index 0000000..0aaf784 --- /dev/null +++ b/OS/endsem/cq.c @@ -0,0 +1,121 @@ +#include +#include +#include +#define MAX_PROC 10 +#define MAX_REF 100 +#define MAX_FRAMES 10 + +typedef struct { + int id; // Process ID + int frames; // Number of frames for this process + int n; // Number of page references + int ref[MAX_REF]; // Array of page references + int faults; // Page faults counter +} Process; + +Process procs[MAX_PROC]; +int proc_count = 0; + +void *simulateOptimal(void *arg) { + Process *p = (Process *)arg; + int frameArr[MAX_FRAMES]; + int i, j; + + // Initialize frames as empty (-1) + for (i = 0; i < p->frames; i++) { + frameArr[i] = -1; + } + p->faults = 0; + + // Process each page reference + for (i = 0; i < p->n; i++) { + int page = p->ref[i]; + int found = 0; + // Check if page is already in a frame + for (j = 0; j < p->frames; j++) { + if (frameArr[j] == page) { + found = 1; + break; + } + } + if (found) + continue; + // Page fault occurs + p->faults++; + + // Look for an empty frame (-1) + int empty = -1; + for (j = 0; j < p->frames; j++) { + if (frameArr[j] == -1) { + empty = j; + break; + } + } + if (empty != -1) { + frameArr[empty] = page; + continue; + } + + // No empty frame; choose a victim using Optimal algorithm: + int replace = 0, farthest = -1; + for (j = 0; j < p->frames; j++) { + int k, nextUse = INT_MAX; + for (k = i + 1; k < p->n; k++) { + if (frameArr[j] == p->ref[k]) { + nextUse = k; + break; + } + } + if (nextUse > farthest) { + farthest = nextUse; + replace = j; + } + } + frameArr[replace] = page; + } + + printf("Process %d: Faults = %d\n", p->id, p->faults); + return NULL; +} + +int main() { + int i, j; + pthread_t threads[MAX_PROC]; + + // Input the number of processes + printf("Enter number of processes (max %d): ", MAX_PROC); + scanf("%d", &proc_count); + if (proc_count > MAX_PROC) proc_count = MAX_PROC; + + // Input process details + for (i = 0; i < proc_count; i++) { + procs[i].id = i + 1; + printf("\nProcess %d:\n", procs[i].id); + printf("Enter number of frames (max %d): ", MAX_FRAMES); + scanf("%d", &procs[i].frames); + if (procs[i].frames > MAX_FRAMES) procs[i].frames = MAX_FRAMES; + printf("Enter number of page references (max %d): ", MAX_REF); + scanf("%d", &procs[i].n); + if (procs[i].n > MAX_REF) procs[i].n = MAX_REF; + printf("Enter %d page references (space separated): ", procs[i].n); + for (j = 0; j < procs[i].n; j++) { + scanf("%d", &procs[i].ref[j]); + } + } + + // Create a thread for each process simulation + for (i = 0; i < proc_count; i++) { + pthread_create(&threads[i], NULL, simulateOptimal, &procs[i]); + } + + int totalFaults = 0; + // Wait for all threads to complete + for (i = 0; i < proc_count; i++) { + pthread_join(threads[i], NULL); + totalFaults += procs[i].faults; + } + + // Calculate and display average faults + printf("\nAverage Page Faults: %.2f\n", (float)totalFaults / proc_count); + return 0; +} From d309978375c888e99fa5b520e1c3a75fda3119cb Mon Sep 17 00:00:00 2001 From: sherlock Date: Fri, 11 Apr 2025 15:26:05 +0530 Subject: [PATCH 8/8] Removed useless lines coz why not --- OS/endsem/bashq.sh | 2 -- 1 file changed, 2 deletions(-) diff --git a/OS/endsem/bashq.sh b/OS/endsem/bashq.sh index 2d81ab7..68f293d 100644 --- a/OS/endsem/bashq.sh +++ b/OS/endsem/bashq.sh @@ -28,9 +28,7 @@ while true; do ls -l "$dir" ;; 3) - # Count .sh files in the given directory (errors hidden if none found) sh_count=$(ls -1 "$dir"/*.sh 2>/dev/null | wc -l) - # Count subdirectories (using a trailing slash pattern) dir_count=$(ls -d "$dir"/*/ 2>/dev/null | wc -w) echo ".sh file count: $sh_count" echo "Subdirectory count: $dir_count"