338 Intermediate C Programming
printf (" ERROR ; pthr ead_join () returns %d n" , rtv ) ;27
return EXIT _FAILUR E ;28
}29
return EXIT _SUCCES S ;30
}31
To compile this file into an executable program, we need to add -lpthread at the end
of the gcc command in this way:
$ gcc -g -Wall -Wshadow thread1.c -o thread1 -lpthread
This will link the pthread library to the executable program. When the program runs,
it prints the following output:
Hello World! arg = 12345
How does this program work? Let’s start at line 17 where pthread
create is called.
The first argument is the address of a pthread t object created at line 14. The second
argument is NULL, and thus the thread is initialized with the default attributes. The third
argument is printHello. This is the address of the printHello function and pthread will
use this address to execute printHello of lines 4 to 10. The function’s return type must be
void *. The function must take one and only one argument and the type must be void *.
The fourth argument to pthread create is the argument that pthreads will in turn pass
to printHello when it calls this function. In this case, it is the address of an integer. If
pthread create succeeds, it returns zero. This indicates that the thread is created normally.
There are now two parallel threads in the program.
Inside printHello, the argument is cast to the correct type. Since line 18 uses the
address of an integer, the correct type is int *. Line 7 dereferences the address to get the
integer value. The printHello has no useful information to return so it returns NULL at line
9. The main thread calls pthread join to wait for the second thread to finish executing
before main terminates. If pthread join is not called, the main thread may terminate and
destroy the second thread before it gets a chance to print anything to the terminal.
21.4 Subset Sum
The subset sum problem is commonly encountered when studying cryptography. This
problem can be defined in different ways. Here is one definition. Consider a positive integer
k and a set of positive integers A = {a
1
, a
2
, ..., a
n
}. Is it possible to find a subset B =
{b
1
, b
2
, ..., b
m
} ⊂ A such that k = b
1
+ b
2
+ ... + b
m
? Note that B cannot be the empty set
because k > 0. Since B is a subset of A, m must be less than or equal to n.
Consider the following example: A = {19, 10, 2, 9, 8} and k = 20. We have k = 10+2+8,
and thus there is a solution. Consider another example, A = {3, 5, 1, 2, 6} and k = 21. In
this case, the sum of all elements in A is 3 + 5 + 1 + 2 + 6 = 17, which is smaller than
k. Hence there is no solution. Sometimes multiple solutions are possible. For example, if
A = {1, 2, 3, 4, 5} and k is 7. Here we have the solutions: {1, 2, 4}, {3, 4}, and {2, 5}.
This problem asks you to write a program that counts the number of subsets whose sum
equals to the value of k. For a set of n elements, there are 2
n
subsets. This program restricts
the size of sets to 31 so that the number of subsets does not exceed 2
31
.