From 18b502014a6dc7e21862068d0f65f553c0eb856c Mon Sep 17 00:00:00 2001 From: Samantha Gatt Date: Wed, 13 Feb 2019 14:17:10 -0500 Subject: [PATCH 01/10] Completes ex1.c --- ex1/ex1 | Bin 0 -> 8520 bytes ex1/ex1.c | 24 +++++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100755 ex1/ex1 diff --git a/ex1/ex1 b/ex1/ex1 new file mode 100755 index 0000000000000000000000000000000000000000..d55c5cc518cc9d079e1d3aa56e262ad38cec5414 GIT binary patch literal 8520 zcmeHN&ubi29Dmy%8ft7d6{JOruYnbnlC;`G5rsrLy2YZciBJmi*v(G5LnpJ#%&bii zVIdT{lt=_Gp8N}x9s~~_OtIMBgy_M8hf<ZkH}8wT%ysYv6rVZ`7)ViFop)m^{ohREgdHVxjSsb@^y`!Y1Y`6v#(;G!wW z;~l^a<9a_canVZRs z;Qn!5>3z&w-d8+xeQc!yUq#D{)rPN{arlpT{l^3j`%@wR*;pyR@-V!jncqPJz&P8V zK6fep&U5H#7;zK8{UH|xr_m75m$6iEELJLr_e_x{X6D~(TExZ69)b<5yYZHg>WmfpR zC!rkU8us4a#4-vybHRLOm!sHSoP2E(^YW@Ff;az+ z_Y=6^aNE~$e(!z&n-pG|f``JdV6L_9Ul(0pCvL(3`6xP&?>P4`bPlvnP->&saCo$YBy_{TM!S&4Q;lnU9v%S*euxoX6 zh^=3HyUFs{BIZ-pD509 zvg6Y{>`M;ARTbyMIJ`a=iZu}y$ETIMA=HWT3WX^isdd2Blhgi~>;xF6gI(Ps8 literal 0 HcmV?d00001 diff --git a/ex1/ex1.c b/ex1/ex1.c index c4b111641..05241d0df 100644 --- a/ex1/ex1.c +++ b/ex1/ex1.c @@ -1,6 +1,8 @@ // Write a program that calls `fork()`. Before calling `fork()`, have the main process access a variable // (e.g., x) and set its value to something (e.g., 100). What value is the variable in the child process? + // The value is the same in the child process // What happens to the variable when both the child and parent change the value of x? + // Each retain their own copies and only edit their respective x #include #include @@ -8,7 +10,27 @@ int main(void) { - // Your code here + int x = 100; + + int rv = fork(); + + if (rv < 0) + { + printf("Fork failed\n"); + exit(1); + } + if (rv == 0) + { + printf("x is %d in the child process\n", x); + x = 300; + printf("x was changed to %d in the child process\n", x); + } + if (rv > 0) + { + printf("x is %d in the parent process\n", x); + x = 500; + printf("x was changed to %d in the parent process\n", x); + } return 0; } From 55cfd6355aec5c9bfabb50120d52dd17e9c22e65 Mon Sep 17 00:00:00 2001 From: Samantha Gatt Date: Wed, 13 Feb 2019 15:13:42 -0500 Subject: [PATCH 02/10] Completes ex2.c --- ex1/ex1.c | 8 +++--- ex2/ex2 | Bin 0 -> 8744 bytes ex2/ex2.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++-- ex2/text.txt | 11 +++++++++ 4 files changed, 80 insertions(+), 6 deletions(-) create mode 100755 ex2/ex2 diff --git a/ex1/ex1.c b/ex1/ex1.c index 05241d0df..fe7998159 100644 --- a/ex1/ex1.c +++ b/ex1/ex1.c @@ -12,20 +12,20 @@ int main(void) { int x = 100; - int rv = fork(); + int rc = fork(); - if (rv < 0) + if (rc < 0) { printf("Fork failed\n"); exit(1); } - if (rv == 0) + if (rc == 0) { printf("x is %d in the child process\n", x); x = 300; printf("x was changed to %d in the child process\n", x); } - if (rv > 0) + if (rc > 0) { printf("x is %d in the parent process\n", x); x = 500; diff --git a/ex2/ex2 b/ex2/ex2 new file mode 100755 index 0000000000000000000000000000000000000000..8a18fb6c945886d9041f975e14330c9736bd3691 GIT binary patch literal 8744 zcmeHNU1%It6u#RZjcI5$f>Qt5Ng&fLlF&e{q-`7j6W zobS8m+;h)8cb3e|%{SM6y?(1wq4i3gSf`X~MVs58)JbIkDRmcG6wNdT`}Rj(j6C-Y zd+Q}sPydlI&LudS=1^pKsNO}!hwJ;4=EzNL)Qujd=~!b9kt_aqMhg}L({-H`IhIqE z4eCUdFkaCqjTYCs{PDJKcH{ME5085TnsmHdbcY+yOj((NWuwm@Z&1e@(f;JD`E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in this directory // and then calls `fork()` to create a new process. Can both the child and parent access the file descriptor -// returned by `fopen()`? What happens when they are written to the file concurrently? +// returned by `fopen()`? + // Yes they both are able to because it is simply stored as an integer +// What happens when they are written to the file concurrently? + // They both can write to the file, but mistakes happened when I ran it a few times. #include #include @@ -8,7 +11,67 @@ int main(void) { - // Your code here + FILE *file = fopen("text.txt", "r+"); + fprintf(file, "Hello, world!\n"); + + int rc = fork(); + if (rc < 0) + { + printf("Fork failed"); + exit(1); + } + if (rc == 0) + { + printf("Begin reading file on child process\n"); + while(1) { + int c = fgetc(file); + if( feof(file) ) { + break ; + } + printf("%c", c); + } + + fprintf(file, "Hello from the child process\n"); + + printf("Begin reading modified file (child)\n"); + + while(1) { + int c = fgetc(file); + if( feof(file) ) { + break ; + } + printf("%c", c); + } + + fclose(file); + } + if (rc > 0) + { + printf("Begin reading file on parent process\n"); + + while(1) { + int c = fgetc(file); + if( feof(file) ) { + break ; + } + printf("%c", c); + } + + fprintf(file, "Hello from the parent process\n"); + + printf("Begin reading modified file (parent)\n"); + + while(1) { + int c = fgetc(file); + if( feof(file) ) { + break ; + } + printf("%c", c); + } + + fclose(file); + } + return 0; } diff --git a/ex2/text.txt b/ex2/text.txt index e69de29bb..3043fbde0 100644 --- a/ex2/text.txt +++ b/ex2/text.txt @@ -0,0 +1,11 @@ +Hello, world! +ello from the parent process +Hello, world!Hello from the child process +Hello from the parent process +Hello, world!Hello from the child process +Hello from the parent process +Hello, world! +Hello from the child process +Hello from the parent process +Hello, world! +Hello from the child process From be454fc40bf137927e2b0b7569a3d11aa30f7c00 Mon Sep 17 00:00:00 2001 From: Samantha Gatt Date: Wed, 13 Feb 2019 15:17:10 -0500 Subject: [PATCH 03/10] Updates ex2.c --- ex2/ex2 | Bin 8744 -> 8744 bytes ex2/ex2.c | 23 ++--------------------- ex2/text.txt | 3 +++ 3 files changed, 5 insertions(+), 21 deletions(-) diff --git a/ex2/ex2 b/ex2/ex2 index 8a18fb6c945886d9041f975e14330c9736bd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`U5L_yH;=$>lW5uPzgDm zJMb8WlS5W;jvDTd?PKFnJ*I0>DW;EV(xJ<7eKguZ$Hjqz5p6_INE6y{R8NdZ|LLv{ zPiTqBj$MR475%O3n?DBb?Do@D@kn0X8CLmce1-Q(-f!@p=DooCecl&XFP-x|67#CF gAU*4x-N%x{)g0jU6H)LW#!j^tpV_ZgT9f*Pf3}4VQ2+n{ diff --git a/ex2/ex2.c b/ex2/ex2.c index 6fd2136e3..4795a6d4c 100644 --- a/ex2/ex2.c +++ b/ex2/ex2.c @@ -1,7 +1,8 @@ // Write a program that opens the text.txt file (with the `fopen()` library call) located in this directory // and then calls `fork()` to create a new process. Can both the child and parent access the file descriptor // returned by `fopen()`? - // Yes they both are able to because it is simply stored as an integer + // Yes they both are able to access the file descriptor because it is simply stored as an integer. + // But only the parent seems to read the file when I execute my program. // What happens when they are written to the file concurrently? // They both can write to the file, but mistakes happened when I ran it a few times. @@ -34,16 +35,6 @@ int main(void) fprintf(file, "Hello from the child process\n"); - printf("Begin reading modified file (child)\n"); - - while(1) { - int c = fgetc(file); - if( feof(file) ) { - break ; - } - printf("%c", c); - } - fclose(file); } if (rc > 0) @@ -60,16 +51,6 @@ int main(void) fprintf(file, "Hello from the parent process\n"); - printf("Begin reading modified file (parent)\n"); - - while(1) { - int c = fgetc(file); - if( feof(file) ) { - break ; - } - printf("%c", c); - } - fclose(file); } diff --git a/ex2/text.txt b/ex2/text.txt index 3043fbde0..97ca418b7 100644 --- a/ex2/text.txt +++ b/ex2/text.txt @@ -9,3 +9,6 @@ Hello from the child process Hello from the parent process Hello, world! Hello from the child process +Hello from the parent process +Hello, world! +Hello from the child process From c835cdf1f6be43131d7d8473fb3571f685da7f3a Mon Sep 17 00:00:00 2001 From: Samantha Gatt Date: Wed, 13 Feb 2019 15:20:46 -0500 Subject: [PATCH 04/10] Completes ex3.c --- ex3/ex3 | Bin 0 -> 8568 bytes ex3/ex3.c | 17 ++++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100755 ex3/ex3 diff --git a/ex3/ex3 b/ex3/ex3 new file mode 100755 index 0000000000000000000000000000000000000000..7e2cc5dfeb75ee15a42de0678a843cd61b413b27 GIT binary patch literal 8568 zcmeHM-)mGy6h5oA4M|8gFU4ZTYak`wQ=Zx~`jk##qj_?32se0r4vS zVz$z5isOxg5aR7O8DSg_=rVY(nuY*R<=w)9TSi+PZ^q!gZ}J(F_qObq--U`__KI^R zA&yr!cyF41#!>0OMu$?1#fx4sr@Z29NynZ0aogZ=&nb-K7(q|)U>e_}fm+jGHu&Q&YEJAdTEBbb+$4Mx8OL;O`I;PY)0lP@cl0n318z%pPNunbrR zECc_420l&K{z@-DbiPkL=VBUP;!#U&EnnJ_{E@ZjFzd9;># z+TeLvC$0>@L*h-Cb!klM{=aqCwLt`98?}35_pe^sYK#PlRh{t=xjdiNQjM{V^zwJk z`ETp3p&!uF8v0Q`f6~ui@SIvZcnfCYTx;kCY(0;MG#|8Dtvrvq%|GZ7ta+avkFm{7 zcqdBb&z;$fS8#KJKaqt}x6GAFxop)9&eh@nyr|`Qndj;EV5$6%N}4;} zW_?trvzz42|G%=aUl@DE*xkn1Hp_rzz%pPNunbrRECZGS%YbFTGGH073|I#K+YAg0 zCUqrqV4TH1T?|)Q)Q3^LK3kT$l#>JF%3bn&`upL};(~&{1AhU9X@8c{FrteY&tLFz z2q_7LI^-TTpT}tT!lLh@CY~Ye;rWbLlp$VP?3ML{IvMvD#$j`Rf)%~sqT+QO2lpGi zxZu49Z2Jy_*EP`?^~H{OG!}mli-TzdwPV&zP0e>a-tM3xy>l%}uO;9oU?$|G&BhK| I@gtCb01tl;)Bpeg literal 0 HcmV?d00001 diff --git a/ex3/ex3.c b/ex3/ex3.c index 3a3698c1f..e8128f557 100644 --- a/ex3/ex3.c +++ b/ex3/ex3.c @@ -9,7 +9,22 @@ int main(void) { - // Your code here + int rc = fork(); + + if (rc < 0) + { + printf("Fork failed"); + exit(1); + } + if (rc == 0) + { + printf("hello\n"); + } + if (rc > 0) + { + waitpid(rc, NULL, 0); + printf("goodbye\n"); + } return 0; } From 201e18aec3b4e77667c8738f519a0866ed8146f9 Mon Sep 17 00:00:00 2001 From: Samantha Gatt Date: Wed, 13 Feb 2019 16:22:14 -0500 Subject: [PATCH 05/10] Completes ex4.c --- ex4/ex4 | Bin 0 -> 8712 bytes ex4/ex4.c | 11 ++++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100755 ex4/ex4 diff --git a/ex4/ex4 b/ex4/ex4 new file mode 100755 index 0000000000000000000000000000000000000000..df762226e018cbd8ee6d65a97796a909f75860d7 GIT binary patch literal 8712 zcmeHNUuauZ7(dxsZC$&z500r7(qW~FYpU4Ns%6b=*s0#Gbxr4}Zl_6l+k_=)xJkBl zP{_c5*BHplpyEsM$p;^*AP6$3DB3sYKrPb;ADpBxp9LR`zwg|8)0^8)_Tt<5;N0)u zIlp_(?@P+bIsN|cKmU1{G2dav3WpeD9cc5-j1`$57Gsa0aWo;OgJ(i>p>tCdZ4_Ul zd!R=+JLo7xICLT0*hP--Y>YA6$J`jEBOCP)BB!t9$g;$Xx6zKpAQiJGMShdhnRF8? zxLqN#xqLKR4|(yLKnOt{w|h9gGoZ!Bd&mx0cp|PRGrED87q8pK8@BtCPrTP<-~3|P zoRLT`*&V!iVH@wfohRQd4cJHskxySuq!&aYy|^s>#(I2W<5A5?$oFDc8S6=PeHR1? zF&+#DT^ZC*mcXg2M^jz#nXA`Eh;&k9S5wjDq{!rq`{Fq;^!cFiju8LuIt#J1?E0AO z!|TL&nxBJ5z8BA_DZN98WaPi*=eP0v)EPp)8;{o8A~V)Qh}d#EyEj8Ge`6jz^4;rm zK}mD(d#X*GeLXZj;e5yZo;tuMX|an+^OALvQN~U%Ru~}LZ_T#_+W9O{c-r>Yp{XCi z(0Ah&{8s2n4`VCPN6@Cx+(Lb+(L3)a^BCWDK=LSJel*Gj@>wI0Ohf|;^C0w1Cz=)=EWdY-p30*0y*_zKoY{5Po%zpvr^yBx3s27O0D-82Gx3x%je_r*^Q@Ge)2AQ z1ZQf!KSEdHc(C0+fD)%|;O?(v(FSk#Hd%vYZNmyg6X`%U&Q8Xdp1#8P)MRKhz@~#? zK1!*V4*H(-PY-veViDvmSN`D|>Kx&i*|(Wa}|ozh-Oa_vhQTFKqqb zZ%{oI1BwB~fMP%~pcqgLCr6Ww@2etX{{ku(ytV)U literal 0 HcmV?d00001 diff --git a/ex4/ex4.c b/ex4/ex4.c index 0221ca96e..770cd261e 100644 --- a/ex4/ex4.c +++ b/ex4/ex4.c @@ -2,6 +2,7 @@ // to run the program `/bin/ls`. Try a few variants of `exec()`, such as // `execl()`, `execle()`, `execv()`, and others. Why do you think there // are so many variants of the same basic call? + // They give you different options; such as including arguments as a null terminated array or listing them right in the exec function call, and also determining the environment to execute the desired program. #include #include @@ -10,7 +11,15 @@ int main(void) { - // Your code here + /* + char *args[2]; + args[0] = "/bin/ls"; + args[1] = NULL; + + execv( args[0], args ); + */ + + execl("/bin/ls", "ls", NULL); return 0; } From 95b1d73c7616b252c18d070e8d1d11b8c78ac568 Mon Sep 17 00:00:00 2001 From: Samantha Gatt Date: Wed, 13 Feb 2019 16:46:45 -0500 Subject: [PATCH 06/10] Completes ex5.c --- ex5/ex5 | Bin 0 -> 8976 bytes ex5/ex5.c | 29 ++++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100755 ex5/ex5 diff --git a/ex5/ex5 b/ex5/ex5 new file mode 100755 index 0000000000000000000000000000000000000000..79ebd5df1141773d15aae1d591f7ba7b29684d67 GIT binary patch literal 8976 zcmeHNO>7%Q6dor{8`@H*5<*4%bQ=gNe{Fs!3P@F*DBEt^5CTm_0JT#mc48AJvDS`~ z93llpg;ap~I`M%j%XT53Q#EE^< z%zNLPH_x-*jIG%j|8Vz@zqSYwsug0kMhFo`Ia4RZSz*8u;vp0ZMatfeBj%gtYp>8- zEum_7PsBVc2$ZtV?C+}%QT-dLeL`c5O<*{xFha^~d@4)J5r4kf`|&VndR-5Sc#|7U zy-Cb^p_G~I#6YIv^59LHGd){rDNEx%-s>;0=Etb8BihZ zR^h;Pw6t8C|9*bCv>WCn6p=HFrOhx(TTtc9<++(-l2oEDSZcoWN9R$3uC?bxhQ?g81q8j zT$;Na%9%^6UzW?|)tR%Wvj_#v(rTtvq;-haSG11PxprbdYwf3e=Kg>X|WLDjhnhLmjk#&#trxEO&o402`w;>ZTK?r7(*-wdoTR) zsbrRRGwdN8YtU||oyHA!c8D+n$m0*%7VSz)FOVIN#@Nyid(uv38Kxy9@gIR2Ihw$y z#0Mv`aooX2Il{+AMI%o}L`^g~CZ5X-i`H5(i~GXb7ICmubhe2jwc@6M6Ot0E+lETW z01A7CLp-Z;4on_Y*g0o$&tby%3BmhW;zqUuCp5-=qQYs7_W>i0p-LjCz4(L=pY~zi zcY5RB_F?{$!P`FV!|(X;dp>;5hk0kq^EEt@;Mo)JZh05V>royr;W&p9;r%=BlK(B< zz)k%6Ig+EC8#$U8Zc%lss@o9jS(9odRe79Ms$9+jbyk?OwkoU2Sx@dta@LSuJ?oaU F_#01X+Ts8J literal 0 HcmV?d00001 diff --git a/ex5/ex5.c b/ex5/ex5.c index cbf3b8e61..18bbd5f35 100644 --- a/ex5/ex5.c +++ b/ex5/ex5.c @@ -16,7 +16,34 @@ char* msg3 = "hello world #3"; int main(void) { - // Your code here + char inbuff[MSGSIZE]; + int p[2]; + + if (pipe(p) < 0) { + printf("Pipe failed\n"); + exit(1); + } + + pid_t rc = fork(); + if (rc < 0) + { + printf("Fork failed\n"); + exit(1); + } + // parent + if (rc > 0) + { + for (int i = 0; i < 3; i++) { + read(p[0], inbuff, MSGSIZE); + printf("%s\n", inbuff); + } + } + // child + else { + write(p[1], msg1, MSGSIZE); + write(p[1], msg2, MSGSIZE); + write(p[1], msg3, MSGSIZE); + } return 0; } From ce5695d9ab2cf19707601782a690bad9f2555523 Mon Sep 17 00:00:00 2001 From: Samantha Gatt Date: Wed, 13 Feb 2019 17:09:52 -0500 Subject: [PATCH 07/10] Completes ex6.c --- ex6/ex6 | Bin 0 -> 8636 bytes ex6/ex6.c | 15 +++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) create mode 100755 ex6/ex6 diff --git a/ex6/ex6 b/ex6/ex6 new file mode 100755 index 0000000000000000000000000000000000000000..7d370c2da0c6c161940809f4c326b6f06ac6865e GIT binary patch literal 8636 zcmeHNU1%It6uy)6CunH44{H2_NoBQGXi`xL7Q=4o=oA}`Z5Bk7yML2pba!^$nQarn z5($FCy08zv)CbWwANml%hdzh~RFaCcr4L$4kvynu!9EB9R}t6q-MN#_PBgyya1We0 z=R5b@Gk3l_19$J;OMl$_Yd2$BKVyr1jIklo#7@TEW;%I{-A!teaz2%Q#(2p%@jRlv zqV>XmM5uF>5;>nXUYzbt5&aXrF(xsMjnQy~B7<|!n)l!Z60eutLk~ku-^+I39nu$O z2V0Cn&RwsbcQ-@vdgDY0>J7_`5QiNGWW8N75Y*#yR;g;$C>F0bCF_mLd|>dtDO<)@ zaJ`!C%*up#y?1225!ntLm7dzvz(#6&kGIZ)^~OM*mKVsM~{WygdXFKFyu2q zW>7=ABEFBl+c9`LV|bPUvI3J28P^$0k)0=tIOY=X@-kuMzdXWNmhA1MQ>0NrE;PzR zv>pB!^+(^h&}WAsu|d+&x?3A9+4<4pLa9g?axW>yQN640>y~@?)aM_(J8^T@fu%<& zrju%rvH<<^hEFmJuNkvcNOhMEfmzQ8imY}VJ?AHr!o{yMGu%*KRoeA|AHB8M*c7yVV!udkSm z%u2gSp|Hzk(>KnyZP6`r*?c6kZrWGaH}4-d`!iPww#`DlV`|^|zxtCM-}tm)Y%Hy4 zzOixRjc&JVHlI2}UC(1E4YV)&YiApi%fJ2Z8=XeIv&`D}1ot=2`D^CVSJ_v-Y;}{@ zDArA07uO%eb)BxqoA-Z0CVjG-{D^F8jxOxR&vcu)JK$Z1_W-;r@D9OSBQJf*s^w-a z-LuPbMYz7Gyb)M9;$_3vY3q4on+$FR zpiaRVyJR^Pp#6lZbaw5{0F4nkU9&wTl4zXH^!s>uj$89qq3&69W}d@sM#?$cVM%Oi z^t1JFpCfb-9@2snKx|~(DR8j=0gQaIn|mA&#qbj`JQ2fB$M9qfV?PDw%=k|NHmu~C hZYmgVZ<|i1##^6{i|3259iuTMFr-eBwq-gP`x_OeRyY6v literal 0 HcmV?d00001 diff --git a/ex6/ex6.c b/ex6/ex6.c index 17532d65f..bd61ae3f7 100644 --- a/ex6/ex6.c +++ b/ex6/ex6.c @@ -20,7 +20,18 @@ and `clock_gettime()` should work just fine. int main() { - // Your code here - + int nanosecs = 0; + for (int i = 0; i < number_iter; i++) + { + struct timespec start, stop; + + clock_gettime(CLOCK_MONOTONIC, &start); + write(fileno(stdout), NULL, 0); + clock_gettime(CLOCK_MONOTONIC, &stop); + + nanosecs += BILLION * (stop.tv_sec - start.tv_sec) + stop.tv_nsec - start.tv_nsec; + } + nanosecs /= number_iter; + printf("Average time is: %d nanoseconds\n", nanosecs); return 0; } From d6996482fe2dd7a1c0d74d9639d5dacc9c160282 Mon Sep 17 00:00:00 2001 From: Samantha Gatt Date: Wed, 13 Feb 2019 18:33:05 -0500 Subject: [PATCH 08/10] Implements bankers.c without locks --- stretch/src/balance.txt | 1 + stretch/src/bankers | Bin 0 -> 9632 bytes stretch/src/bankers.c | 39 +++++++++++++++++++++++++++++---------- 3 files changed, 30 insertions(+), 10 deletions(-) create mode 100644 stretch/src/balance.txt create mode 100755 stretch/src/bankers diff --git a/stretch/src/balance.txt b/stretch/src/balance.txt new file mode 100644 index 000000000..2036146d0 --- /dev/null +++ b/stretch/src/balance.txt @@ -0,0 +1 @@ +8966 \ No newline at end of file diff --git a/stretch/src/bankers b/stretch/src/bankers new file mode 100755 index 0000000000000000000000000000000000000000..b4a8fdcd916882944597d2c49cc74dfff8a380ac GIT binary patch literal 9632 zcmeHNZ)_Y#6`xDv#&sLcp`oEbl}(6gQb=OAMRF1%C6Fyw;WVa=ixo)M=exD<$obCN z+ly_1BR8%^wwsd-D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&75U-0CM zxzUM7WV~<>P2n;{rqC#RvNYdPFW#)}F4_{K!de?J-xW=!C~t&3YG;zEOz0w|R=P1&>;Ak%B6SN|N%0(qk(;~+wYs1Zwyo&?5W z#&Wia-vmS%u>*9%Q1?JaS2AUagCRNaO5u+}@`9=;6o*5+sw%-BhcHg*W~Rr8_GEHt zTRa`=i6+zHXCdAM-6Y0tM={4@bUBKlJ5Ur>P__bfjoMtO5YnK@#1`RDz2tc@#JB1& zm^s`h5%Z>#xPW(7;;qGcg?X9E`r_{|9#vSV`nbaNb$)yP;sWY4CDTEa<@l)p{#5|K z6u>VB@Yw+VZ2-U1c;frh9-wfQpfpu-jWi5AaK(}H`7?m4_2U^)NRB^t^1E@mcpLkN_A9nu1 xq>d#2F!GO~(g9R<^b`*rb=>#|P4S2+o-O{NV#ph3b&UAuhwr$!?r=r?{{vETM_T{@ literal 0 HcmV?d00001 diff --git a/stretch/src/bankers.c b/stretch/src/bankers.c index b44aed25c..aa98d43c2 100644 --- a/stretch/src/bankers.c +++ b/stretch/src/bankers.c @@ -90,6 +90,7 @@ int get_random_amount(void) // !!!! IMPLEMENT ME: // Return a random number between 0 and 999 inclusive using rand() + return rand() % 1000; // ^^^^^^^^^^^^^^^^^^ } @@ -114,17 +115,25 @@ int main(int argc, char **argv) // Check to make sure they've added one paramter to the command line // with argc. If they didn't specify anything, print an error // message to stderr, and exit with status 1: - // - // "usage: bankers numprocesses\n" + + if (argc < 2) + { + fprintf(stderr, "usage: bankers numprocesses\n"); + exit(1); + } // Store the number of processes in this variable: // How many processes to fork at once - int num_processes = IMPLEMENT ME + int num_processes = atoi(argv[1]); // Make sure the number of processes the user specified is more than // 0 and print an error to stderr if not, then exit with status 2: - // - // "bankers: num processes must be greater than 0\n" + + if (num_processes < 1) + { + fprintf(stderr, "bankers: num processes must be greater than 0\n"); + exit(2); + } // ^^^^^^^^^^^^^^^^^^ @@ -151,17 +160,27 @@ int main(int argc, char **argv) // Open the balance file (feel free to call the helper // functions, above). + int balance_file = open_balance_file(BALANCE_FILE); // Read the current balance + read_balance(balance_file, &balance); // Try to withdraw money - // - // Sample messages to print: - // - // "Withdrew $%d, new balance $%d\n" - // "Only have $%d, can't withdraw $%d\n" + + if (balance >= amount) + { + int new_balance = balance - amount; + write_balance(balance_file, new_balance); + printf("Withdrew $%d, new balance $%d (pid: %d)\n", amount, new_balance, (int) getpid()); + } + else + { + printf("Only have $%d, can't withdraw $%d (pid: %d)\n", balance, amount, (int) getpid()); + } // Close the balance file + close_balance_file(balance_file); + //^^^^^^^^^^^^^^^^^^^^^^^^^^ // Child process exits From aea2416ae03d9f144cec47d64acf91ce63903e80 Mon Sep 17 00:00:00 2001 From: Samantha Gatt Date: Wed, 13 Feb 2019 18:36:52 -0500 Subject: [PATCH 09/10] Completes bankers.c with thread safe locking --- stretch/src/balance.txt | 2 +- stretch/src/bankers | Bin 9632 -> 9680 bytes stretch/src/bankers.c | 3 +++ 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/stretch/src/balance.txt b/stretch/src/balance.txt index 2036146d0..cd93a2fff 100644 --- a/stretch/src/balance.txt +++ b/stretch/src/balance.txt @@ -1 +1 @@ -8966 \ No newline at end of file +7650 \ No newline at end of file diff --git a/stretch/src/bankers b/stretch/src/bankers index b4a8fdcd916882944597d2c49cc74dfff8a380ac..976da8530bc4160915bc3c90ee40e09872c093e2 100755 GIT binary patch delta 1842 zcmZ8ieN0Uu z!p&Q?<7NhvnUJ_m@FVFQ;xsTJEVTQ`|u*jahV0O9QMWfm|o1F=^`1g!os-C>(FTGX4g8DKk#HRHJABl(j z6v*EVFJfliRhyw+&n6Nq(h>H1f;G;dXHw$hDMkY!wE_RidNf+ACS^Rt_9pJ}syae! ztJJ>)t~$O9Q5!-lN;hq@F@yT&(f$Z*CGZud$9$#vJZ34>AI9-7V4P0T@kJ0lJQ7o2 z7c58#RJ6h-Ypqy7HFUx<6cs)%t|`y4yNTvFNyqLdX2q2mSh_J+T4Q^S7KnCZanTs0 zXrr)G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xuIbfjMK61tNNmxu_l;XK^*_eZdmZD9H1#MJi{suIkd zQR)Dcl~uT=fvO|WO~r_^a2KU#fr|5T@4S)*v$D8QDMm~KJE0Z$<6?x0b1*4;=9GV6 zx$KF^_BrYaQTv2)6Ba>SDa6U1kZhl5pi`qnnFE`OUq*{D!?PgUBh)ia?W4+>-D$U2Z+M%Ik%Iz)cBm0SRX9r;RrM~a!}sQfQFS-AxiMr!6?o-IkTqNTEul=JB{3|) zO;CKBp@g@fw}ph5A@8y0V{ixX4&jg71SFiA)Ky%^xW)$w=kNU|?Bkm7`DU)oMs_^! zE!(#sVKpYJAAFYX*FUe*zp;w^oa(>Ux%f`Hze|@CYpKR{+RRSn=Li;dAwP#H`8BM( OU@r?6%&>;S*Zu`rEf}Bx diff --git a/stretch/src/bankers.c b/stretch/src/bankers.c index aa98d43c2..d4cfb33db 100644 --- a/stretch/src/bankers.c +++ b/stretch/src/bankers.c @@ -162,6 +162,7 @@ int main(int argc, char **argv) // functions, above). int balance_file = open_balance_file(BALANCE_FILE); + flock(balance_file, LOCK_EX); // Read the current balance read_balance(balance_file, &balance); @@ -171,10 +172,12 @@ int main(int argc, char **argv) { int new_balance = balance - amount; write_balance(balance_file, new_balance); + flock(balance_file, LOCK_UN); printf("Withdrew $%d, new balance $%d (pid: %d)\n", amount, new_balance, (int) getpid()); } else { + flock(balance_file, LOCK_EX); printf("Only have $%d, can't withdraw $%d (pid: %d)\n", balance, amount, (int) getpid()); } From b3e2b26f3d6f8a0eea2842a028b973fe49ba4cbb Mon Sep 17 00:00:00 2001 From: Samantha Gatt Date: Wed, 13 Feb 2019 18:47:53 -0500 Subject: [PATCH 10/10] Answers questions in the stretch goal README.md --- stretch/README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/stretch/README.md b/stretch/README.md index 8fe640818..87f15d6ba 100644 --- a/stretch/README.md +++ b/stretch/README.md @@ -66,16 +66,19 @@ simulated bank account, that is. Don't get your hopes up.) 1. **Short answer**: How can things go wrong if two processes attempt the above plan at the same time? Is there more than one way things can go wrong? + - Without locking the file before reading and writing, one process could read the file right when another process deletes everything before rewriting. There could also be two process that read the file before either have the chance to change the balance, resulting in the wrong calculations of the balance being written to the file. 2. Study and understand the skeleton code in the `src/` directory. **Short answer**: what do each of the arguments to `open()` mean? + - The first one is the path to the desired file, the second one is the flags, and the last argument is the mode the file is opened with (read/write capabilites). 3. Take the skeleton code in the `src/` directory and implement the pieces marked. Run it. **Short answer**: What happens? Do things go as planned and look sensible? What do you speculate is happening? + - Without locking the file, sometimes a process will read that the balance is $0 even though it is not. It is probably reading the file in the middle of when another process is rewriting it. 4. Add calls to [`flock()`](https://linux.die.net/man/2/flock) to capture and release an exclusive lock on the file before reading and @@ -86,7 +89,7 @@ simulated bank account, that is. Don't get your hopes up.) 5. **Short answer**: Why is it working? How has adding locks fixed the problems you noted in question 1? How is overall performance of the application affected? - + - By locking the file when reading and writing, only the respective process can edit the file. That way the file can't be overwritten before the first process gets the chance to update it. The performance will slow down since it essentially turns it into a queue but it also reduces the risk of mistakes happening. ## Stretch Goals