You are on page 1of 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);
}
void*thread_function(void*arg)
{
while(print_count1++<20)
{
if(run_now==2)
{
Printf(“2”);
run_now=1;
}
Else
{
Sleep(1);
}
}
}
}
Program – 2
Program 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 1024
char 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);
}
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);
}
Program – 3
Program 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 1024
char 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);
}
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);
}
Program – 4
Implementation 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)],j
Begin
Spawn (p0, p1, p2, p└n/2-1┘)
For all Pi where 0<= i<= P[n/2]-1 do
For j←0 to ┌log n┐-1 do
If i module 2j =0 and 2i+2j<n then
A[2i]← A A[2i]+A[2i+2j]
Endif
Endfor
Endfor
end
Program – 5
Implementation 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)],j
Begin
Spawn (p1, p2,............pn-1)
For all Pi where 1<=i<= n-1 do
For j←0 to ┌log n┐-1 do
If i-2j >= 0 then
A[i]← A[i]+A[i-2j]
Endif
Endfor
Endfor
end
Program - 6
Implementation 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}
Begin
Spawn(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 then
if sibling[i] != null then
succ[(I,j)] ← (j,sibling[i])
else if parent[j] != null then
succ[(I,j)] ← (j, parent[j])
else
succ[(I,j)] ← (i,j)
preorder[j] ← 1{ j is the root of the tree}
endif
else
if child[j] != null then succ[(I,j)] ← (j,child[j])
else succ[(I,j)] ← (i,j)
endif
endif
{Number of the edges of the successor list}
If parent[i] = j then position[(i,j)] ← 0
Else position [(i,j)] ← 1
Endif
{Perform suffix sum on successor list}
For k ← 1 to ┌log(2(n-1)┐ do
Position[(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)]
Endif
Endfor
End
Program – 7
Implementation of Matrix Multiplication.

MATRIX 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}
b[0….(n-1)][0….(n-1)] {product matrix}
Local i,j,k {loop indices}
t {accumulates subtotal}
begin
for all Pm where 1 <= m <= p do
for i ← m to n step p do
for j ← I to n do
t←0
for k ← 1to n do
t ← t + a[i][k] * b[k][j]
endfor
c[i][j] ← t
endfor
endfor
endfor
end
Program – 8
MATRIX 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}
bigin
for j←0 to n-1 do
for j←I to n-1 do
t<-0
for k←t+a[i][k]*b[k][j]
endfor
c[i][j]←t
endfor endfor {after changimg access order}
for i←0 to n-1 do
for j←I to i+n-1 do
m←j modulo n
t←0
for k←0 to n-1 do
endfor
c[i][m]←t
endfor
endfor {after parallelization}
for all pi where 0<=I<=n do
for j←i to i+n-1 do
t←0
for k←0 to n-1 do
t←t+a[k]*b[k]
endfor
c[(j)modulo n]←t
b[0…..(n-1)]<=successor(b(0…..(n-1)))
endfor
endfor
end

You might also like