Data Does not stores in Structure
Hi there I have to read data from text file into the array in structure and then start multiple threads to compute it and finally show the results .now i have worked out the File read part and Threading part as well as the display part .
What remains is the mysterious BUG that When i call my ReadFile function from MyFunctions.C then it reads the file well and displays well But if i call the same function in same file but call from other .c file then it shows garbage values.I tried everything but cannot trace the BUG following is my complete program
myheader.h
struct Matrix {
int m1[10][10];
int row;
int mult[10][10];
};
void *CalculateSquare(void *arguments);
Main.c
#include <stdio.h>
#include<stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include "myheader.h"
#define Row 4
#define Column 4
int main()
{
pthread_t pth[4];
struct Matrix args;
int i=0,j=0,k=0;
ReadFromFile(&args);
printf("Matrix is :\n");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf("%d\t",(int)args.m1[i][j]);
}
printf("\n");
}
///////////////////////// Create New Thread and Store its Id in an array for future Use//////////////////
for(i=0;i<Row;i++)
{
args.row = i;
pthread_create(&pth[i],NULL,CalculateSquare,(void *)&args);
}
///////////////////////// Join All threads so that program waits for completion of each/////////////////
for(i=0;i<Row;i++)
{
pthread_join(pth[i],NULL);
}
printf("Waiting for Thread to Complete\n");
/////////////////////// Display the Matrix ////////////
printf("Square of the Matrix is :\n");
for(i=0;i<Row;i++)
{
for(j=0;j<Column;j++)
printf("%d\t",args.mult[i][j]);
printf("\n");
}
return 0;
}
MyFunctions.C
#include <stdio.h>
#include<stdlib.h>
#include "myheader.h"
struct Matrix ReadFromFile(struct Matrix args)
{
int col = 4, row = 4, i, j;
int temp;
FILE *fin;
fin=fopen("test.txt","r");
if (!fin)
perror("Can't open input");
//args.array = (int **)(malloc(sizeof(int**) *row ));
for(i=0;i<4;i++)
{
//args.array[i] = (int **)(malloc(sizeof(int**) * col));
for(j=0;j<4;j++)
{
fscanf(fin,"%d \n",&temp);
args.m1[i][j] = (int)temp;
}
}
开发者_开发知识库fclose(fin);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf("%d\t",(int)args.m1[i][j]);
}
printf("\n");
}
return args;
}
/* This is our thread function.It takes the Row to Process */
void *CalculateSquare(void *arguments)
{
struct Matrix args =*((struct Matrix *) arguments);
int rowToProcess;
int j = 0;
int k = 0;
rowToProcess = (int)args.row;
printf("Thread calculating Row Number %d\n",rowToProcess);
for(j=0;j<4;j++)
{
args.mult[rowToProcess][j]=0;
for(k=0;k<4;k++)
{
//args.mult[rowToProcess][j] += (int)(args.m1[rowToProcess][k]*args.m1[k][j]);
}
}
// sleep(5);
return NULL;
}
Output is
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Matrix is :
2 2 -1 0
0 2 1 4242343
3 -1075858606 1 0
4193048 -1075858606 4242341 0
Thread calculating Row Number 1
Thread calculating Row Number 2
Thread calculating Row Number 3
Thread calculating Row Number 3
Waiting for Thread to Complete
Square of the Matrix is :
0 909653609 0 0
0 0 0 0
0 0 0 15868726
15892704 134513372 -1217004872 15949812
In your mainfile you call ReadFromFile(&args);
- so the function argument is a pointer to a struct Matrix. But you function itself has this declaration:
struct Matrix ReadFromFile(struct Matrix args)
This means that it needs a struct Matrix - not a pointer to it. You you may want to change this argument into a pointer.
This will of course also mean that you need to change the access to this struct from args.m1[i][j] = (int)temp;
to args->m1[i][j] = (int)temp;
Edit: Also, if you posted your whole header file, you missed the declaration of ReadFromFile.
To be honest, I wonder why your code actually compiled in the first place with those errors, you might want to output all warnings while compiling (-Wall flag for gcc)
精彩评论