Prog01.c

 /*	
Practical Assignment : 5
Name    : Divyen K Patel Roll No:24 MCA-4
Program : 1
Multiply matrix (10 x 10) with a vector using variation
in Self Scheduling
(Hint : assume that the total no of calculations are n*n)
*/

# include<stdio.h>
# include "/usr/include/sys/types.h"
# include "/usr/include/sys/shm.h"
# include "/usr/include/sys/ipc.h"
# include "/usr/include/sys/sem.h"
# include "sharedlib.h"
# include "forkjoin.h"
# include "spinlock.h"
# include "barrier.h"


int main()
{
int MatRows,MatCols;
int iCount,jCount;
int Mat[10][10];
int Vect[10];
int i,*index;
int shmid1,shmid2,shmid3,shmid4;
int *lock1,*lock2;
int nProc,id;

int *resVect; // Result Vector


MatRows=3;
MatCols=3;
nProc=MatRows;

printf("\n Enter Matrix Elements ... \n");
for(iCount=1;iCount<=MatRows;iCount++)
{
for (jCount=1;jCount<=MatCols;jCount++)
{
printf("Mat[%d][%d] :",iCount,jCount);
scanf("%d",&Mat[iCount][jCount]);
}
}

printf("\n Enter Vector Elements ... \n");
for(iCount=1;iCount<=MatCols;iCount++)
{
printf("Vect [%d] :",iCount);
scanf("%d",&Vect[iCount]);
}


resVect=(int*)sshared(sizeof(int)*MatRows,&shmid1);
lock1=(int*)sshared(sizeof(int),&shmid2); 
lock2=(int*)sshared(sizeof(int)*MatRows,&shmid3);
index=(int*)sshared(sizeof(int),&shmid4); 

spin_lock_init(lock1);

id=process_fork(nProc);  
spin_lock_init(&lock2[id]);
*index=MatRows * MatCols;

while(1)
{
spin_lock(lock1);
i=*index;
*index=*index - 1;
spin_unlock(lock1);

if(i<0)
{
break;       
}
iCount=(i-1)%MatRows + 1;
jCount=(i-1)/MatCols + 1;

spin_lock(&lock2[iCount]);
resVect[iCount]+=Mat[iCount][jCount] * Vect[jCount];
spin_unlock(&lock2[iCount]);
}

  process_join(nProc,id);


printf("\n Matrix ...\n");
for(iCount=1;iCount<=MatRows;iCount++)
{
for(jCount=1;jCount<=MatCols;jCount++)
{
printf("%d\t",Mat[iCount][jCount]);
}
printf("\n");
}

printf("\n Vector ...\n");
for(iCount=1;iCount<=MatCols;iCount++)
{
printf("%d\n",Vect[iCount]);
}


printf("\n Result Vector ...\n");
for(iCount=1;iCount<=MatRows;iCount++)
{
printf("%d\n",resVect[iCount]);
}

cleanup_memory(&shmid1);
cleanup_memory(&shmid2);
cleanup_memory(&shmid3);
cleanup_memory(&shmid4);
cleanup_semaphore(lock1);
for(iCount=0;iCount<MatRows;iCount++)
{
cleanup_semaphore(&lock2[iCount]);
}
return 0;

}

Project Homepage: