- To get familiar with Pthread API
Write a program mathreads.c
and compile it as mathreads
. The program should accept two arguments, create four threads. Each of the threads should perfom an arithmetic operation over the arguments (like addition, subtraction, multiplication or division) as shown below. Main thread should wait for its child threads and print the result. Child threads don't print anything, they just calculate the results.
$ gcc mathreads.c -o mathreads -lpthread
$ ./mathreads 3 5
add: 3+5=8
mul: 3*5=15
sub: 3-5=-2
div: 3/5=0
Write a program single_mult.c
that will take size n
of a matrix as command line argument, will create 2 matrices, A
and B
, of size n
by n
with random values. Then it will multilply A
and B
, store the result in C
.
$ gcc single_mult.c -o single_mult
$ ./single_mult 2
A:
1 2
3 4
B:
2 1
0 1
C:
2 3
6 7
Write program that does the same task as in the previous task but each element C[i][j]
should be calculated by a separate thread.
$ gcc multi_mult.c -o multi_mult
$ ./multi_mult 2
A:
1 2
3 4
B:
2 1
0 1
C:
2 3
6 7
- measure running time of
TASK A
, and running time of similar taskDOMATH
ofsplab2
. Compare them. - measure running time of
TASK B
, and running time ofTASK C
. Compare them. - measure the time of creating
100
threads (usingpthread_create
) vs100
processes (usingfork
). Main/parent thread/process shouldpthread_join
/wait
all child threads/processes. Processes/threads don't do any jobs, justexit
/pthread_exit
successfully.