// Program: OS2.c

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#include<sys/types.h>

int arr1[3][3];
int arr2[3][3];
int res[3][3];

struct v{ int i,j;};

int sum=0;

void* result(void *arg);
int main()
{
 int i,j;
 void *status=NULL;
 int res1=0;
 pthread_t tid;
 int add=0;

 struct v *data=(struct v *) malloc(sizeof(struct v));
 printf("\nEnter First Matix:");

 for(i=0;i<3;i++)
 {
  for(j=0;j<3 ;j++)
   scanf("%d",&arr1[i][j]);
 }

 printf("Enter Second Matrix:");
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
   scanf("%d",&arr2[i][j]);
 }

 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   data->i=i;
   data->j=j;
   pthread_create(&tid,NULL,result,data);
   pthread_join(tid,&status);
   
   res1=*((int *)status);
   printf("Sum:%d",res1);
   add+=res1;
   res[data->i][data->j]=res1;

  }
  
 }

 printf("\nResulting matrix:\n");
 
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
   printf("\t%d",res[i][j]);
  printf("\n");
 }
 printf("Addtiotin:%d",add);
}

void* result(void *arg)
{
 struct v *data=arg;
 int k;
 sum=0;
 for(k=0;k<3;k++)
  sum+=arr1[data->i][data->j]*arr2[data->i][data->j];
 pthread_exit(&sum); 

}

//OUTPUT

huzaif@huzaif-Lenovo-Z51-70:~/Desktop/os/2$ gcc os2.c -o p1 -lpthread
huzaif@huzaif-Lenovo-Z51-70:~/Desktop/os/2$ ./p1

Enter First Matix:3
4
2
1
3
45
5
4
3
Enter Second Matrix:5
4
3
2
3
6
7
8
2
Sum:45Sum:48Sum:18Sum:6Sum:27Sum:810Sum:105Sum:96Sum:18
Resulting matrix:
 45 48 18
 6 27 810
 105 96 18
Addtiotin:1173