0% found this document useful (0 votes)
103 views

ACA Practicals

The document contains 7 programs demonstrating the use of threads and synchronization in C programming. Program 1 shows matrix multiplication using nested for loops. Program 2 creates 5 threads that each print a message. Program 3 creates threads to update a text file. Program 4 uses semaphores to synchronize threads accessing a shared variable. Program 5 uses a mutex to synchronize addition to a shared sum. Program 6 demonstrates mutex locking around a counter increment. Program 7 is another thread synchronization example without details.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views

ACA Practicals

The document contains 7 programs demonstrating the use of threads and synchronization in C programming. Program 1 shows matrix multiplication using nested for loops. Program 2 creates 5 threads that each print a message. Program 3 creates threads to update a text file. Program 4 uses semaphores to synchronize threads accessing a shared variable. Program 5 uses a mutex to synchronize addition to a shared sum. Program 6 demonstrates mutex locking around a counter increment. Program 7 is another thread synchronization example without details.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 16

Program 1

Write a program in C for Matrix Multiplication.

# include<math.h>
# include<stdio.h>
int main()
{
int a[3][3],b[3][3],d[3][3];
int i,k,j,c=1;
printf("Enter the values for Matrix A");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Value %d------>\n"c);
scanf("%d",&a[i][j]);
c++;
}
}
c=1;
printf("Enter the values for Matrix B");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Value %d------>\n"c);
scanf("%d",&a[i][j]);
c++;
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
d[i][j]=0;
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
d[i][j]+=a[i][j]*b[k][j];
}
}
}
printf("Matrix D:");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d",&d[i][j]);
}
printf("\n");
}
return(0);
}
Program 2

Program for creating thread

#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5

void hello()
{
printf("Im a thread!\n");
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
pthread_t threads[NUM_THREADS]; //Declaring 5 threads.
int rc, t; //Cycling through threads[5] to pthread_create() each thread.
for(t=0; t < NUM_THREADS; t++)
{
printf("Initializing thread %d\n", t);
rc = pthread_create(&threads[t], NULL, (void *)hello, NULL); //creating each thread.
if (rc != 0)
{
//if pthread_create() doesn't return 0, then we have error.
printf("ERROR; return code from pthread_create() is %d\n", rc);
return -1;
}
}
pthread_exit(NULL);
return 0;
}
Program 3
program to execute three POSIX Threads simultaneously for updating a text file.

# include<pthread.h>
# include<stdio.h>
# define NUM_THREADS 5
void *PrintHello(void *threadid)
{
int tid;
tid=(int)threadid;
printf("Hello world! It's me ,thread #%d!\n",tid);
pthread_exit(NULL);
}
int main(int argc,char *argv[])
{
pthread_t threads(NUM_THREADS];
int rc,t;
for(t=0;t<NUM_THREADS;t++)
{
printf("In main: creating thread %d \n",t);
rc=pthread_create(&threads[t],NULL,PrintHello,(void *)t);
if(rc)
{
printf("ERROR ; return code from pthread_create() is %d\n",rc);
exit(-1);
}
}
pthread_exit(NULL);
}
Program-4

Write A program for Synchronizing POSIX threads (PThreads) using semaphores

#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include <semaphore.h>

void *Do_Task(void *);


int val;
sem_t sem;
int main()
{
pthread_t tidA, tidB;
sem_init(&sem, 0, 1);
pthread_create(&tidA, NULL, &Do_Task,NULL);
pthread_create(&tidB,NULL,&Do_Task,NULL);
pthread_join(tidA,NULL);
printf("Thread A terminated Value is :=%d\n",val);
pthread_join(tidB,NULL);
printf("Thread B terminated Value is :=%d\n",val);
sem_destroy(&sem);
exit(0);
}

void *Do_Task (void *arg)


{
int i;
for(i=0;i<3;i++)
{
printf("Waiting to enter critical region...\n");
sem_wait(&sem);
printf("Incrementing val.......\n", i);
val=val+1;
printf("New Value: %d\n", val);
printf("Exiting critical region...\n");
sem_post(&sem);
}
pthread_exit(0);
}
Program 5

Write A program for Synchronizing POSIX Threads (PThreads) using MUTEX

#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
double *a;
double *b;
double sum;
int veclen;
}DOTDATA;
#define NUMTHRDS 4
#define VECLEN 100
DOTDATA dotstr;
pthread_t callThd[NUMTHRDS];
pthread_mutex_t mutexsum;
void *dotprod(void *arg)
{
int i,start,end, offset,len;
double mysum,*x,*y;
offset=(int)arg;
len=dotstr.veclen;
start=offset*len;
end=start+len;
x=dotstr.a;
y=dotstr.b;
mysum=0;
for(i=start;i<end;i++)
{
mysum+=(x[i]*y[i]);
}
pthread_mutex_lock(&mutexsum);
dotstr.sum+=mysum;
pthread_mutex_unlock(&mutexsum);
pthread_exit((void*)0);
}
int main(int argc,char*argv[])
{
int i;
double *a,*b;
void *status;
pthread_attr_t attr;
a=(double*)malloc(NUMTHRDS*VECLEN*sizeof(double));
b=(double*)malloc(NUMTHRDS*VECLEN*sizeof(double));
for(i=0;i<VECLEN*NUMTHRDS;i++)
{
a[i]=1;
b[i]=a[i];
}
dotstr.veclen=VECLEN;
dotstr.a=a;
dotstr.b=b;
dotstr.sum=0;
pthread_mutex_init(&mutexsum,NULL);
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_JOINABLE);
for(i=0;i<NUMTHRDS;i++)
{
pthread_create(&callThd[i],&attr,dotprod,(void*)i);
}
pthread_attr_destroy(&attr);
for(i=0;i<NUMTHRDS;i++)
{
pthread_join(callThd[i],&status);
}
printf("sum=%f\n",dotstr.sum);
free(a);
free(b);
pthread_mutex_destroy(&mutexsum);
pthread_exit(NULL);
}
Program 6

program in c for Mutex

#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>

int myglobal;
pthread_mutex_t mymutex=PTHREAD_MUTEX_INITIALIZER;

void *thread_function(void *arg)


{
int i,j;
for ( i=0; i<20; i++ )
{
pthread_mutex_lock(&mymutex);
j=myglobal;
j=j+1;
printf(".");
fflush(stdout);
sleep(1);
myglobal=j;
pthread_mutex_unlock(&mymutex);
}
return NULL;
}

int main(void)
{
pthread_t mythread;
int i;
if ( pthread_create( &mythread, NULL, thread_function, NULL) )
{
printf("error creating thread.");
bort();
}

for ( i=0; i<20; i++)


{
pthread_mutex_lock(&mymutex);
myglobal=myglobal+1;
pthread_mutex_unlock(&mymutex);
printf("o");
fflush(stdout);
sleep(1);
}

if ( pthread_join ( mythread, NULL ) )


{
printf("error joining thread.");
abort();
}
printf("\nmyglobal equals %d\n",myglobal);
exit(0);
}
Program 7

program for Synchronized swimming threads


#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>

int myglobal;

void *thread_function(void *arg)


{
int i,j;
for ( i=0; i<20; i++ )
{
j=myglobal;
j=j+1;
printf(".");
fflush(stdout);
sleep(1);
myglobal=j;
}
return NULL;
}

You might also like