我使用gcc编译器在Ubuntu 14.04和CentOS 7上执行了以下代码,但奇怪的是它显示了相同输入的不同输出。有两个问题我无法解决。
代码
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#define N 15
struct matrix
{
int num1, num2;
};
void* multiply(void *c);
int main()
{
int i, j, rows, cols, a[N][N], b[N][N], sum, k, final, res[N][N],*ptr;
pthread_t t1, t2;
struct matrix m1;
ptr=∑
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of cols: ");
scanf("%d", &cols);
for(i = 0; i < rows; i++)
{
for(j = 0; j < cols; j++)
{
printf("Enter the value at: a[%d][%d] : ", i, j);
scanf("%d", &a[i][j]);
}
}
for(i = 0; i < rows; i++)
{
for(j = 0; j < cols; j++)
{
printf("Enter the value at: b[%d][%d] : ", i, j);
scanf("%d", &b[i][j]);
}
}
for(i = 0; i < rows; i++)
{
for(j = 0; j < cols; j++)
{
final = 0;
for(k = 0; k < rows; k++)
{
m1.num1 = a[i][k];
m1.num2 = b[k][j];
pthread_create(&t1, NULL, (void*)multiply,(void*)&m1);
pthread_join(t1, (void**)&ptr);
sum=*ptr;
printf("\t%d",sum);
final += sum;
res[i][j] = final;
}
printf("\n");
}
}
printf("The result is :\n");
for(i = 0; i < rows; i++)
{
for(j = 0; j < cols; j++)
{
printf("%d\t", res[i][j]);
}
printf("\n");
}
return 0;
}
void* multiply(void *c)
{
struct matrix *m;
m = (struct matrix *)c;
int p = 0;
p = m->num1 * m->num2;
printf("\t%d * %d = %d",m->num1,m->num2,p);
pthread_exit((void*)&p);
}
在Ubuntu上执行的输出
Enter the number of rows: 2
Enter the number of cols: 2
Enter the value at: a[0][0] : 1
Enter the value at: a[0][1] : 2
Enter the value at: a[1][0] : 3
Enter the value at: a[1][1] : 4
Enter the value at: b[0][0] : 1
Enter the value at: b[0][1] : 2
Enter the value at: b[1][0] : 3
Enter the value at: b[1][1] : 4
1 * 1 = 1 0 2 * 3 = 6 32648
1 * 2 = 2 32648 2 * 4 = 8 32648
3 * 1 = 3 32648 4 * 3 = 12 32648
3 * 2 = 6 32648 4 * 4 = 16 32648
The result is :
32648 65296
65296 65296
在CentOS上执行的输出
Enter the number of rows: 2
Enter the number of cols: 2
Enter the value at: a[0][0] : 1
Enter the value at: a[0][1] : 2
Enter the value at: a[1][0] : 3
Enter the value at: a[1][1] : 4
Enter the value at: b[0][0] : 1
Enter the value at: b[0][1] : 2
Enter the value at: b[1][0] : 3
Enter the value at: b[1][1] : 4
1 * 1 = 1 0 2 * 3 = 6 6
1 * 2 = 2 2 2 * 4 = 8 8
3 * 1 = 3 2 4 * 3 = 12 4
3 * 2 = 6 3 4 * 4 = 16 16
The result is :
6 10
15 22