PKUOS - Pintos
Pintos source browser for PKU Operating System course
priority-donate-sema.c
Go to the documentation of this file.
1/** Low priority thread L acquires a lock, then blocks downing a
2 semaphore. Medium priority thread M then blocks waiting on
3 the same semaphore. Next, high priority thread H attempts to
4 acquire the lock, donating its priority to L.
5
6 Next, the main thread ups the semaphore, waking up L. L
7 releases the lock, which wakes up H. H "up"s the semaphore,
8 waking up M. H terminates, then M, then L, and finally the
9 main thread.
10
11 Written by Godmar Back <gback@cs.vt.edu>. */
12
13#include <stdio.h>
14#include "tests/threads/tests.h"
15#include "threads/init.h"
16#include "threads/synch.h"
17#include "threads/thread.h"
18
20 {
21 struct lock lock;
23 };
24
28
29void
31{
32 struct lock_and_sema ls;
33
34 /* This test does not work with the MLFQS. */
36
37 /* Make sure our priority is the default. */
39
40 lock_init (&ls.lock);
41 sema_init (&ls.sema, 0);
42 thread_create ("low", PRI_DEFAULT + 1, l_thread_func, &ls);
43 thread_create ("med", PRI_DEFAULT + 3, m_thread_func, &ls);
44 thread_create ("high", PRI_DEFAULT + 5, h_thread_func, &ls);
45 sema_up (&ls.sema);
46 msg ("Main thread finished.");
47}
48
49static void
50l_thread_func (void *ls_)
51{
52 struct lock_and_sema *ls = ls_;
53
54 lock_acquire (&ls->lock);
55 msg ("Thread L acquired lock.");
56 sema_down (&ls->sema);
57 msg ("Thread L downed semaphore.");
58 lock_release (&ls->lock);
59 msg ("Thread L finished.");
60}
61
62static void
63m_thread_func (void *ls_)
64{
65 struct lock_and_sema *ls = ls_;
66
67 sema_down (&ls->sema);
68 msg ("Thread M finished.");
69}
70
71static void
72h_thread_func (void *ls_)
73{
74 struct lock_and_sema *ls = ls_;
75
76 lock_acquire (&ls->lock);
77 msg ("Thread H acquired lock.");
78
79 sema_up (&ls->sema);
80 lock_release (&ls->lock);
81 msg ("Thread H finished.");
82}
#define ASSERT(CONDITION)
This is outside the header guard so that debug.h may be included multiple times with different settin...
Definition: debug.h:31
void msg(const char *format,...)
Definition: lib.c:28
void test_priority_donate_sema(void)
static thread_func m_thread_func
static thread_func h_thread_func
static thread_func l_thread_func
Low priority thread L acquires a lock, then blocks downing a semaphore.
struct semaphore sema
Lock.
Definition: synch.h:22
A counting semaphore.
Definition: synch.h:9
void lock_release(struct lock *lock)
Releases LOCK, which must be owned by the current thread.
Definition: synch.c:229
void sema_init(struct semaphore *sema, unsigned value)
This file is derived from source code for the Nachos instructional operating system.
Definition: synch.c:45
void sema_up(struct semaphore *sema)
Up or "V" operation on a semaphore.
Definition: synch.c:109
void lock_init(struct lock *lock)
Initializes LOCK.
Definition: synch.c:176
void lock_acquire(struct lock *lock)
Acquires LOCK, sleeping until it becomes available if necessary.
Definition: synch.c:193
void sema_down(struct semaphore *sema)
Down or "P" operation on a semaphore.
Definition: synch.c:61
bool thread_mlfqs
If false (default), use round-robin scheduler.
Definition: thread.c:60
int thread_get_priority(void)
Returns the current thread's priority.
Definition: thread.c:343
tid_t thread_create(const char *name, int priority, thread_func *function, void *aux)
Creates a new kernel thread named NAME with the given initial PRIORITY, which executes FUNCTION passi...
Definition: thread.c:166
#define PRI_DEFAULT
Default priority.
Definition: thread.h:24
void thread_func(void *aux)
Definition: thread.h:116