0% found this document useful (0 votes)
17 views5 pages

Exercises On Threads Solutions

Uploaded by

Huy Đặng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views5 pages

Exercises On Threads Solutions

Uploaded by

Huy Đặng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Solutions to exercises on Threads

1. Modify the program thread3.c on slide 27 (or close to) such that it uses 8 threads
rather than 4 threads. a) Each thread must also print the content of the variables
mytid, sum and mysum after the for loop but before calling the lock synchronization
primitive. b) Now try again, this time print the values of these variables immediately
after the instruction sum = sum + mysum;. What differences do you see and how
can you explain those differences.
Sol to b: The values printed are not necessary the same as those printed previously.
This is because the value printed in the synchronization primitive correspond to the
values of the thread that has captured the lock variable. As threads compete to get
this lock variable, the one that succesfully get it is not necessary the thread which
printed the values before the synchronization.

2. Design a simulation with threads of a wed browser connecting with a server (web
site). The browser ask to download a file name OldFinalExam.pdf from this web
site. However, the web site respond ”You don’t have the permission to download
this file”. In your design you must specify:

(a) Which thread is the web browser and which thread is the web server. Sol: The
web browser is simply the main program, and the server is a thread creaed by
the main program.
(b) How the web browser will deliver its request to the thread simulating the server.
Sol: The web browser can pass the name of the file as a function argument to
the thread that is created to simulate the server.
(c) How the thread simulating the web site will deliver its message to the thread
simulating the web browser. Sol: It can copy the message to the request in the
static area of the main program.
(d) The declaration of each function that will be executed as a thread (function
name, function parameters)
(e) How the web browser will wait for the respond from the web server. Sol: Will
execute a pthread join() on the tid of the server thread

You do not need to code this simulation. Your design is not general, only for this
specific example. Your design focus on how the two threads communicate with each
other.

3. Write the pseudo-code of a parallel matrix multiplication algorithm A = B × C


using threads. The dimensions of the matrices are A = a × c, B = a × b and C =
b × c. The number of threads is a.

4. List the fields of a thread control block (TCB) and explain the purpose of each field.
Sol:

(a) Thread Identifier: Unique id (tid) is assigned to every new thread


(b) Stack pointer: Points to thread’s stack in the process

1
(c) Program counter: Points to the current program instruction of the thread
(d) State of the thread (running, ready, waiting, start, done)
(e) Thread’s register values
(f) Pointer to the Process control block (PCB) of the process that the thread lives
on

5. Explain why each thread control block has entries (fields) to store the contain of its
run time registers but does not have entries to store the contain of the dynamically
allocated variables (variables that are allocated memory on the heap section of a
process).
Sol: Values stored in CPU registers, such as the program counter of a thread, are
erased during a context switch. Values stored in CPU registers are not stored any-
where else, thus they must be saved prior to a context switch. Values in dynamically
allocated variables are not erased during a context switch, they stay permanently
in the address space of a process unless they are explicitly erased by a thread in the
process. Dynamically allocated variables don’t have to be restored when a thread
is re-schedule to run, they don’t have to be stored in the TCB.

6. Unlike static variables, threads do not share their stack with other threads. Why
then the stack is not saved in the TCB when a context switch is performed (thread
is de-scheduled)?
Sol: Static variables are accessible by all threads but this not the reason why they
are not stored in the TCB. Static variables are not saved in the TCB because their
values are stored permanently in the process static area. Same for the stacks of the
threads, they are part of the address space of a process, the content of the address
space of a process is not erased by a context switch.

7. Can a thread write into the memory stack of another thread in a same process?
Sol: Normally no. However, since the memory stacks of all the threads in a same
process are in the address space of the process, then nothing prevent a thread from
writing anywhere is the address space of the process, including the memory stack of
another thread. However, it will be impossible for a thread to write in the memory
stack of a thread from another process

8. What is the binary representation of the character 5? What is the binary represen-
tation of the integer 5? How many bytes are needed to store the character 5 and
how many bytes are needed to store the integer 5?
Sol: Character 5 = in ASCII 0110101 (one byte), number 5 101 plus 29 0’s preceding
those 4 bits (4 bytes)

9. Does the multithreaded web server below exhibit task or data parallelism?

2
Sol: It depends whether the thread is calling the same routine or different ones. If
it is calling the same routine, then it is data parallelism, each thread performing the
same task, but on different requests, i.e. different data. If the process call threads
with different function depending on the request, then it is functional parallelism

10. Is it possible to have concurrency but not parallelism? Explain


Sol: Yes, if there is only one CPU there is concurrency, i.e. many threads on the
ready queue, only one in the running state at a time. There could not be two or
more threads in the running state in same time as there is only one CPU, thus no
parallelism.

11. Consider the following code segment:

pid t pid;
pid = fork();
if (pid == 0) { /* child process */
fork();
thread create( . . .);
}
fork();

(a) How many unique processes are created?


(b) How many unique threads are created?

12. Consider again the program thread3.c in the slides on threads (slide 27 or close to
that).

(a) How many TCBs have been instantiated in the kernel immediately after the
execution of the instruction ”pthread t threads[NTHREADS];” (line 1)
Sol: None, line 1 is just a declaration of an array
(b) How many thread stacks there are in the process immediately after the execu-
tion of the instruction ”pthread t threads[NTHREADS];” (line 1)
Sol: There is no stack as there is no thread. The stack is created when the
thread is created, based on the information passed in the parameter attr in
pthreadcreate.
(c) How many TCBs are still instantiated just before the execution of the instruc-
tion ”for (i=0;i<ARRAYSIZE;i++){” (line 2) of the main thread

3
Sol: The resources of all the threads, including stacks and TCBs have been
released in the loop that executes pthread join(). Thus there is no TCBs left
when line 2 is executed
(d) How many thread stacks there are just before the execution of the instruction
”for (i=0;i<ARRAYSIZE;i++){” (line 2) of the main thread
Sol: Same answer as previous question
(e) Assume a fork is executed just after the instruction ”pthread t threads[NTHREADS];”
(line 1), nothing else change in the code of thread3.c. How many TCBs will be
instantiated by the parent and child process all together
Sol: If NTHREADS = 4 then 8 threads are created. A fork() in that position
in the code simply duplicate the parent process, the parent and the child both
do the same computation.
(f) Assume a POSIX fork is executed just before the instruction ”for (i=0; i<NTHREADS;
i++)” (line 3), nothing else change in the code of thread3.c. Theorize (argue)
what could happen when the loop ”for (i=0; i<NTHREADS; i++)” (line 3) is
executed in the child process (more likely this is implementation dependent).
Sol: POSIX fork does not replicate the active threads of the parent process in
the child process. So the child process has no thread to join with. In my case
this seems to generate an error, the child process terminates abnormally.
(g) Assume the piece of code below is executed just after the instruction
"printf ("Done. Sum= %e \n", sum);"
(line 4). What will be the value of sum printed by the child process. Explain
your answer.

pthread_mutex_destroy(&sum_mutex);
if ((childpid = fork()) <= 0) {
pthread_mutex_init(&sum_mutex, NULL);
for (i=0; i<NTHREADS; i++) {
tids[i] = i;
pthread_create(&threads[i], &attr, do_work, (void *) &tids[i]);
}
for (i=0; i<NTHREADS; i++)
pthread_join(threads[i], NULL);
printf ("Done. Sum= %e \n", sum);
}
Sol: Sum in child = 2 *(Sum in parent) because when the fork() is performed,
the value of sum in the parent is copied in the child variable Sum.
(h) The previous example is not practical as the child process repeat the same as
the parent. Give at least one good reason for forking processes which create
threads rather than having all the threads in a same process.
Sol: There are many possible reasons. One if the threads need a lot of
stack memory, a single process may run out of memory given it is allocated a

4
fixed amount of memory, thus better to create child processes with each fewer
threads. Processes could be preferable over threads because of resources isola-
tion, a thread can only mess-up with the contain of a process, so if threads are
split over several processes this could mitigate the impact of a dysfunctional
thread. Similarly, if a thread dies because of some errors like a segment fault,
then only one process dies. Threads are convenient for sharing information
among computing tasks. If such a sharing is not required, then maybe creating
processes might found some value in some circumstances.

13. Assume the image of a process is swapped out from main memory to the external
memory. The process has 3 active threads, two in the ready queue and one in the
running state. What will happen to those 3 threads?
Sol: Before the process is swapped out of main memory, the running thread will be
de-scheduled and the ready threads will be removed from the ready queue.

You might also like