From 100dc3f1c60a42ccbf0583f86bd33e851af02ae9 Mon Sep 17 00:00:00 2001 From: Farhan Farooqui Date: Wed, 20 Feb 2019 13:43:38 -0500 Subject: [PATCH 1/4] exercies 1,2,3 complete --- ex1/ex1 | Bin 0 -> 8480 bytes ex1/ex1.c | 17 ++++++++++++++++- ex2/ex2 | Bin 0 -> 8616 bytes ex2/ex2.c | 17 ++++++++++++++++- ex2/text.txt | 3 +++ ex3/ex3 | Bin 0 -> 8480 bytes ex3/ex3.c | 10 ++++++++-- ex3/tempCodeRunnerFile.c | 5 +++++ 8 files changed, 48 insertions(+), 4 deletions(-) create mode 100755 ex1/ex1 create mode 100755 ex2/ex2 create mode 100755 ex3/ex3 create mode 100644 ex3/tempCodeRunnerFile.c diff --git a/ex1/ex1 b/ex1/ex1 new file mode 100755 index 0000000000000000000000000000000000000000..36287e344645f52f18b5f9ef8bb390f70f31adb9 GIT binary patch literal 8480 zcmeHM-D@0G6h9kV4KXwu5UFV48eGvLjURoGqOdU?-D1_)Mi8M~_G8i=oc&;C)+9(F zB^C)%&8si|1wQ!VyQt8<`Kq9xppwuE6@r8yuD{=%JDb@}^jYB^_RKwB_sluJI|Fy; zezW)6U-yU{iGn2Z0Js9ydSUJ*?=A1bYqSniVOTv>W1epjsP&@v z=HhUOnO_{XiPce?9JR~{trLGGp_I~SzO`d$2Cl2tPaW;-(-B$QSC}tOdbN098qGHb zK$vgRMr3+6Vch1s+Zxh*y6J~qKZ0#EU(x1!#Ku!6?gKhdUp-Eupncf}jOIIS^DSFH z^*jybhC}OKdpT$~bkJVv7=3s>KDGI{<|OK)IZ{u0<+^TxwLUwyIG6Ka{fJw(j-w;j z70=utx7O`Y$E&ScC)C{}`p0~O#{>fBXGHw-b!r_}|1Dq9)|cg@p3len^3TowdvN~T zt1o+J&t=cB8tD@{NtxzFn8%97K7;q{NfRJ4j&WRx95wBL( z_l*AVhrRD^?Sx-G4VwZNz;bNDFlAH2k=*--p=}!9hrLX2`H88&%cY|gWm3EvgM_Q z_f60p7*0b%5^O{<>-IJVPWT8zI^jM@PyMmsZEf7CtbeU8eYM#yjbp>wFHM*&?D^{_ zen-gM#eV50jGIkdWc$~Czu)8*b^Cir^9!n3ja6K2`Rbi&*z*;%R~F8wQe#5qn?cx6 z-KbOd<5(>RVW`@jWmRuh+n0TnG|iMVN?wdQE!C<9?Ln;dAzcuw&|gYSH1CpTHhb)c zSPv*aa=*;|Ga^j2si{B0uBL( zfJ49`;1F;KI0PI54uSs{0@E|a3#@p$%)>r&7_MtLAI9PJ>yd0qRGcpBrB3uV<>QdM zIE+q~pjj$toJ)@BR#W>ce!Z9YIQy=0`osLVA*J*{a_xi-&jgyM3hBupKS*zrLxP!>w{KN?l literal 0 HcmV?d00001 diff --git a/ex1/ex1.c b/ex1/ex1.c index c4b111641..1f01420ea 100644 --- a/ex1/ex1.c +++ b/ex1/ex1.c @@ -2,13 +2,28 @@ // (e.g., x) and set its value to something (e.g., 100). What value is the variable in the child process? // What happens to the variable when both the child and parent change the value of x? +// Answer: At the start the child process has the same value (100). +// When changed the variable has two values one on the main thread and one on the forked thread. + #include #include #include int main(void) { - // Your code here + + int x = 100; + + if (x == 100){ + int new_process = fork(); + if (new_process){ + printf("CP has same value as MP: %d\n", x); + printf("Child process will now change the val: "); + x = 10; + } + + } + printf("From main process the value is left the same: %d\n", x); return 0; } diff --git a/ex2/ex2 b/ex2/ex2 new file mode 100755 index 0000000000000000000000000000000000000000..81ba892d25911726b3aa85ac86a865c2b1e57186 GIT binary patch literal 8616 zcmeHNU1%It6h7M@4Q*^T50#2mrp0Zmg`|~6L=-lmgIf$*ZAwc)F1y)DcX6{>W+#|D z1cFg;s0(>26&3p6gD-*)KKnxjBM4R>q%A=zsB9oY-$W7D@4ItncXm>J?!!HB=A7@| zbI;uS-5Iz$XTSUF_kZpe$!r%{-X^7zIod6-s_o#`8aXeta(42_2LWe$y4z&?4;HSO!-JJKFVQS-F^`7*)e*K$N^|6794C6; zDzJM9#zRrVmXDW2y18nCy#v2jd50kg#}7#DTJJbiV12)~cn^@?xzF zOnDf}d0amCdGXNC&+fjmaQOShYm*~SqRoM3pt5I|R{EOD!FMxHzz4Nm0pH8a!}l^N zyzkRc_Hhk6*M7lko9WF3^EtEF47}Nqi4n}JT0#)y5S00^I1xXO8+;YmieUFs zSOzQumI2Gaz0bheLilT8WiwxBJ@*G@TWF2_3G=o4H>ciyn@yp6r*LWMP9a>n-45B% z8oQy*-kUU+mTre%ej@D(KzIQV*dlAtyAo;G{mkGihw!Hf&t zZ+V~g1MrW36S`~RT4Cj@{HxbGxgXKg$?>k8+%0`w*VmtL6`5@9{}^WcOegmN>?&s> z8^3iroeEFA8|(CVpx*d_-eG!c^q!*k89e^R9SQI|*FP$`rsL$wyX2hb6n)RB*7aHY z5rk5bmgi=kt53jE$INd*`aF*hVq`WtsRQ*Kf>_Gt_g~%E1!LbZ_8nv6cxx>KmI2Fv zWxz6E8L$jk1}p=X0n318z%pPNxc3eRi9gttfB7o1PCm)X0mx z&4Jk>zG>w6@nXBI?vJ9o4x5KEQ3a88#RoNxzH<=s`v)vtEsx=v#Qczx#K%+knG~K# t;b>aXe+XD#W^L8fh4Xjob5Ob7x<;kf81rZtgaLP5)?&W4>;vv7@-I%NDEt5b literal 0 HcmV?d00001 diff --git a/ex2/ex2.c b/ex2/ex2.c index 4245375b9..88803d04b 100644 --- a/ex2/ex2.c +++ b/ex2/ex2.c @@ -2,13 +2,28 @@ // 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? +// Answer: Yes they can both access the file descriptor. The file is written to 3 times. + #include #include #include int main(void) { - // Your code here + FILE *fp; + + fp = fopen ("text.txt", "r+"); + + fprintf(fp, "%s %d\n", "We are in", getpid()); + + int new_process = fork(); + + if (new_process) + { + fprintf(fp, "%s %d\n", "We are in", getpid()); + } + + fclose(fp); return 0; } diff --git a/ex2/text.txt b/ex2/text.txt index e69de29bb..01cd6a13c 100644 --- a/ex2/text.txt +++ b/ex2/text.txt @@ -0,0 +1,3 @@ +We are in 43508 +We are in 43508 +We are in 43508 diff --git a/ex3/ex3 b/ex3/ex3 new file mode 100755 index 0000000000000000000000000000000000000000..726d4ab9b97ced0f9acb5d81ccb04d043d6a6601 GIT binary patch literal 8480 zcmeHM&1(}u6n{~xMT<>6NW~A}?an5f)Q__;kG`4rd++Vb%x`94y7Th; zr=M#?LMuhCtq_rJ=+jjqb7H{~*#wP4l^Pp97CRq1bBf+p3AMuCBG!3@M5V@K6XUHZ z?(^YRpJvic2 zANvn<>UyiS!>gw(z9jBnrKL6k zN=@f4XY(nQ&8G`4Ze5Q@x*pe@Lfl@%i+C;9brGu6$nf}ZAcFO4Syz931M7-C*Ql*j z-cjXhE?IC?u~PbDJ--b12cP$a{0GOWl#}>dy)d5V`22dr!Ft?x!F|(w4@QrlJP{i? z?(bo%ZJxjs%bmOk>uA&U_u)O;=O&1BqVKdsdfkZESity`C>rK*PK?h0^F4TrK8k)F zbPO64=2C<62*znISR(#j4aU|3ASB%91JmWwfRjxQq^eE|n6eqlaSR=@?|ywZ`1(oT zhWmw^j}H&RXF)?yS-;j*+U^*-?u7Qj2DJ^@jrp?<-^-xzcMn1t;~Y8`-huv5b1WFo z*=o6B=lYNKV_exJk{~;wtY5S|KKE!$zKj?K3GHFkl!k3>XIf#lX4v{QLOq z$MB2T52h|2K{FrwHhcA(bYI^H5Wh7vf$pI!?s7w4mU=ud{~>^vXg>CN|I_&F^YDdd z^~gOq>X8R%M{jMtjc)jCJu-*BmO&#+AL{jbhR5HfH?(ZWDOl-JA!kiyvQBEPOce^L zWYzY3xkNUPL$-I`-a-Qs?0I(P+4=x_YC@m0Y4c3pg)?>7JFNpX4Fbv)4gfMLKeU>GnA7zPXjh5^HXVZbn87%&X{*9`RZh0kyTJyBlvxtHN8iThz(UY{;W zO-kXOs7e<~muUCGF5)t}k_OTeQn;5~p>i2z&)AdG6&rWo2|m7^-zp^HT}alp>2%+q zyF29F4Dx#eKdp4b;I|86e$zlt)0IB7LCkx_AohNyac3L#v4xxDx( H_ji8*?;+WP literal 0 HcmV?d00001 diff --git a/ex3/ex3.c b/ex3/ex3.c index 3a3698c1f..9f6535190 100644 --- a/ex3/ex3.c +++ b/ex3/ex3.c @@ -6,10 +6,16 @@ #include #include #include +#include int main(void) { - // Your code here - + int new_process = fork(); + if (new_process){ + printf("hello from child\n"); + } else { + printf("goodbye from main\n"); + } + return 0; } diff --git a/ex3/tempCodeRunnerFile.c b/ex3/tempCodeRunnerFile.c new file mode 100644 index 000000000..0bb9446b9 --- /dev/null +++ b/ex3/tempCodeRunnerFile.c @@ -0,0 +1,5 @@ + + int new_process = fork(); + if (new_process){ + printf("%s from child\n", hello); + } \ No newline at end of file From 145b139e4f3d96569fc898e62d52b34de8cf1ecb Mon Sep 17 00:00:00 2001 From: Farhan Farooqui Date: Wed, 20 Feb 2019 15:04:08 -0500 Subject: [PATCH 2/4] ex4 complete --- ex3/ex3.c | 4 ++++ ex4/ex4 | Bin 0 -> 8480 bytes ex4/ex4.c | 12 +++++++++++- 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100755 ex4/ex4 diff --git a/ex3/ex3.c b/ex3/ex3.c index 9f6535190..20e9653f7 100644 --- a/ex3/ex3.c +++ b/ex3/ex3.c @@ -11,6 +11,10 @@ int main(void) { int new_process = fork(); + + // Return value of fork() + // On success, the PID of the child process is returned in the parent, and 0 is returned in the child. + if (new_process){ printf("hello from child\n"); } else { diff --git a/ex4/ex4 b/ex4/ex4 new file mode 100755 index 0000000000000000000000000000000000000000..866be13ee9debed32c225ddafa93c12625f97b51 GIT binary patch literal 8480 zcmeHMOK4L;6us$3i&mR%q~eEXHKHzzilQ4ODGjD5T5ZJ-$k5nmf=L?kp)DvB+b#+U z1wm8<-MA4Ex9&s*yV9)-7cNvM5F`yyhUVM99SZ2p>e2E{n5j*bFq`h*;+4wdiYz!IPc)7)L`t)V7-eT zAFOW^gOQuG8l{I)rQ}$NmMe|%987@myfle1otm`BL=7-ru~beJt4)pZ@+Mxp@$j%W zpv}ZvW*XdhDwWLSlLfRj#=BzTZ884DH+mk6UtG=b=jVMhBz59;{#8x_ayDTUYG4YHX!) z8C4w5CPp$UUn=}Do)?DugX29R|NcCc$_)K2UJ&D%pBInVACLRazi(>q!O^~BM`FEw z-X3-u<_S!3^yEbt$C$dk8}HdJ?I6;Iy3G;k(mihB0OKbS6!hbq*gpo$_uvES2x_i( zKh!VurTXXL@25TIhr%D}1k%%}L| zm*DHz4-TF0K`|3soV>ay9aEbC;x`VS(bc}QsCGY}dGS={*J!VA3p}weK+ztbd=)(Z za<&q>3s?!=LwWSZhG|sMfl8>1dNzeZ7Cu)hmDDO)?_q@#>0CHd6#VoVdp&(1{+j1y zo~QSsrt&{3g*MOO?HC!UFsSOzQu{~-gN-NBPgyEDSeK7ARk@X28qm)9o>GAo5(XGA5(k{2=rT>uwH3YTGf z5>U98Y*N{j_9~Z>xceUB@J-pFbWTG2IMFN*HX47=U~@;ny&2^927WQ=fWhw=#QeU2 nnx>Y`DE*lCe17acrg~=^@vwuN Date: Thu, 21 Feb 2019 15:26:56 -0500 Subject: [PATCH 3/4] ex5 --- 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..e9be56eebd0404c24ad9eb7bc2cff5143100f359 GIT binary patch literal 8976 zcmeHNO>9(E6uv`2p&-*H#GoR1REuB~+9DB5ke2Xd2%y#01T}g)?MyoZ^OrX>DP1%{ z2ejijWEL#yt_wrdg|aXa6C(sksHBPbmvjLM@h{T^6E`Yx;rM;`-8X&nT6AUN;+^E) zbG~!VJ@348@65b+?|k{^FMr)BM4(BC<4c4PL9~HoLYx!^Bq8oW3!zEb+q2Jn(R}V1 zY8xrg2p2_+GYdy4`_04sjV@|@OQTL`jNAm$QH>r_=Ay@Plx*_GGw#C8z%;6pB3|k^ zlP?v=-B8MGECTc)D$@L>fxMMthxH!NSR8=>{xOzosgNFb=!DO7_JYFcZl_0?f`Hp zN7AlkvLIdy#xp-B93g&nvw38_@CAY8PU6 zy!nx~H2*n0xc}(`=HC6zXUy=-12CyY2V-7po|IOIHhd3sP&Vv(E(dqk0@c01$H1q- zd%?Ljn4_`5u4M)Rrhr$X(e5Vab4=Uq$iVnDix}}SVM?loTk~0KYa%|lH8PfnXy5z5 znMcPDYacl?*){yZci&tptotz51)YHwKohGmqiB@X?XP+}@GxYQwoaRH|LT75z2PR# z-3~O`c+bnGf5g{HK-67%*GqU;4T++6@ zW?%F@pv&eAdKjk)<1G1ncRmwQmNm#CWZ_pOHq4k8E?-C_PB&Yf2rI zrYY^F^ckfCls=;L0;TsUMJY`wDUnDUqiHJIV>ld7L?g}G zHf*Jn#!xJtfMz6}jtq{$!=Faw%Ws zSHQ1;Uje@Yeg*sr_!aOg;8(z}fL{T>0)7Sj3j9YEXl*lIV(8Xx$)oG=&`~)QJ1R%= zVJjk@wOT5+J`%O8jHq<;#7&);%Rw9>D}>&(U)P#cE2%+auTrJ57pT3$?6p;2RrY#vR+7Dj^y*o#oWI*B literal 0 HcmV?d00001 diff --git a/ex5/ex5.c b/ex5/ex5.c index cbf3b8e61..89fe1d963 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 buf[MSGSIZE]; + int p[2]; // a two-element array to hold the read and write file descriptors that are used by the pipe + + // establish our pipe, passing it the p array so that it gets populated by the read and write file descriptors + if (pipe(p) < 0) { + fprintf(stderr, "pipe failed\n"); + exit(1); + }else { + int new_process = fork(); + if (new_process){ + printf("hello from child\n"); + write(p[1], msg1, MSGSIZE); + write(p[1], msg2, MSGSIZE); + write(p[1], msg3, MSGSIZE); + printf("goodbye from child\n"); + + } else { + printf("hello from parent\n"); + for (int i = 0; i < 3; i++) + { + // read 16 bytes of data from the read file descriptor + read(p[0], buf, MSGSIZE); + printf("\t%s\n", buf); + } + printf("goodbye from parent\n"); + } + } + return 0; } From 21e6ace1fda4a704bd3d95905fa8cd6f0ee414a6 Mon Sep 17 00:00:00 2001 From: Farhan Farooqui Date: Thu, 21 Feb 2019 15:36:21 -0500 Subject: [PATCH 4/4] exercise 5 and 6 completed --- ex1/ex1.c | 2 +- ex6/ex6 | Bin 0 -> 8496 bytes ex6/ex6.c | 12 ++++++++++-- 3 files changed, 11 insertions(+), 3 deletions(-) create mode 100755 ex6/ex6 diff --git a/ex1/ex1.c b/ex1/ex1.c index 1f01420ea..e6423ff02 100644 --- a/ex1/ex1.c +++ b/ex1/ex1.c @@ -2,7 +2,7 @@ // (e.g., x) and set its value to something (e.g., 100). What value is the variable in the child process? // What happens to the variable when both the child and parent change the value of x? -// Answer: At the start the child process has the same value (100). +// Answer: At the start the child process has the same value (100). // When changed the variable has two values one on the main thread and one on the forked thread. #include diff --git a/ex6/ex6 b/ex6/ex6 new file mode 100755 index 0000000000000000000000000000000000000000..d44621c60d2f344b20b7e197459510e2e56937d5 GIT binary patch literal 8496 zcmeHMTWb?R6rQNnqNPm{rJ{wk7*Pvty*vm~NY%wHR;{%K6=cX|ZGxLk*bBB$Bp`x8 z3Vp5rKoIl~s09mF5u3+8_~3&|1O;Cd6fu5hcQ)N@)punMoIU58IeT*En;DqREPwy> za}#5+^^BF*F~&Ne3mX`_#T0mq?SN`f&PRt%sh8EUbJW@_v1a&NM0r*a$oaT>alARi zdq3XX#so%ftVJkAaPH_+4tc&r^Htk17{es%&88RY3V{$4%Rfrf9wq5JLId zL_~mt7g|NWjiSNN$8)+-(k-+_^Ia7A_K0}G#Ct>5)R(axD{oGUfM~uMk*`nm6AnvL zZgg<&n%DDYmgmi@MGrU6$9<8H=A49ZG>6~gKWScTP|i;djSq!-Fn>++8q9BKUa{sH zxj8otZci7|MT3_d>yP<@G_*eG{XmHSaGjhRslVlm<9(_x$VWJwkJepy-K_3|k>Rta z)sw@)8df6X1ST2q2G1Df5w6$XgMBtYp2AowOe<^`Y#@rl*ggOamf~nk)L#aseNcm? zx!D38g@%P;wGF}8%9MxpWmsr`9feXKjiF`fE$EN2 zMlGo4!nEz^g}&o`s4JgF5M(cu@=qx~pZf(a151$%NCqSWk^#wpWI!??8TkJhn9wTk zw3{E3Pv}6Y&APQ`>JJ)qjeB_THcUl*`H|72H_ThcKUe9eK?=?K88>y15XO)sw%;>hQoEV)@oP0r729<iY_wlu$$(@)G9Vd{ z3`hnf1CjyBfMh^2AQ_MhNCy5r20DAF^TWRMikS{Tg`^j~@kmLH4o^c%=ci<^Xv%8SWn=Ij9Ojf%^c;HZT zN6fz=q@1pFP>q1ID-A3Y24i=1FYaid2Mant1U9VU}S3up#pvZ DW6u8t literal 0 HcmV?d00001 diff --git a/ex6/ex6.c b/ex6/ex6.c index 17532d65f..b1ebd3b59 100644 --- a/ex6/ex6.c +++ b/ex6/ex6.c @@ -20,7 +20,15 @@ and `clock_gettime()` should work just fine. int main() { - // Your code here - + struct timespec start, end; + /* now re-do this and measure CPU time */ + /* the time spent sleeping will not count (but there is a bit of overhead */ + clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start); /* mark start time */ + printf( "" ); /* write to stdout */ + clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end); /* mark the end time */ + + uint64_t diff = BILLION * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec; + printf("elapsed process CPU time = %llu nanoseconds\n", (long long unsigned int) diff); + return 0; }