You are on page 1of 4

Lab 1 Posix Threads Programming

1 Mc tiu SV tm hiu v s dng th vin Posix Thread trn linux Vit mt chng trnh multithread n gin minh ha tnh hiu qu ca chng trnh khi c song song ha. Cc hm Posix threads c bn: o pthread_create(), pthread_attr_init(), pthread_attr_destroy(), pthread_exit(). o pthread_join(), pthread_detach(), pthread_attr_getdetachsate(), pthread_attr_setdetachstate(). o Pthread_mutex_init(), pthread_mutex_destroy(), pthread_mutexattr_init() , pthread_mutexattr_destroy(), pthread_mutex_lock(), pthread_mutex_unlock().

2 Ni dung 2.1 Yu cu SV vit mt chng trnh multithread thc thi ng thi 3 gii thut sp xp sau: Selection sort, Interchange sort, Bubble sort. Th t to thread thc thi mi gii thut l ty , SV thay i kch tht d liu u vo v quan st kt qu ca chng trnh. Nhn xt v kt qu chng trnh trong cc ln thay i kch tht d liu. 2.2 Hng dn Chng trnh mu cung cp ch mang tnh tham kho, SV c th t vit chng trnh hoc b sung, thay i bi mu hon tt yu cu ca bi lab. Chng trnh mu c cung cp di y hin thc 3 gii thut sp xp yu cu, SV cn tm hiu cc hm Posix threads hon tt hm main() trong chng trnh. /* Ma nguon */
#include <stdio.h> #include <stdlib.h> #include <pthread.h> #define NUM_THREADS 3 #define N 100 struct threadData { int tid; long num; long *a; }; struct threadData arrayData[NUM_THREADS];

void Swap(long *x, long *y){ long temp; temp = *x; *x = *y; *y = temp; } /* selection Sort */ void *selectionSort(void* data){ long i,j,temp,min; /* Thread Argument Passing */ struct threadData *myData; myData = (struct threadData *)data; int myid = myData->tid; long myNum = myData->num; long *myArray = myData->a; printf("\n Thread %d starts Selection Sort: \n",myid); /* Processing */ for(i=0; i < myNum-1; i++) { min = i; for(j=i+1; j < myNum; j++){ if(myArray[min] > myArray[j]) min = j; } Swap(&myArray[min],&myArray[i]); } printf("\n Result selection sort from thread %d : \n",myid); for(i=0; i < myNum; i++) printf("%5ld ", myArray[i]); pthread_exit(NULL);

/* interchange sort */ void *interchangeSort(void* data){ long i,j; /* Thread Argument Passing */ struct threadData *myData; myData = (struct threadData *)data; int myid = myData->tid; long myNum = myData->num; long *myArray = myData->a; printf("\n Thread %d starts Interchange Sort: \n",myid); /* Processing */ for(i=0; i < myNum-1; i++) { for(j=i+1; j < myNum; j++) if(myArray[i] > myArray[j]) Swap(&myArray[j],&myArray[i]); } printf("\n Result interchange sort from thread %d : \n",myid);

for(i=0; i < myNum; i++) printf("%5ld ", myArray[i]); pthread_exit(NULL);

/* bubble sort */ void *bubbleSort(void* data){ long i,j; /* Thread Argument Passing */ struct threadData *myData; myData = (struct threadData *)data; int myid = myData->tid; long myNum = myData->num; long *myArray = myData->a; printf("\n Thread %d starts Bubble Sort: \n",myid); /* Processing */ for(i=0; i < myNum-1; i++) { for(j= myNum-1; j > i; j--) if(myArray[j] < myArray[j-1]) Swap(&myArray[j],&myArray[j-1]); } printf("\n Result bubble sort from thread %d : \n",myid); for(i=0; i < myNum; i++) printf("%5ld ", myArray[i]); pthread_exit(NULL);

int main(int argc, char **argv){ pthread_t threads[NUM_THREADS]; int status,rc; long i,j,n,ret; /* Initialize Data */ long **a; (*a) = (long *)malloc(NUM_THREADS * sizeof(long)); for(i=0; i < NUM_THREADS; i++) a[i] = (long *)malloc(N * sizeof(long)); /* data for first row */ for(j=0; j < N; j++) a[0][j] = random()%1000; /* replicate to all rows */ for(i=1; i < NUM_THREADS; i++) { for(j=0; j < N; j++) a[i][j] = a[0][j]; } /* Mornitoring data */ printf("\n Data before sorting: \n"); for(j=0; j < N; j++){

printf("%5ld ", a[0][j]); } printf("\n \n"); for(j=0; j < N; j++){ printf("%5ld ", a[1][j]); } for(i=0; i < NUM_THREADS; i++){ printf("\n Creating thread %d: \n",i); arrayData[i].tid = i; arrayData[i].num = N; arrayData[i].a = a[i]; } /* SV to thread thc thi cc gii thut */ .................... /* SV gi hm ch cc thread kt thc */ .................... pthread_exit(NULL); return 0;

/* Ket thuc */ SV nhn xt v kt qu th nghim chng trnh ! 3 Bi tp SV s dng kin thc v lp trnh multithread vit cc chng trnh sau: 3.1 Vit chng trnh nhn hai vector 3.2 Vit chng trnh nhn hai ma trn
Hng dn: SV s dng hm pthread_mutex_lock() v pthread_mutex_unlock() trong trng hp ng b d liu dng chung gia cc thread.

3.3 Vit chng trnh chi c caro trn bn c N*N. xut m hnh multithread cho gii thut tm kim nc i tt nht v kho st tnh hiu qu, thi gian chy ca gii thut c song song ha bng multithread. 3.4 Vit chng trnh t N qun hu vo bn c tng qut N *N . xut m hnh multithread cho gii thut t hu v kho st tnh hiu qu, thi gian chy ca gii thut c song song ha bng multithread.

You might also like