Threads - problem

Hi,
I've run into a wall while working on a pretty basic C program that uses threads. I've already written a very similar one using basically the same method, only that the previous one used one recursive call (and worked perfectly fine), and this one requires two. I keep getting "Segmentation fault (core dumped)" message while trying to run it. What could be the matter? Because, quite frankly, I've run out of options...



#include <stdio.h>
#include <pthread.h>

typedef struct Newton
{
unsigned n, k;
} Newton;

int alg(Newton s)
{
int value;
Newton s1, s2;

if(s.k == 0 || s.k == s.n)
{
value = 1;
}
else
{
s1.n = s.n - 1;
s1.k = s.k - 1;

s2.n = s.n - 1;
s2.k = s.k;

value = alg(s1) + alg(s2);
}

return value;
pthread_exit((void*)value);
}

int main(void)
{
int x;
Newton data;

pthread_t thread1;

printf("(n choose k) = ?\n");
printf(" n: ");
scanf("%d", &data.n);
printf(" k: ");
scanf("%d", &data.k);

if(pthread_create(&thread1, NULL, (void*)&alg, (void*)&data) != 0)
printf("Error! (creating a thread)\n");

if(pthread_join(thread1, (void**)&x) != 0)
printf("Error! (exiting a thread)\n");

printf("(%d choose %d) = %d\n\n", data.n, data.k, x);

return(0);
}
Last edited on
My comments are in the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdio.h>
#include <pthread.h>

typedef struct Newton
{
	unsigned n, k;
} Newton;

int alg(Newton s)  // kbw: thread functions should return void*
{
	int value;
	Newton s1, s2;

	if (s.k == 0 || s.k == s.n)
	{
		value = 1;
	}
	else
	{
		s1.n = s.n - 1;
		s1.k = s.k - 1;

		s2.n = s.n - 1;
		s2.k = s.k;

		value = alg(s1) + alg(s2);
	}

	return value;  // kbw: value is interpreted as a pointer, the derefence gives you unpredictable results
	pthread_exit((void*)value);  // kbw: never runs this code
}

int main(void)
{
	int x;
	Newton data;

	pthread_t thread1;

	printf("(n choose k) = ?\n");
	printf(" n: ");
	scanf("%d", &data.n);
	printf(" k: ");
	scanf("%d", &data.k);

	// kbw: cast overrides the compiler's refusal to accept alg it stands
	if (pthread_create(&thread1, NULL, (void*)&alg, (void*)&data) != 0)
		printf("Error! (creating a thread)\n");

	if (pthread_join(thread1, (void**)&x) != 0)  // kbw: x has the wrong type
		printf("Error! (exiting a thread)\n");

	printf("(%d choose %d) = %d\n\n", data.n, data.k, x); // kbw: crash happens here, x dereferences possibly bad pointer

	return(0);
}
Last edited on
Hi, thanks for the help. I've changed the code according to your feedback, but it still doesn't seem to work. Now the result is always 0, no matter what numbers I put.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#include <pthread.h>

typedef struct Newton
{
	unsigned n, k;
} Newton;

void* alg(Newton s)
{
	int value;
	Newton s1, s2;

	if (s.k == 0 || s.k == s.n)
	{
		value = 1;
	}
	else
	{
		s1.n = s.n - 1;
		s1.k = s.k - 1;

		s2.n = s.n - 1;
		s2.k = s.k;

		value = (int)alg(s1) + (int)alg(s2);
	}

	pthread_exit((void*)value);
    return((void*)value);
}

int main(void)
{
	int x;
	Newton data;
	pthread_t thread1;

	printf("(n choose k) = ?\n");
	printf(" n: ");
	scanf("%d", &data.n);
	printf(" k: ");
	scanf("%d", &data.k);

	if(pthread_create(&thread1, NULL, alg, (void*)&data) != 0)
		printf("Error! (creating a thread)\n");

	if(pthread_join(thread1, (void*)x) != 0)
		printf("Error! (exiting a thread)\n");

	printf("(%d choose %d) = %d\n\n", data.n, data.k, x);

	return(0);
}
Registered users can post here. Sign in or register to post.