From db9665d4e4ee6f3ebe28741e547167d01651f61e Mon Sep 17 00:00:00 2001 From: linomp-droplet Date: Fri, 23 Aug 2024 15:40:30 +0000 Subject: [PATCH 1/7] add ostep exercises --- sandbox/.gitignore | 2 -- sandbox/ostep/Makefile | 18 ++++++++++++++++++ sandbox/ostep/ex5_1.c | 25 +++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) delete mode 100644 sandbox/.gitignore create mode 100644 sandbox/ostep/Makefile create mode 100644 sandbox/ostep/ex5_1.c diff --git a/sandbox/.gitignore b/sandbox/.gitignore deleted file mode 100644 index b40d66a..0000000 --- a/sandbox/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -msys_server -server \ No newline at end of file diff --git a/sandbox/ostep/Makefile b/sandbox/ostep/Makefile new file mode 100644 index 0000000..68dfb33 --- /dev/null +++ b/sandbox/ostep/Makefile @@ -0,0 +1,18 @@ +CC=gcc +CCOPTS=#-Wall -Wextra +LIBS= + +SRCS=$(wildcard *.c) +TARGETS=$(SRCS:.c=) + +.PHONY: all clean pristine + +all: $(TARGETS) + +clean: + rm -f $(TARGETS) + +pristine: clean + +%: %.c + $(CC) $(CCOPTS) -o $@ $< $(LIBS) \ No newline at end of file diff --git a/sandbox/ostep/ex5_1.c b/sandbox/ostep/ex5_1.c new file mode 100644 index 0000000..4947639 --- /dev/null +++ b/sandbox/ostep/ex5_1.c @@ -0,0 +1,25 @@ +#include +#include +#include +#include + +int main(int argc, char *argv[]){ + + printf("hello world (pid:%d)\n", (int) getpid()); + + int rc = fork(); + + if (rc < 0) { + fprintf(stderr, "fork failed\n"); + exit(1); + } else if (rc == 0) { + printf("hello, I am child (pid:%d)\n", (int) getpid()); + sleep(1); + } else { + int wc = wait(NULL); + printf("hello, I am parent of %d (wc:%d) (pid:%d)\n", + rc, wc, (int) getpid()); + } + + return 0; +} \ No newline at end of file From 52ce485b6f6d68132ec165d35746c62a1f630769 Mon Sep 17 00:00:00 2001 From: linomp-droplet Date: Fri, 23 Aug 2024 15:47:10 +0000 Subject: [PATCH 2/7] simplify folders --- {sandbox/ostep => ostep}/Makefile | 2 +- ostep/ex5_1 | Bin 0 -> 16256 bytes {sandbox/ostep => ostep}/ex5_1.c | 10 ++++++---- 3 files changed, 7 insertions(+), 5 deletions(-) rename {sandbox/ostep => ostep}/Makefile (83%) create mode 100755 ostep/ex5_1 rename {sandbox/ostep => ostep}/ex5_1.c (65%) diff --git a/sandbox/ostep/Makefile b/ostep/Makefile similarity index 83% rename from sandbox/ostep/Makefile rename to ostep/Makefile index 68dfb33..6d01a79 100644 --- a/sandbox/ostep/Makefile +++ b/ostep/Makefile @@ -12,7 +12,7 @@ all: $(TARGETS) clean: rm -f $(TARGETS) -pristine: clean +pristine: clean all %: %.c $(CC) $(CCOPTS) -o $@ $< $(LIBS) \ No newline at end of file diff --git a/ostep/ex5_1 b/ostep/ex5_1 new file mode 100755 index 0000000000000000000000000000000000000000..3b572349190354579e48c8223dd094b64fdbdcb2 GIT binary patch literal 16256 zcmeHOeQXrR6`wo4Oaj;>kbr5!mO^A2ytctO1mtr5I5<}ZTri3vO1Ep@+IP+Q?sB&m z+fhlKkVc#gsw1kFKO!|zkwTS9rB$njsA){oKof~5sHzI8QbANCTcwS0XzR2I*Y{@L zTkoyzntuJ)eb(Na_j~U*Gy8V-Zq_^V)o|yIU?3n^=7`SL+XVJMC#m2GQiFqZ1ea!rINl+#IQuZgHg9qp7`9q?v~%@=g%%1o2o;QG2X8 zpO_OowopF3x*mKeti8M#@w~cS(FAL317+VeJNlor*G0Uenyf~6b)$j>Yi{rJz{9-! ztHPIgFSS?hIt)?%SyOsNT_WDo)L55@)+XY~+)(Y%mZsXKMm?L-H_9$xzce^-Ozqsg zUqChEh*26va75W$ANrP&ALIYJ@>g@~&u=*TV(|EHs>i2KUVrF77sg<}k%KWPSRW4& zw6VMh4vdlc2s{BL5kzb;92c#(1|FCm<=X~*MRGS-cN8JYut8}haO_X1_|7u;7~mQl zUI_tEs-AEe{M&$QaCl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2*Pp7nzb4K2pl+exqY7B^ZsAn=Zp6r3iJ3nz6cneKlAzd`bZr{!+T1loKNPno)2U< zUxX;MUTJQJD_|bm_}qXPx37hir>YEJQ*P&TDYZ}G4$l1h97GkcuPAQKV@G+^XNW4S wpaJtqfN@^T{pasrd>-Y#E2jF4(7-$3shr#8SiIdz@xOyVY#`Mhg2xp903AT~bN~PV literal 0 HcmV?d00001 diff --git a/sandbox/ostep/ex5_1.c b/ostep/ex5_1.c similarity index 65% rename from sandbox/ostep/ex5_1.c rename to ostep/ex5_1.c index 4947639..047e18e 100644 --- a/sandbox/ostep/ex5_1.c +++ b/ostep/ex5_1.c @@ -6,19 +6,21 @@ int main(int argc, char *argv[]){ printf("hello world (pid:%d)\n", (int) getpid()); - + + int x = 100; + int rc = fork(); if (rc < 0) { fprintf(stderr, "fork failed\n"); exit(1); } else if (rc == 0) { - printf("hello, I am child (pid:%d)\n", (int) getpid()); + printf("hello, I am child (pid:%d) // x=%d\n", (int) getpid(), x); sleep(1); } else { int wc = wait(NULL); - printf("hello, I am parent of %d (wc:%d) (pid:%d)\n", - rc, wc, (int) getpid()); + printf("hello, I am parent of %d (wc:%d) (pid:%d) // x=%d\n", + rc, wc, (int) getpid(), x); } return 0; From ab285372661bc3276302ebbb2ee1e4496243de6d Mon Sep 17 00:00:00 2001 From: linomp-droplet Date: Fri, 23 Aug 2024 16:29:53 +0000 Subject: [PATCH 3/7] do some exercises --- ostep/Makefile | 2 +- ostep/ex5_1 | Bin 16256 -> 16128 bytes ostep/ex5_1.c | 48 ++++++++++++++++++++++++++++++--------------- ostep/ex5_2 | Bin 0 -> 16216 bytes ostep/ex5_2.c | 47 ++++++++++++++++++++++++++++++++++++++++++++ ostep/ex5_2.output | 11 +++++++++++ 6 files changed, 91 insertions(+), 17 deletions(-) create mode 100755 ostep/ex5_2 create mode 100644 ostep/ex5_2.c create mode 100755 ostep/ex5_2.output diff --git a/ostep/Makefile b/ostep/Makefile index 6d01a79..a8b6d0e 100644 --- a/ostep/Makefile +++ b/ostep/Makefile @@ -10,7 +10,7 @@ TARGETS=$(SRCS:.c=) all: $(TARGETS) clean: - rm -f $(TARGETS) + rm -f $(TARGETS) & rm -f *.output pristine: clean all diff --git a/ostep/ex5_1 b/ostep/ex5_1 index 3b572349190354579e48c8223dd094b64fdbdcb2..2ee6ffd8d8a2320418db6542b9762149ed4d9af8 100755 GIT binary patch delta 2191 zcmZ8iZ%k8H6u-BBXiH$chv1`V?5i6Sw$PR$|EaCh#_F8WL^EA9r3ef*5GN&)O{Ijn z*f5gfNepf=DW8N-n=%)(#8_tr`e9SDeUM}yV9AnZm9Obd2JXT;kS8NBHvAe(~KWVGHcy%KG$xo+- zf4epHxqr`h@Tsv>%|=a@S(c_fpk0}dt(n#zy5NdoJtRFQST+{WX4q#^Xgw?$6?%r* zpqh#xP;Y|$rUF`}`916ki^d}G)SBQQQvsBkrLwL_XJ_|O(%aM7+IpOX!<`+?N5ehQ z$cbpUE7Ea{v~{0&pPY27C8^PT-x$moy3z)xw zyv?%4C2C;RDM`t?k>oWJ{uuXL1NuJqLw8otZsKPdEc$iAij)Q4XDKur$}NiBsJpf6 zbsobyzzjz$3LLiNBFx7=zc8lrjEF%3L9=pj}Hl0LkF!+!To_?5pW_n@wba0VH ztk!7MMXtRl^6E2@!KdK zB`w+Hc;rOuv8Z5lov~pv9yT+D#Z0Chp$Rd8cpPyYF@?AnabhWxNg)!rp}tJpVO~|t ziAR}C6FfzJ2b@mDl|s&9#afTXr}#gQ8dJ;}d`bGmuq`*sa3jZJ#isf!$1eI@^fjvz zTU%kQ+#x6Ob!OLrjKWO0gC=3A-0hs>?XHgjLLTte7Ng)YY<0QiQQpmbw2(d+a5u7d!7Z2HndDyVi*A;H$PFJyjl)A;L_5nW7pV? z={+WGp2L`pSl~7GznPdJ=lMwGnNXOiRA@fCPKGv@QW&f{SgH^2z>eKJ-`E!Ry1cF` z5^g=#f@8e7r$?XSgiUcMsNMu`Rd37E$Nc1l>UMgyR;Bb6aM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w+;lWhtbHb9x{upyq+y=mUVw94ReluO_NCFRsWshj5PdRiK&69BBN$C9&t9UnUf}~MJ|p+Dvq=Y z##32EjE7*jcq1ZPD{>qxWg>r* zCA%WCz*8o!QWBJC;q9^#UddubPQxs^D!H!6VX%~UaGfwzF6z&)9XbOu<)!=xvlQ6` zmI^WU|Cqqi`3LY+h #include -#include #include +#include -int main(int argc, char *argv[]){ - - printf("hello world (pid:%d)\n", (int) getpid()); - - int x = 100; +int main(int argc, char* argv[]) { + int* x = (int*)malloc(sizeof(int)); int rc = fork(); - if (rc < 0) { - fprintf(stderr, "fork failed\n"); - exit(1); - } else if (rc == 0) { - printf("hello, I am child (pid:%d) // x=%d\n", (int) getpid(), x); - sleep(1); + if (rc == 0) { + for (int i = 0; i <= 10; i++) { + printf("child | x=%d\n", (*x)++); + usleep(1); + } + } else { - int wc = wait(NULL); - printf("hello, I am parent of %d (wc:%d) (pid:%d) // x=%d\n", - rc, wc, (int) getpid(), x); + for (int i = 0; i <= 10; i++) { + printf("parent| x=%d\n", (*x)++); + usleep(1); + } + + wait(NULL); } return 0; -} \ No newline at end of file +} + +/* +Q: 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? What happens to the +variable when both the child and parent change the value of x? + +A: +No race condition happens because they don't share memory actually. +Child process gets its own "private memory" (copy of parent's address space) +Therefore parent & child modify their own copy of x, indepedently. + +Things would break if they were using shared memory. + +note: Even if x is a pointer, nothing changes! +*/ \ No newline at end of file diff --git a/ostep/ex5_2 b/ostep/ex5_2 new file mode 100755 index 0000000000000000000000000000000000000000..9bf065114c7e0e83a6f978b062289fbefa5cc5d7 GIT binary patch literal 16216 zcmeHOYit}>6~4R9!+F|Clh#Q{Go)?QQh8!0P6$CJeyu$&b`lat2m~F*-m$%OA7*#9 zv8yIHxKLYZkShe_2R~Y=B0(ULA}Tc%ZJdTCEzl|vs+tNA1|^cMq|`AFnWAPm=iYP1 zz3EGk13G?A-h1S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ar{jY+pi<_% z!{exwUnc!uN#^6A@c%L3_1(dMRrF9>N4S^p3xs`ygM_1m!8jiz^SaHJ#>So6y$3q; zX(z8$SL-!;Rb_Qviq(g=Y|*P~?j!miN5nkN6F1zT6`rREXr7=%YGi=JxR5=b$0)2+ z-An6D$y{i}4+i+H#PfB;_N`Efh_#};PBDC*aUE<#+%EXKP+IsM11i`quYYBJSlX}l z*2yw|0xE_2U+lPkDC4XWLgy%+dGadrYeZ_oBkvN|%!Y3(yus;L9 zYaG;v!@#eIt`R)GS@lii@d(Ii9x0vyz8uC$MIBoz%mTj>G#ZV!O{D%F+4Hz!ujj~q z5hY3f)&$BIB+pi}_;cW~pTRh90k6%Pd9@ow;zZ$#b)#oBTUihM$|&4L0^bjJAwO&8 z)oP0HVt-fGsn#>FK;7z0n2yyWfXF%d?ryzH%r0g(osj! zW*3+>@2hW*nX%nXCaj`w`poY7_SQzAVSj}gGe~PR?Gt88+n$E{HgivN^Zr3Gen=z(;3Isd(wHmFAIIoIs-ncGY>ya zS0>^_)i<|~zV->X& z>_2zl7*sH)?9cOV#xXMF_JhZd`?t`)f$Vr5&v?@3k2wqaKMfq_gyVbnExLd4^9_ZO zZE+p(JMi)QYvji`h(^A`w?)Pip!54bMG8i?;}9$}o(lM%A_Zd~I})-Vmd-aRE5{{Y4#_Rw?9m zH~{;xjL!qear-i{j^DJ70!~rKct0!mhrfRp iffM)L?_Wg&w +#include +#include +#include +#include +#include +#include + +int main(int argc, char* argv[]) { + int fd = open("./ex5_2.output", O_CREAT | O_WRONLY | O_TRUNC, S_IRWXU); + int rc = fork(); + + if (rc == 0) { + char* msg = "msg from child\n"; + + for (int i = 0; i < 5; i++) { + write(fd, msg, strlen(msg)); + usleep(100); + } + + } else { + char* msg = "msg from parent\n"; + write(fd, msg, strlen(msg)); + + for (int i = 0; i < 5; i++) { + write(fd, msg, strlen(msg)); + usleep(200); + } + + wait(NULL); + close(fd); + } + + return 0; +} + +/* +Q: Write a program that opens a file (with the open() system call) +and then calls fork() to create a new process. Can both the child +and parent access the file descriptor returned by open()? What +happens when they are writing to the file concurrently, i.e., at the +same time? + +A: the order in which the content will be written to the file is +undeterministic. I needed to introduce loops & delays to force the race +condition to happen, but it was clear +*/ \ No newline at end of file diff --git a/ostep/ex5_2.output b/ostep/ex5_2.output new file mode 100755 index 0000000..3e61eef --- /dev/null +++ b/ostep/ex5_2.output @@ -0,0 +1,11 @@ +msg from parent +msg from parent +msg from child +msg from parent +msg from child +msg from parent +msg from child +msg from parent +msg from child +msg from parent +msg from child From f128d77a4eeac6049b2f277799c875ba8d2b0b49 Mon Sep 17 00:00:00 2001 From: linomp-droplet Date: Fri, 23 Aug 2024 16:30:08 +0000 Subject: [PATCH 4/7] do some exercises --- ostep/ex5_1 | Bin 16128 -> 0 bytes ostep/ex5_2 | Bin 16216 -> 0 bytes ostep/ex5_2.output | 11 ----------- 3 files changed, 11 deletions(-) delete mode 100755 ostep/ex5_1 delete mode 100755 ostep/ex5_2 delete mode 100755 ostep/ex5_2.output diff --git a/ostep/ex5_1 b/ostep/ex5_1 deleted file mode 100755 index 2ee6ffd8d8a2320418db6542b9762149ed4d9af8..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16128 zcmeHOYit}>6~4R9!%36aNt-qgNET?Kl*$u3u|o(lS;yXFT$~3o(g8T^1tDWWUqmcUt^_lhPg# z`Q)7Nu~qUJ#9GpE0S5hg#4m^~uA2gK8(a=tx0COuy?x>ri#cIb5bIr90ny&4$&d5$ z?-pF<{nFlS*P%!950vz(jp;G3REN!!t$OpRtlg#9fG_(NwX3alN3*d>+oUyk_09FlXg#>wNLqQTJ5{i){K4JL z>0H)2Xm+G6ir<~dWhK7hnwB-BZb#{wR(MAR)%n^-h;JgqcrLC=E#Oh{DdB$d^>5NJ zh^o(!%mw!ue7{GRy8J1*kANQ#zB2CW!9%{f-Q{_Vac)t@e7t{Lj{A5X$0(CN-W{*5 zX4=Q|ct$xRJm1FgU1HtKKHj;Hi^H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RrF9>N4S^p3xs`ygM_1m!8jiz^SaHJ#>So6y$3q; zX(z8$SL-!;Rb_Qviq(g=Y|*P~?j!miN5nkN6F1zT6`rREXr7=%YGi=JxR5=b$0)2+ z-An6D$y{i}4+i+H#PfB;_N`Efh_#};PBDC*aUE<#+%EXKP+IsM11i`quYYBJSlX}l z*2yw|0xE_2U+lPkDC4XWLgy%+dGadrYeZ_oBkvN|%!Y3(yus;L9 zYaG;v!@#eIt`R)GS@lii@d(Ii9x0vyz8uC$MIBoz%mTj>G#ZV!O{D%F+4Hz!ujj~q z5hY3f)&$BIB+pi}_;cW~pTRh90k6%Pd9@ow;zZ$#b)#oBTUihM$|&4L0^bjJAwO&8 z)oP0HVt-fGsn#>FK;7z0n2yyWfXF%d?ryzH%r0g(osj! zW*3+>@2hW*nX%nXCaj`w`poY7_SQzAVSj}gGe~PR?Gt88+n$E{HgivN^Zr3Gen=z(;3Isd(wHmFAIIoIs-ncGY>ya zS0>^_)i<|~zV->X& z>_2zl7*sH)?9cOV#xXMF_JhZd`?t`)f$Vr5&v?@3k2wqaKMfq_gyVbnExLd4^9_ZO zZE+p(JMi)QYvji`h(^A`w?)Pip!54bMG8i?;}9$}o(lM%A_Zd~I})-Vmd-aRE5{{Y4#_Rw?9m zH~{;xjL!qear-i{j^DJ70!~rKct0!mhrfRp iffM)L?_Wg&w Date: Fri, 23 Aug 2024 16:51:51 +0000 Subject: [PATCH 5/7] ex 3 --- ostep/Makefile | 2 +- ostep/ex5_1 | Bin 0 -> 16128 bytes ostep/ex5_2 | Bin 0 -> 16216 bytes ostep/ex5_3 | Bin 0 -> 16400 bytes ostep/ex5_3.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 43 insertions(+), 1 deletion(-) create mode 100755 ostep/ex5_1 create mode 100755 ostep/ex5_2 create mode 100755 ostep/ex5_3 create mode 100644 ostep/ex5_3.c diff --git a/ostep/Makefile b/ostep/Makefile index a8b6d0e..c94cb88 100644 --- a/ostep/Makefile +++ b/ostep/Makefile @@ -1,5 +1,5 @@ CC=gcc -CCOPTS=#-Wall -Wextra +CCOPTS=-Wall -Wextra LIBS= SRCS=$(wildcard *.c) diff --git a/ostep/ex5_1 b/ostep/ex5_1 new file mode 100755 index 0000000000000000000000000000000000000000..2ee6ffd8d8a2320418db6542b9762149ed4d9af8 GIT binary patch literal 16128 zcmeHOYit}>6~4R9!%36aNt-qgNET?Kl*$u3u|o(lS;yXFT$~3o(g8T^1tDWWUqmcUt^_lhPg# z`Q)7Nu~qUJ#9GpE0S5hg#4m^~uA2gK8(a=tx0COuy?x>ri#cIb5bIr90ny&4$&d5$ z?-pF<{nFlS*P%!950vz(jp;G3REN!!t$OpRtlg#9fG_(NwX3alN3*d>+oUyk_09FlXg#>wNLqQTJ5{i){K4JL z>0H)2Xm+G6ir<~dWhK7hnwB-BZb#{wR(MAR)%n^-h;JgqcrLC=E#Oh{DdB$d^>5NJ zh^o(!%mw!ue7{GRy8J1*kANQ#zB2CW!9%{f-Q{_Vac)t@e7t{Lj{A5X$0(CN-W{*5 zX4=Q|ct$xRJm1FgU1HtKKHj;Hi^Hgoms`K1dCkY;+*onh$5SH15@sOGK$w9r17QZj z41^i@zstbitJeHOAG}$qpO|}Xqf+`)qjt1(Ss(mm<#{I;rG_I!PnFhvnV@Q|V*4J} zOijFADwReYnJ0)-6IVQ$Cx%lK=RBDwa#IsO@?@SMPE9-qnWAT$=q<{LPxA7Byf`4w z4ao0k+`D`JZ~EoBzqSrt`Mf^#Cw=hGllu-P>PG7>>BEh`qF$7i|BftgcWG5?zeXY@ zV7T!uV)Ws;Qw$%UwwF_Ce#&)9&4SWc)!G5JJ1w}FQT-ArJ+l%xQja?*+U%Oi>jVCEt z7v?@ea^wl}_1b6uiNmDiN;~<{#jG7V_D6lFc=;^ndHm<(t`8-q$FERjC~~N_Atiej+h_LJjK?{Y3lp(88e$bm})2a~CeY%ogKcAh)x8g~v}(Z7^{&GI;Ff z!s8cjv|UV0vPJnAFZpAv_hLNm#F(T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xVK& z`%knOhYIGD{f|>aj8kOD?fdutyX3!H!-uTdD; zSFR(TfX(Z#kssrv*BI)gcJVo%|9LWCWP1+7GUKGr{}L$}PqHH+`!T-k^XKsiV-Ft~ zpFf9v)#uOSKSq8YW_$njqsGtIkH65!lJFQi68imr0X*iPyLn7ghK$WhbbtN+zXgrg ze@auPj6ZZeRfW$VrmsL{32glQ$FW$NzcYaR1q#-veYep*5_3Q{euxKjRJH?DZF6GHq1I?Qj70 zV|fucBggIYxVD!3{r1?Fw=AXh$=p@3|3;Tl754WcpF3XDFa_+S6J`x4@fnc&!}s4C if#be={U_+e`@SzYx68KpI9$qq%e~5CmCwN^ihlx2`J2lC literal 0 HcmV?d00001 diff --git a/ostep/ex5_3.c b/ostep/ex5_3.c new file mode 100644 index 0000000..fc61065 --- /dev/null +++ b/ostep/ex5_3.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include +#include + +int main() { + int pipe_fds[2]; + assert(pipe(pipe_fds) >= 0); + + int pipe_read_fd = pipe_fds[0]; + int pipe_write_fd = pipe_fds[1]; + + int rc = fork(); + assert(rc >= 0); + + if (rc == 0) { + sleep(2); // sleep to prove that nothing happens in parent until child + // writes to pipe + char* msg = "msg from child!"; + write(pipe_write_fd, msg, strlen(msg)); + } else { + char buffer[200]; + printf("parent blocked on read() ...\n"); + read(pipe_read_fd, buffer, 200); + printf("parent received: “%s”\n", buffer); + printf("parent terminating\n"); + } + + return 0; +} + +/* +Q: Write another program using fork(). The child process should +print “hello”; the parent process should print “goodbye”. You should +try to ensure that the child process always prints first; can you do +this without calling wait() in the parent? + +A: can be achieved using a pipe. parent blocks on read() until child writes to +it. +*/ \ No newline at end of file From bc490b51d311a3dfd0fda62f4a95f862bd3eaa94 Mon Sep 17 00:00:00 2001 From: linomp-droplet Date: Fri, 23 Aug 2024 16:52:24 +0000 Subject: [PATCH 6/7] cleanup --- ostep/ex5_1 | Bin 16128 -> 0 bytes ostep/ex5_2 | Bin 16216 -> 0 bytes ostep/ex5_3 | Bin 16400 -> 0 bytes 3 files changed, 0 insertions(+), 0 deletions(-) delete mode 100755 ostep/ex5_1 delete mode 100755 ostep/ex5_2 delete mode 100755 ostep/ex5_3 diff --git a/ostep/ex5_1 b/ostep/ex5_1 deleted file mode 100755 index 2ee6ffd8d8a2320418db6542b9762149ed4d9af8..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16128 zcmeHOYit}>6~4R9!%36aNt-qgNET?Kl*$u3u|o(lS;yXFT$~3o(g8T^1tDWWUqmcUt^_lhPg# z`Q)7Nu~qUJ#9GpE0S5hg#4m^~uA2gK8(a=tx0COuy?x>ri#cIb5bIr90ny&4$&d5$ z?-pF<{nFlS*P%!950vz(jp;G3REN!!t$OpRtlg#9fG_(NwX3alN3*d>+oUyk_09FlXg#>wNLqQTJ5{i){K4JL z>0H)2Xm+G6ir<~dWhK7hnwB-BZb#{wR(MAR)%n^-h;JgqcrLC=E#Oh{DdB$d^>5NJ zh^o(!%mw!ue7{GRy8J1*kANQ#zB2CW!9%{f-Q{_Vac)t@e7t{Lj{A5X$0(CN-W{*5 zX4=Q|ct$xRJm1FgU1HtKKHj;Hi^Hgoms`K1dCkY;+*onh$5SH15@sOGK$w9r17QZj z41^i@zstbitJeHOAG}$qpO|}Xqf+`)qjt1(Ss(mm<#{I;rG_I!PnFhvnV@Q|V*4J} zOijFADwReYnJ0)-6IVQ$Cx%lK=RBDwa#IsO@?@SMPE9-qnWAT$=q<{LPxA7Byf`4w z4ao0k+`D`JZ~EoBzqSrt`Mf^#Cw=hGllu-P>PG7>>BEh`qF$7i|BftgcWG5?zeXY@ zV7T!uV)Ws;Qw$%UwwF_Ce#&)9&4SWc)!G5JJ1w}FQT-ArJ+l%xQja?*+U%Oi>jVCEt z7v?@ea^wl}_1b6uiNmDiN;~<{#jG7V_D6lFc=;^ndHm<(t`8-q$FERjC~~N_Atiej+h_LJjK?{Y3lp(88e$bm})2a~CeY%ogKcAh)x8g~v}(Z7^{&GI;Ff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{T1c`}4S(Q6o$4Kie@r33?o}v&`dm#xVK& z`%knOhYIGD{f|>aj8kOD?fdutyX3!H!-uTdD; zSFR(TfX(Z#kssrv*BI)gcJVo%|9LWCWP1+7GUKGr{}L$}PqHH+`!T-k^XKsiV-Ft~ zpFf9v)#uOSKSq8YW_$njqsGtIkH65!lJFQi68imr0X*iPyLn7ghK$WhbbtN+zXgrg ze@auPj6ZZeRfW$VrmsL{32glQ$FW$NzcYaR1q#-veYep*5_3Q{euxKjRJH?DZF6GHq1I?Qj70 zV|fucBggIYxVD!3{r1?Fw=AXh$=p@3|3;Tl754WcpF3XDFa_+S6J`x4@fnc&!}s4C if#be={U_+e`@SzYx68KpI9$qq%e~5CmCwN^ihlx2`J2lC From cc6bf23a30c84dc485c99879efbacaea79850094 Mon Sep 17 00:00:00 2001 From: linomp-droplet Date: Fri, 23 Aug 2024 17:32:34 +0000 Subject: [PATCH 7/7] solve ex 5.8 --- .vscode/settings.json | 6 ++++ ostep/Makefile | 2 +- ostep/ex5_8.c | 74 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 .vscode/settings.json create mode 100644 ostep/ex5_8.c diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..982432c --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "files.associations": { + "*html": "html", + "unistd.h": "c" + } +} \ No newline at end of file diff --git a/ostep/Makefile b/ostep/Makefile index c94cb88..a8b6d0e 100644 --- a/ostep/Makefile +++ b/ostep/Makefile @@ -1,5 +1,5 @@ CC=gcc -CCOPTS=-Wall -Wextra +CCOPTS=#-Wall -Wextra LIBS= SRCS=$(wildcard *.c) diff --git a/ostep/ex5_8.c b/ostep/ex5_8.c new file mode 100644 index 0000000..698685a --- /dev/null +++ b/ostep/ex5_8.c @@ -0,0 +1,74 @@ +#include +#include +#include +#include +#include +#include + +int main() { + int status; + + int pipe_fds[2]; + assert(pipe(pipe_fds) >= 0); + + int pipe_read_fd = pipe_fds[0]; + int pipe_write_fd = pipe_fds[1]; + + pid_t rc1 = fork(); + assert(rc1 >= 0); + + if (rc1 == 0) { + char* out_buffer = "msg from child 1!"; + + printf("child 1 is writing...\n"); + write(pipe_write_fd, out_buffer, strlen(out_buffer)); + + close(pipe_write_fd); + printf("child 1 terminating...\n"); + + exit(0); + } + + pid_t rc2 = fork(); + assert(rc2 >= 0); + + if (rc2 == 0) { + printf("child 2 blocked on read()...\n"); + + char in_buffer[200]; + read(pipe_read_fd, in_buffer, 200); + printf("child 2 finished reading...\n"); + + char out_buffer[400]; + sprintf(out_buffer, "%s || msg from child 2!", in_buffer); + + printf("child 2 is writing...\n"); + write(pipe_write_fd, out_buffer, strlen(out_buffer)); + + close(pipe_write_fd); + printf("child 2 terminating...\n"); + + exit(0); + } + + // Parent process + + printf("parent waiting for child 1 (pid: %d) ...\n", rc1); + waitpid(rc1, &status, 0); + printf("parent waiting for child 2 (pid: %d) ...\n", rc1); + waitpid(rc2, &status, 0); + + char buffer[200]; + read(pipe_read_fd, buffer, 200); + printf("parent received: “%s”\n", buffer); + + close(pipe_write_fd); + printf("parent terminating...\n"); + + return 0; +} + +/* +Q: Write a program that creates two children, and connects the standard output +of one to the standard input of the other, using the pipe() system call. +*/