acapracticals

11
Program – 1 Program showing simultaneous execution of threads #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<pthread.h> void*thread_function(void*arg); char message()=”Hello World”; int main() { int res; int print_count1=0; pthread_t a_thread; void*thread result; res=pthread_create(&a_thread, NULL, thread_function,(void*)message); if(res!=0) { perror(“Thread Creation Failed”); exit(EXIT_FAILURE); } while(print_count1++<20) { if(run_nowc==1) { printf(“1”); run_now=2; } else { sleep(1); } printf(“Waiting for thread to finish\n”); res=pthread_join(a_thread,&thread_result); if(res!=0) { perror(“Thread join failed”); exit(EXIT_FAILURE); } printf(“Thread joined, it returned %s\n”,(char*)thread_result); printf(“Message is now %s\n”,message); exit(EXIT_SUCCESS); }

Upload: kshitij-kumar

Post on 28-Nov-2014

134 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ACAPracticals

Program – 1Program showing simultaneous execution of threads #include<stdio.h>#include<conio.h>#include<stdlib.h>#include<pthread.h>void*thread_function(void*arg);char message()=”Hello World”;int main(){int res;int print_count1=0;pthread_t a_thread;void*thread result;res=pthread_create(&a_thread, NULL, thread_function,(void*)message);if(res!=0){perror(“Thread Creation Failed”);exit(EXIT_FAILURE);} while(print_count1++<20){if(run_nowc==1){printf(“1”);run_now=2;}else{sleep(1);}printf(“Waiting for thread to finish\n”);res=pthread_join(a_thread,&thread_result);if(res!=0){perror(“Thread join failed”);exit(EXIT_FAILURE);}printf(“Thread joined, it returned %s\n”,(char*)thread_result);printf(“Message is now %s\n”,message);exit(EXIT_SUCCESS);}

Page 2: ACAPracticals

void*thread_function(void*arg){while(print_count1++<20){if(run_now==2){Printf(“2”);run_now=1;}Else{Sleep(1);}}}}

Page 3: ACAPracticals

Program – 2Program for synchronizing POSIX threads using semaphores #include<stdio.h>#include<unisted.h>#include<stdlib.h>#include<string.h>#include<pthread.h>#include<semaphore.h>void*thread_function(void*arg);sem_t bin_sem;#define WORK_SIZE 1024char work_area(WORK_SIZE);int main(){int res;pthread_t a_thread;void*thread_result;res=sem_init(&bin_sem,0,0);if(res!=0){perror(“Semaphore initialization failed.”);exit(EXIT_FAILURE);}res=pthread_create(&a_thread, NULL, thread_function, NULL);if(res!=0){perror(“Thread creation failed.”);exit(EXIT_FAILURE);}printf(“Input some text, enter ‘end’ to finish\n”);while(strncmp(“end”, work_area,3)!=0){fgets(work_area,WORK_SIZE,stdin);sem_post(&bin_sem);}printf(“Waiting for thread to finish\n”);res=pthread_join(a_thread,&thread_result);if(res!=0){perror(“Thread join failed.”)exit(EXIT_FAILURE);}

Page 4: ACAPracticals

printf(“Thread joined\n”);sem_destroy(&bin_sem);exit(EXIT_SUCCESS);}void*thread_function(void*arg){sem_wait(&bin_sem);while(strncmp(“end”,work_area,3)!=0){printf(“You input %d characters \n”, strlen(work_area)-1)sem_wait(&bin_sem);}pthread_exit(NULL);}

Page 5: ACAPracticals

Program – 3Program for synchronizing POSIX threads using mutex. #include<stdio.h>#include<unisted.h>#include<stdlib.h>#include<string.h>#include<pthread.h>#include<semaphore.h>void*thread_function(void*arg);pthread_mutex_t work_mutex;/*protects both work_area and time to exit*/#define WORK_SIZE 1024char work_area(WORK_SIZE);int time_to_exit=0;int main(){int res;pthread_t a_thread;void*thread_result;res=pthread_mutex_init(&work_mutex,NULL);if(res!=0){perror(“mutex initialization failed”);exit(EXIT_FAILURE);}res=pthread_create(&a_thread,NULL,, thread_function, NULL);if(res!=0){perror(“thread creation failed”);exit(EXIT_FAILURE);}pthread_mutex_lock(&work_mutex);printf(“input some text. enter ‘end’ to finish \n”);while(!time_to_exit){fgets(work_area WORK_SIZE, stdin);pthread_mutex_unlock(&work_mutex);while(1){pthread_mutex_lock(&work_mutex);if(work_area(0)!=’\0’){pthread_mutex_unlock(&work_mutex);sleep(1);

Page 6: ACAPracticals

}else{break;}}}pthread_mutex_unlock(&work_mutex);printf(“waiting for thread to finish \n”);res=pthread_join(a_thread, &thread_result);if(res!=0){perror(“thread join failed”);exit(EXIT_FAILURE);}printf(“thread joined \n”);pthread_mutex_destroy(&work_mutex);exit(EXIT_SUCCESS);}void*thread_function(void*arg){sleep(1);pthread_mutex_lock(&work_mutex);while(strncmp(*end*, work_area, 3)!=0){printf(“You input %d characters \n”, strlen(work_area)-1”);work_area[0]=‘\0’;pthread_mutex_unlock(&&work_mutex);sleep(1);pthread_mutex_unlock(&work_mutex);while(work_area[0]==’\0’){pthread_mutex_unlock(&work_mutex);sleep(1);thread_mutex_lock(&work_mutex);}}time_to_exit=’1’;work_area[0]=’\0’;pthread_mutex_unlock(&work_mutex);pthread_exit(0);}

Page 7: ACAPracticals

Program – 4Implementation of PRAM algorithm for parallel reduction. SUM(PRAM)Initial condition: List of n>=element sorted in A [0..........(n-1)]Final condition: Sum of elements stored in A[0]Global variables: n, A[0.........(n-1)],jBeginSpawn (p0, p1, p2, p└n/2-1┘)For all Pi where 0<= i<= P[n/2]-1 doFor j←0 to ┌log n┐-1 doIf i module 2j =0 and 2i+2j<n thenA[2i]← A A[2i]+A[2i+2j]EndifEndforEndforend

Page 8: ACAPracticals

Program – 5Implementation of PRAM algorithm for prefix sum. PREFIX.SUMS (PRAM)Intial condition : list of n>= element sorted in A [0.....(n-1)]Final condition : each element A[i] contain A[0] A[1] .......... A[i]Global variable : n, A[0........(n-1)],jBeginSpawn (p1, p2,............pn-1)For all Pi where 1<=i<= n-1 doFor j←0 to ┌log n┐-1 doIf i-2j >= 0 thenA[i]← A[i]+A[i-2j]EndifEndforEndforend

Page 9: ACAPracticals

Program - 6Implementation of PRAM algorithm for preorder tree traversal.PREORDER.TREE.TRAVERSAL(PRAM)Global n {number of vertices in tree}Parent[1….n] {Vertex number of parent node}Child[1….n] {Vertex number of first child}Sibling[1….n] {vertex no of sibling}Succ[1….(n-1)] {Index of successor edge}Position[1….(n-1)] {Edge rank}Preorder[1….n] {Preorder traversal number}BeginSpawn(set of all P(I,j)where(I,j) is an edge)For all P(I,j) where (I,j)is an edge do{Put the edge in the linked list}if parent[i] = j thenif sibling[i] != null thensucc[(I,j)] ← (j,sibling[i])else if parent[j] != null thensucc[(I,j)] ← (j, parent[j])elsesucc[(I,j)] ← (i,j)preorder[j] ← 1{ j is the root of the tree}endifelseif child[j] != null then succ[(I,j)] ← (j,child[j])else succ[(I,j)] ← (i,j)endifendif{Number of the edges of the successor list}If parent[i] = j then position[(i,j)] ← 0Else position [(i,j)] ← 1Endif{Perform suffix sum on successor list}For k ← 1 to ┌log(2(n-1)┐ doPosition[(i,j)] ← Position[(i,j)]+ Position[succ[(i,j)]]Succ[(i,j)] ← succ[succ[(i,j)]endfor{assign preorder values}If i = parent[j] then preorder[j] ← n+1 - Position[(i,j)]EndifEndforEnd

Program – 7

Page 10: ACAPracticals

Implementation of Matrix Multiplication. MATRIX MULTIPLICATIONGlobal n {Dimension of matrices}a[0….(n-1)][0….(n-1)] {first factor matrix}b[0….(n-1)][0….(n-1)] {second factor matrix}b[0….(n-1)][0….(n-1)] {product matrix}Local i,j,k {loop indices}t {accumulates subtotal}beginfor all Pm where 1 <= m <= p dofor i ← m to n step p dofor j ← I to n dot ← 0for k ← 1to n dot ← t + a[i][k] * b[k][j]endforc[i][j] ← tendforendforendforend

Page 11: ACAPracticals

Program – 8MATRIX MULTIPLICATION Global n {Dimension of matrices}a[0…..(n-1)][0……(n-1)] {first factor matrix}b[0…..(n-1)][0……(n-1)] {second factor matrix}c[0…..(n-1)][0……(n-1)] {product matrix}local I,j,k {loop indices}t {accumulates subtotal}biginfor j←0 to n-1 dofor j←I to n-1 dot<-0for k←t+a[i][k]*b[k][j]endforc[i][j]←tendfor endfor {after changimg access order}for i←0 to n-1 dofor j←I to i+n-1 dom←j modulo nt←0for k←0 to n-1 doendforc[i][m]←tendforendfor {after parallelization}for all pi where 0<=I<=n dofor j←i to i+n-1 dot←0for k←0 to n-1 dot←t+a[k]*b[k]endforc[(j)modulo n]←tb[0…..(n-1)]<=successor(b(0…..(n-1)))endforendforend