Gcc derleyicisini kullanarak Ubuntu 14.04 ve CentOS 7'de aşağıdaki kodu uyguladık ama garip olan şey aynı girişler için farklı çıktılar göstermesidir. Çözemediğim iki konu var.Farklı Platformlar için garip çok iş parçacıklı çıktı
- Her zaman ilk çoğaltma (1 * 1) için toplam olarak (ana işlevde) 0 aldım.
- Ubuntu için tamamen beklenmedik çıktı. İşte kod ve her iki çıkış. CentOS 01 çalıştırılacak Ubuntu üzerinde yürütülmesi için
Kod
#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);
}
Çıktı
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
Çıktı
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
Ancak pthread_join el ile sayfa pthread_exit() tarafından döndürülen değeri ptr (benim programımda ptr) işaret eden tamsayıya ayarladığını söylüyor. Aklımda, ptr = & sum'u başlangıçta beyan edersem, p değerini tam olarak almamalı mıyım? Bunu denedim ama işe yaramadı. [burada] (http://man7.org/linux/man-pages/man3/pthread_join.3.html) bu el kitabının bağlantısıdır. –
İş parçacığınız tarafından döndürülen çıkış durumu tamsayı gösterici değildir: işaretçinin kendisi ("p" değişkeninin "multiply()') işaretçisi. Bu işaretçi değeri pthread_join() ile ptr'ye döndürülür, ancak artık kullanmak için geçerli değildir, çünkü "p" işaretli değişken artık mevcut değildir. (Man sayfadaki "pthread_exit()' * "öğelerine sağlanan hedef iş parçacığının * değeri" * işareti "değeridir). – caf
Ayrıca, 'ptr'' pthread_join() 'ile üzerine yazılır, böylece başlangıçta ne ayarladığınız önemli değildir. Bu, "p" harfini işaret ederek biter, ancak "p" artık yok. – caf