0% found this document useful (0 votes)
44 views3 pages

Exercise

CSCI3150-labCond introduces students to operating systems concepts through a lab exploring condition variables. The lab has students use condition variables to simulate multiple processes waiting for and accessing shared resources in a synchronized manner. Students will gain experience with condition variables and their role in process synchronization and coordination between threads.

Uploaded by

杨喆
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)
44 views3 pages

Exercise

CSCI3150-labCond introduces students to operating systems concepts through a lab exploring condition variables. The lab has students use condition variables to simulate multiple processes waiting for and accessing shared resources in a synchronized manner. Students will gain experience with condition variables and their role in process synchronization and coordination between threads.

Uploaded by

杨喆
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/ 3

CSCI3150-labCond

C
S
C
I
-
3
1
5
0
:
I
n
t
r
o
d
u
c
t
i
o
n
t
o
O
p
e
r
a
t
i
n
g
S
y
s
t
e
m
s
1. Background

L
a
b
8
:
C
o
n
d
i
t
i
o
n
V
a
r
i
a
b
l
e
D
e
a
d
l
i
n
e
:
D
e
c
e
m
b
e
r
1
1
,
2
0
2
3
2
3
:
5
9
In this lab, you will learn how to use condition variables in pthread library to implement a wakeupenabled multi-thread program. You will
learn the following functions:
1. pthread_cond_init() : function to create a condition variable for wakeup-enabled access to
shared resources.
2. pthread_cond_wait() : function to wait for a condition variable.
3. pthread_cond_signal() : function to wake up a waiting thread.

2. Creating Thread
The file create_thread.c is an example program of creating multiple threads. When a new thread is created, first we need to declare the
function that the thread will execute. In the function thr_func , we ask the thread to print a message according to the data passed to the
thread. The data is an integer indicating the thread id. Once the thread has finished its job, you need to call pthread_exit() function to
terminate the thread.

In the main program, we declare an array of pthread_t, the data structure storing thread information, and create 8 threads using
pthread_create() . For each thread, we need to pass the address of pthread_t (also regarded as the thread handle), the thread function

(need to be executed) and the data to this function. Once the thread is created, it will execute the code in the function thr_func . When
pthread_join() is called, the main thread will be blocked until all the threads created in array thr have terminated.

Compile & Run: The program (create_thread.c) can be compiled and executed as follows:

1 $ gcc -o phello create_thread.c -lpthread


2 $ ./phello

Note: The compilation option -lpthread must be included when using pthread library.
From the result of the program, we can see that the main thread will print "All threads exited." only after all the thread has terminated.
hello from thr_func, thread id: 0
hello from thr_func, thread id: 1
hello from thr_func, thread id: 2
hello from thr_func, thread id: 3
hello from thr_func, thread id: 4
hello from thr_func, thread id: 5
hello from thr_func, thread id: 6
hello from thr_func, thread id: 7
All threads exited.
Note: Due to the indeterministic nature of thread execution (threads can be schedule to run by the OS in arbitrary order), the output of the
child threads is not necessarily in a specific order. Other orders such as 7 6 5 4 3 2 1 0 are also possible.
Question: What if we try to remove the function call to pthread_join()?

3. Mutual Exclusion
It is common to share resources and data among multiple threads so that they can cooperate with each other to accomplish some tasks.
However, we need to avoid multiple threads to access shared resources that cannot be accessed at the same time (e.g. critical section)
since this may lead to unexpected behaviors. Mutual Exclusion is the method of serializing access to shared resources. In multi-threading
programming, a common way to perform mutual exclusion is to use mutex.

The file mutex.c is an example program of resource sharing between multiple threads. The program is to create multiple threads, and
each thread will add 10 to a shared variable cnt. In the file we declare a pthread_mutex_t variable named mtx, which is used to store the
information of mutex. In the thread function, before the thread tries to add value to cnt, we need to call function pthread_mutex_lock() to
lock the mutex mtx. If mtx is not locked by other threads, then the thread can lock it and enter the critical section. Otherwise, the thread will
be blocked until the mutex is unlocked by other threads. After incrementing the number cnt, we call pthread_mutex_unlock() to unlock
the mutex so that other threads could get access to the shared variable.
Compile & Run: The program (mutex.c) can be compiled and executed as follows:

1 $ gcc -o my-mutex mutex.c -lpthread


2 $ ./my-mutex

From the result of the program, it can be observed that each thread will increase the counter cnt
when the mutex mtx is locked. 10 iterations will be repeated each thread, so after execution the
cnt will become 80 (because we created 8 threads).
Question: What if we try to remove the function call to pthread_mutex_lock() and pthread_-
mutex_unlock() ? What if the iterations are set to 10000? or 1000000?

4. Condition Variable Exercise


In this exercise, you are required to create some child threads and use condition variables to achieve
the same behaviour as pthread_join and pthread_exit . The following program creates 5 (N_-THREADS) and each of them prints a
message before they call thr_exit . The main function calls thr_join to wait for the threads to terminate before it returns.

You should fill in all /* YOUR CODE HERE */ with your own code to complete the program.

1 #include <stdio.h>
2 #include <pthread.h>
3
4 #define N_THREADS (5)
5
6 static int exited;
7
8 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
9 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
10
11 void thr_exit() {
12 pthread_mutex_lock(&mutex);
13
14 /* YOUR CODE HERE */
15
16 pthread_mutex_unlock(&mutex);
17 }
18
19 void thr_join() {
20 pthread_mutex_lock(&mutex);
21
22 while (/* YOUR CODE HERE */ ) /* YOUR CODE HERE */
23
24 pthread_mutex_unlock(&mutex);
25 }
26
27 void *child_func(void arg) {
28 int thr_id = /* YOUR CODE HERE */ ;
29 printf("child %d created and exited\n", thr_id);
30 thr_exit();
31 return NULL;
32 }
33
34 int main() {
35 pthread_t p[N_THREADS];
36 int thr_idx[N_THREADS];
37 void arg;
38 int i;
39
40 exited = /* YOUR CODE HERE */ ;
41
42 puts("parent: begin");
43
44 for (i = 0; i < N_THREADS; i++) {
45 thr_idx[i] = i;
46 arg = &thr_idx[i];
47 pthread_create(/* YOUR CODE HERE */ );
48 }
49
50 thr_join();
51
52 puts("parent: end");
53
54 return 0;
55 }

You can find the program code in exercise.c. To compile and run the program, use:

1 $ gcc -o exercise exercise.c -lpthread


2 $ ./exercise

Sample output:
parent: begin
child 0 created and exited
child 3 created and exited
child 1 created and exited
child 4 created and exited
child 2 created and exited
parent: end
Note: Because the threads are scheduled to run in arbitrary order, the order of child output can be different from the sample output. If you
correctly implement thr_join and thr_exit, you should see "parent: end" at the end of the output, after all child outputs. Also, it is possible that
all child threads run and finish before the main thread call thr_join so you may get the sample output even if the two functions are not
correctly implemented. You should run the program multiple times to detect potential bugs.

5 Submission
You ONLY need to submit the completed exercise.c to Blackboard. If you have any questions about this assignment, please send a email to
[email protected] (Chengye.Yu)

You might also like