ACA Practicals
ACA Practicals
# 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
#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
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include <semaphore.h>
#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
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
int myglobal;
pthread_mutex_t mymutex=PTHREAD_MUTEX_INITIALIZER;
int main(void)
{
pthread_t mythread;
int i;
if ( pthread_create( &mythread, NULL, thread_function, NULL) )
{
printf("error creating thread.");
bort();
}
int myglobal;