From b5ebe8f76e1744911c8a03c1a3e237d16a27fb04 Mon Sep 17 00:00:00 2001 From: zeus Date: Wed, 7 Apr 2021 16:17:08 +0300 Subject: [PATCH] tar --- .../sec/project/courses/pthread/Example_cond1 | Bin 0 -> 8904 bytes .../project/courses/pthread/Example_cond1.c | 69 ++++++++++++++++++ .../courses/pthread/Example_cond1.c.sh | 1 + .../courses/pthread/Example_deadlock1.c | 42 +++++++++++ .../sec/project/courses/pthread/Example_join1 | Bin 0 -> 8728 bytes .../project/courses/pthread/Example_join1.c | 40 ++++++++++ .../courses/pthread/Example_join1.c.sh | 1 + .../sec/project/courses/pthread/Example_sync1 | Bin 0 -> 8664 bytes .../project/courses/pthread/Example_sync1.c | 44 +++++++++++ .../courses/pthread/Example_sync1.c.sh | 1 + 10 files changed, 198 insertions(+) create mode 100755 install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_cond1 create mode 100644 install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_cond1.c create mode 100755 install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_cond1.c.sh create mode 100644 install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_deadlock1.c create mode 100755 install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_join1 create mode 100644 install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_join1.c create mode 100755 install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_join1.c.sh create mode 100755 install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_sync1 create mode 100644 install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_sync1.c create mode 100755 install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_sync1.c.sh diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_cond1 b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_cond1 new file mode 100755 index 0000000000000000000000000000000000000000..5e83bb7da9fa35eaf96de9b43436ff3855e33d53 GIT binary patch literal 8904 zcmd5>eT-aH6~D8yU!~jGZE2xj%Cl0_f;;Rk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ition_cond = PTHREAD_COND_INITIALIZER; + +void *functionCount1(); +void *functionCount2(); +int count = 0; +#define COUNT_DONE 10 +#define COUNT_HALT1 3 +#define COUNT_HALT2 6 + +int main() +{ + pthread_t thread1, thread2; + + pthread_create( &thread1, NULL, &functionCount1, NULL); + pthread_create( &thread2, NULL, &functionCount2, NULL); + pthread_join( thread1, NULL); + pthread_join( thread2, NULL); + + exit(0); +} + +void *functionCount1() +{ + for(;;) + { + pthread_mutex_lock( &condition_mutex ); + while( count >= COUNT_HALT1 && count <= COUNT_HALT2 ) + { + pthread_cond_wait( &condition_cond, &condition_mutex ); + } + pthread_mutex_unlock( &condition_mutex ); + + pthread_mutex_lock( &count_mutex ); + count++; + printf("Counter value functionCount1: %d\n",count); + pthread_mutex_unlock( &count_mutex ); + + if(count >= COUNT_DONE) return(NULL); + } +} + +void *functionCount2() +{ + for(;;) + { + pthread_mutex_lock( &condition_mutex ); + if( count < COUNT_HALT1 || count > COUNT_HALT2 ) + { + pthread_cond_signal( &condition_cond ); + } + pthread_mutex_unlock( &condition_mutex ); + + pthread_mutex_lock( &count_mutex ); + count++; + printf("Counter value functionCount2: %d\n",count); + pthread_mutex_unlock( &count_mutex ); + + if(count >= COUNT_DONE) return(NULL); + } + +} diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_cond1.c.sh b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_cond1.c.sh new file mode 100755 index 0000000..55dd026 --- /dev/null +++ b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_cond1.c.sh @@ -0,0 +1 @@ +gcc -pthread -o Example_cond1 Example_cond1.c diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_deadlock1.c b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_deadlock1.c new file mode 100644 index 0000000..0c226d0 --- /dev/null +++ b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_deadlock1.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include + +void *functionC(); +pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; +int counter = 0; + + void *function1() + { + //... + pthread_mutex_lock(&lock1); // - Execution step 1 + pthread_mutex_lock(&lock2); // - Execution step 3 DEADLOCK!!! + //... + //... + pthread_mutex_lock(&lock2); + pthread_mutex_lock(&lock1); + ... + } + + void *function2() + { + //... + pthread_mutex_lock(&lock2); // - Execution step 2 + pthread_mutex_lock(&lock1); + //... + //... + pthread_mutex_lock(&lock1); + pthread_mutex_lock(&lock2); + //... + } + + int main() + { + //... + pthread_create(&thread1, NULL, function1, NULL); + pthread_create(&thread2, NULL, function1, NULL); + //... + } + diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_join1 b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_join1 new file mode 100755 index 0000000000000000000000000000000000000000..0c216b8636073d27f7b5d1092f837c32e0a064c2 GIT binary patch literal 8728 zcmeHMeQaCR6~B)2RcIX-+EUtXHHA*?M%@J35JUiX{umuXBVD(aN}E)e#t<;AFklHET2P_tSX=LR?mOq@ z`NbJ%+F#Rd!g0I$?EpM?2q;@&au7%b!?gu5i<|-TT(j}}XR3ao=!?H@`(B(ENRH={#$c|zi zn4?+Hkgf@YXFH)v^qETksBiS^V%$sHAfqCbbg688%Suqa@iNU$ zqoOZV?7jp$iu2Q!AS-U;_GTKF{T#ngc@L}8sqS@aR;LrI(y2^gVAVkLx>f7eg!9?( z4Z7dtpL92F-mY7s_bo$3G0($~Fp@tO{J8Fesz(#cYIj`t-fPcZ^WxW+yhpb3L-kXa zuR-&Q3fgmlg(~3R1y)J_Co|xuX23rH9>ULC?gdcE|FbjTsTuH9z(e?X%ToZBc|q_S z@vGFX!h7qtdUMHGf?igmGCR69nTcdBxjU7&lDV!;8`9ZKvMbh|PTI1H5_Vzs6|Cfd zna;-dD1qYeZ6zJ{o@^@PFt&OjqYV&AoMKp~OfzrA;(N?^?;f)!mP)JsTquoL*z-@sGx=|5Yacw4_p>k}{gi|h;i zTp8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6c zBJ?_&b)H`PHb~Cl&LZuPaz&`bFUMZq)*XMFQKKV=*dOJJQ2KsTp<^vo#a`}TJAlz$ zN$ll3E>s&qF2y^3cSAsTGRK}fDCc*%&r1BFCv*?&>5eD7obQEh<%TJK(vkQXR{Kz) zCdFRv0Yb-EQQ8-K>HkA)-^zM&j}>~@WxoJ*ul?h|XiVhVAomKPIFXbU9v%lPqUN?g z!;XXwI+}I`Clg(Seg~Y}eixsgg-X0^=EN`f4?Onr|74*L%D}P%X;=LH*kdnw4xxM; z5Ow{o90rx2vc1!c+;^BZx|LD)j^EEb_TK-;wta&Aa~^xSZwX!DneZ}h;`S6Oba$aO zL`D9OB>$)N>I*OQkFaULPweIXHpq!1(IekfgctfcFq$%AFZVq8Ke#LmnV(`O?~(2~ z)J4a>Ap{QpN2j1r25jOy#a`$;khtxg^EWpj?T7=h6MhmE!leBWpL2t3xK$LHhCKX7 z;Hmva4=(mAJvix7aZ_jR*&_XH;r7RP;OIL;XLX5R_79rh61z5(2QU0rJkF(EQJXu1 P{VRN>z9foFz4reC*Z)2j literal 0 HcmV?d00001 diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_join1.c b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_join1.c new file mode 100644 index 0000000..8ee2023 --- /dev/null +++ b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_join1.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include + +#define NTHREADS 10 +void *thread_function(void *); +pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; +int counter = 0; + +int main() +{ + pthread_t thread_id[NTHREADS]; + int i, j; + + for(i=0; i < NTHREADS; i++) + { + pthread_create( &thread_id[i], NULL, thread_function, NULL ); + } + + for(j=0; j < NTHREADS; j++) + { + pthread_join( thread_id[j], NULL); + } + + /* Now that all threads are complete I can print the final result. */ + /* Without the join I could be printing a value before all the threads */ + /* have been completed. */ + + printf("Final counter value: %d\n", counter); +} + +void *thread_function(void *dummyPtr) +{ + printf("Thread number %ld\n", pthread_self()); + pthread_mutex_lock( &mutex1 ); + counter++; + pthread_mutex_unlock( &mutex1 ); +} diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_join1.c.sh b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_join1.c.sh new file mode 100755 index 0000000..4d88c29 --- /dev/null +++ b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_join1.c.sh @@ -0,0 +1 @@ +gcc -pthread -o Example_join1 Example_join1.c diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_sync1 b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_sync1 new file mode 100755 index 0000000000000000000000000000000000000000..80d46ed635062dca8809df9ecfeda0c56576ec7f GIT binary patch literal 8664 zcmeHMdvIJ;89$qCn)J2VhPITKy;eG=NR|X>36E+v$tE{WTA-vTkM^?3ZkmP7Zrr_# z2{XlvDGX}}W*l@1gN_b9@E@l-OsxZIO0nn+5h2-2wtU4P%X-*oFR#qIjP%M;cRV`{2elJlM ztCgTv;Av&GAJ1}V!CJNp0?DtX$|3xCG201B4GF7RR|%TdIIN&jh9=05>N>Vgi$FtX z1gE$j3yk7NBKb|R-xT`^DzRsj_EFpD>1E!J9net{BwI>1y=o^Y|NJT~E)mDa?j@xxD52WsK`DDDUBlbgFOX)mNkwjp2_GwSFm0W z&ZxjY4z7~@(PjAGf;aH^)4d=n#Yrv0e|;Ig5xjxNpS}ZPtDl72g{M-v3h!sw9?T`< z33@3_%IfajZ6%Vq8CpERLPN)t%h@{j6I+%tA8YJ4Wu&hbm~4-!6Gv&Z^v`CH55mz zuAc6;cI(Q>m609L9vsSMSZh%?&{zuU5mbddjijLF0vI(DkZAlFcnTM$Fq4AnIpisk zJ^+;Q@93*sy{rc^j}>Pg;(0IpG_Q}fVS~bonG*TsI)|q@DdV2(W<9*jUE=3Fymx(D z@bJ7OkhwbR;a6+M`FVIiRweE3}rqrZC90M%<<=IPih_gly=lZRob=zzja$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7EXHr}+MUz63nI-__i2dT$P&t(s?N58SW}zXv?^k3atH;0=$zWa)dbRRj;dtJ}MD zy`_8HO8u$&b9UY?3=Bm2)#5FfWe-{XbPtxt6;mQ>9ZYBY;%O^kXLETgUKmmR*`eWd z(oQBK*X+EurLqxSVx_EjE*BrQk{LTUss?iLp`?{43=NIK#Uoj;w98CmBXKIh%8zFH zn^8B*>f9UM9kXJ4IxGuPnL|QZ9k=a??(S|c*XUa_P`FIBVkSp0JN7E8tLMhHXpePc zXXh=kUaL3S))S+s`mU`%U(i)|Z|$5Lz1G&Uo45aUZJ12N?KofPmAefTGW|Bru2fOq z#a_7~x*ai`n?2Z;UGqVg&su}=OhVsL-UzEiDq|J$$%I>H?@gmWvfE1PM7&CUa#q@- z8D;h5^IWdJZ1>8N_qY&N-TU zles)zQJG*tmrJJOO_Mmg14a}m~KOuLY{g4|7PjjgCzprekaL3R8Y=6tk$-YC;S8++A@g0 z>{kU1=)6hRRpFYE@%qcYR`60WWc2%YfIow?hLkV+UqPwAoX>b!cBAe=jz?+eO#mHap1@%ujlj`~El z_i|nr#EzmY`QLvLTCe{Y2NKkGYn}*U&I>;3^Otjmpww4Fh%ER`pTF!Y1ivgD>nm5{ zJ?Zn8`w2n+amC;MAF{s`Bj+2ztE3=CzyFVY{{H_{JKo2iE|;nQrF=Or2{!l!y!4xd z{S7j7_MkaLN%q(BKdawfWWnFTrw)(!%lYdezVHz{icLvm!M}i`AtU~>@0b6vrC~BY z#ZSf$ol~fZu78~Y3IBsrrI7sNPy7X6hmF_Y-9K{yQjQLWAMq1;fjKGP;C*VC9d8mt zM#Cq+4VlVs^6}!o-N%zHB`?amcQnp#E0_NicN~3ZXs<2xm-U0jx76M7bbjY(u|PWN X3ohk4TH3Q$_CLZ$;&Woi==c9Gr?wO= literal 0 HcmV?d00001 diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_sync1.c b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_sync1.c new file mode 100644 index 0000000..333bb1a --- /dev/null +++ b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_sync1.c @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include + +void *functionC(); +pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; +int counter = 0; + +int main() +{ + int rc1, rc2; + pthread_t thread1, thread2; + + /* Create independent threads each of which will execute functionC */ + + if( (rc1=pthread_create( &thread1, NULL, &functionC, NULL)) ) + { + printf("Thread creation failed: %d\n", rc1); + } + + if( (rc2=pthread_create( &thread2, NULL, &functionC, NULL)) ) + { + printf("Thread creation failed: %d\n", rc2); + } + + /* Wait till threads are complete before main continues. Unless we */ + /* wait we run the risk of executing an exit which will terminate */ + /* the process and all threads before the threads have completed. */ + + pthread_join( thread1, NULL); + pthread_join( thread2, NULL); + + exit(0); +} + +void *functionC() +{ + pthread_mutex_lock( &mutex1 ); + counter++; + printf("Counter value: %d\n",counter); + pthread_mutex_unlock( &mutex1 ); +} diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_sync1.c.sh b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_sync1.c.sh new file mode 100755 index 0000000..284bcbd --- /dev/null +++ b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_sync1.c.sh @@ -0,0 +1 @@ +gcc -pthread -o Example_sync1 Example_sync1.c