You are on page 1of 6

Nguyn Thnh Nguyn - 12520293

Bo Co v POSIX THREADS
1. pthello.c
2. /* pthello.c - a simple pthreads program */ 3. 4. #include<stdio.h> 5. #include<stdlib.h> 6. #include<pthread.h> 7. 8. void *thread_func(void *arg) 9. { 10. int myid = (int)arg; /* pass an int via the void * parameter */ 11. 12. printf("Hello from thread %d\n", myid); 13. 14. pthread_exit((void *)myid + 100); 15. } 16. 17. int main(int argc, char *argv[]) 18. { 19. pthread_t t1, t2; 20. int rv1, rv2; 21. 22. printf("Hello from main\n"); 23. 24. if (pthread_create(&t1, NULL, thread_func, (void *)1) != 0) { 25. printf("pthello: pthread_create error\n"); 26. exit(1); 27. } 28. if (pthread_create(&t2, NULL, thread_func, (void *)2) != 0) { 29. printf("pthello: pthread_create error\n"); 30. exit(1); 31. } 32. 33. pthread_join(t1, (void **)&rv1); 34. printf("Main joined with thread 1 retval = %d\n", rv1); 35. pthread_join(t2, (void **)&rv2); 36. printf("Main joined with thread 2 retval = %d\n", rv2); 37. }

Nguyn Thnh Nguyn - 12520293

Kt qu chy th: (vi trong s cc kt qu):

Nhn xt:
_Mi ln chy th t in c th c s khc nhau (nh hnh)

Gii thch:
_v thread t1 v thread t2 ch y song song, cng thc hin 1 chc nng ca hm con tr thread_func nn thread no chy trc(in Hello from thread), xong trc(in Main joined with thread retval) s in ra trc. _hm con tr thread_func thc hin chc nng in ra id ca thread, khi thread kt thc th id thread +100, do vy kt qu in ra ca thread 1 lun bng 101, thread 2 lun bng 102

2. ptmutex.c
/* ptmutex.c - using pthreads mutex variables for mutual exclusions */ #include<stdio.h> #include<stdlib.h> #include<pthread.h> /* global variables shared among threads */ pthread_mutex_t gmutex = PTHREAD_MUTEX_INITIALIZER; int x = 0; void *thread_func(void *arg) { int myid = (int)arg; /* pass an int via the void * parameter */ pthread_mutex_lock(&gmutex); x = x + myid; printf("Hello from thread %d x = %d\n", myid, x); pthread_mutex_unlock(&gmutex); pthread_exit((void *)myid + 100);

Nguyn Thnh Nguyn - 12520293

int main(int argc, char *argv[]) { pthread_t t1, t2; int rv1, rv2; printf("Hello from main x = %d\n", x);

/* pthread_mutex_init(&gmutex, NULL); */ if (pthread_create(&t1, printf("pthello: exit(1); } if (pthread_create(&t2, printf("pthello: exit(1); } NULL, thread_func, (void *)1) != 0) { pthread_create error\n");

NULL, thread_func, (void *)2) != 0) { pthread_create error\n");

pthread_join(t1, (void **)&rv1); printf("Main joined with thread 1 retval = %d pthread_join(t2, (void **)&rv2); printf("Main joined with thread 2 retval = %d }

x = %d\n", rv1, x); x = %d\n", rv2, x);

Kt qu chy th:

Nhn xt:
_2 ln chy th: + ln 1 thread 1 chy trc, kt qu in ra x=1, thread 2 chy sau, kt qu in ra x=3 + ln 2 thread 2 chy trc, kt qu in ra x=2, thread 1 chy sau, kt qu in ra x=3

Gii thch:
_Hm con tr thread_func thc hin chc nng x=x+id ( vi id l id thread), nu thread kt thc th id+100 _Nh vy nu thread 1 chy trc, x = x+id = 0+1=1, kt qu in ra Hello from thread 1 x=1, sau thread 2 chy, x=x+id=1+2=3, kt qu in ra Hello from thread 2 x=3.

Nguyn Thnh Nguyn - 12520293

Khi thread 1 kt thc th id+100, v sau khi pthread_join th tr v gi tr rv1( i vi thread1), rv2( i vi thread2) nn in ra Main joined.thread1 101 (thread2 102). _ y 2 thread khng chy song song m i ln lt v c s xut hin ca pthread_mutex_lock() v pthread_mutex_unlock(&gmutex), nhng li gi hm ny rt cn thit trong lp trnh vi thread. Chng cung cp cch thc loi tr ln nhau (mutual exclusion) ,c ngha l khi mt thread s dng ti nguyn th thread khc khng c s dng v phi ch cho n khi thread kia gii phng ti nguyn. Khng c hai thread no c th cng c mt mutex b kha mt thi im. Nu thread2 c gng kha mt mutex trong khi thread1 kha cng mutex ri th thread2 s ri vo trng thi sleep. Ngay khi thread1 gii phng mutex (thng qua pthread_mutex_unlock()), thread2 s c th kha mutex.

3. race.c
/* race.c - a simple pthreads program */ #include<stdio.h> #include<stdlib.h> #include<pthread.h> long long gx = 0; long long count = 999999; pthread_mutex_t gmutex = PTHREAD_MUTEX_INITIALIZER; void *thread_func(void *arg) { long long i; for (i = 0; i < count; i++) { /* remove lock and unlock to see race */ pthread_mutex_lock(&gmutex); gx += 1; pthread_mutex_unlock(&gmutex); } return (void *)0; } int main(int argc, char *argv[]) { pthread_t t1, t2; int rv1, rv2; printf("Starting race\n"); if (pthread_create(&t1, printf("pthello: exit(1); } if (pthread_create(&t2, printf("pthello: exit(1); } NULL, thread_func, (void *)1) != 0) { pthread_create error\n");

NULL, thread_func, (void *)2) != 0) { pthread_create error\n");

pthread_join(t1, (void **)&rv1);

Nguyn Thnh Nguyn - 12520293

pthread_join(t2, (void **)&rv2); printf("Expected value = %lld\n", count * 2); printf("Actual value = %lld\n", gx); return 0; }

Kt qu chy th:

Nhn xt:
_Kt qu thc t bng kt qu d kin.

Gii thch:
_V c s xut hin ca mutex lock v mutex unlock nn 2 thread chy ln lt,khi thread th nht chy xong, gx=999999, thread th hai chy s reset li i=0, gx tip tc tng ln, khi thread th hai chy xong, gx=1999998 .Khng th hin c race. Kt qu chy th(khi b cc hm mutex):

Nhn xt:
_Kt qu thc t khc vi kt qu d kin.

Gii thch:
_V 2 threads chy song song, cng s dng vng lp for(i=0;i<999999;i++), ua nhau thay i i cho n khi i<999999 th 2 thread cng exit, nn khi thot khi vng lp gx khng th t c kt qu nh d kin.

4. thex1.c
#include<stdio.h> #include<stdlib.h> #include<pthread.h> void *thread_func(void *arg) { int myid = (int)arg; pthread_exit((void *)myid + 100); } int main(int argc, char *argv[]) { pthread_t t1; int rv1;

Nguyn Thnh Nguyn - 12520293

if (pthread_create(&t1, NULL, thread_func, (void *)1) != 0) { exit(1); } pthread_join(t1, (void **)&rv1); return 0; }

Kt qu chy th: Nothing Nhn xt:


_v khng c in nn khng hin kt qu _y l 1 chng trnh create, join threads c bn

Gii thch:
_u chng trnh l hm con tr thread_func c chc nng ly a ch id ca thread +100 khi kt thc thread. _Vo trong hm main +khai bo tittle ca thread (pthread_t t1) +khai bo bin rv1 tr gi tr v khi join li +Tip theo l to 1 thread chy chc nng ca hm con tr thread_func +Sau khi chy xong th thu hi li b nh (v khi to 1 thread chng trnh b chia ra nn phi join li)

You might also like