PKUOS - Pintos
Pintos source browser for PKU Operating System course
mlfqs-block.c
Go to the documentation of this file.
1/** Checks that recent_cpu and priorities are updated for blocked
2 threads.
3
4 The main thread sleeps for 25 seconds, spins for 5 seconds,
5 then releases a lock. The "block" thread spins for 20 seconds
6 then attempts to acquire the lock, which will block for 10
7 seconds (until the main thread releases it). If recent_cpu
8 decays properly while the "block" thread sleeps, then the
9 block thread should be immediately scheduled when the main
10 thread releases the lock. */
11
12#include <stdio.h>
13#include "tests/threads/tests.h"
14#include "threads/init.h"
15#include "threads/malloc.h"
16#include "threads/synch.h"
17#include "threads/thread.h"
18#include "devices/timer.h"
19
20static void block_thread (void *lock_);
21
22void
24{
26 struct lock lock;
27
29
30 msg ("Main thread acquiring lock.");
31 lock_init (&lock);
33
34 msg ("Main thread creating block thread, sleeping 25 seconds...");
37
38 msg ("Main thread spinning for 5 seconds...");
40 while (timer_elapsed (start_time) < 5 * TIMER_FREQ)
41 continue;
42
43 msg ("Main thread releasing lock.");
45
46 msg ("Block thread should have already acquired lock.");
47}
48
49static void
50block_thread (void *lock_)
51{
52 struct lock *lock = lock_;
54
55 msg ("Block thread spinning for 20 seconds...");
57 while (timer_elapsed (start_time) < 20 * TIMER_FREQ)
58 continue;
59
60 msg ("Block thread acquiring lock...");
62
63 msg ("...got it.");
64}
#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_mlfqs_block(void)
Definition: mlfqs-block.c:23
static void block_thread(void *lock_)
Checks that recent_cpu and priorities are updated for blocked threads.
Definition: mlfqs-block.c:50
static int64_t start_time
Starts 60 threads that each sleep for 10 seconds, then spin in a tight loop for 60 seconds,...
signed long long int int64_t
Definition: stdint.h:16
Lock.
Definition: synch.h:22
void lock_release(struct lock *lock)
Releases LOCK, which must be owned by the current thread.
Definition: synch.c:229
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
bool thread_mlfqs
If false (default), use round-robin scheduler.
Definition: thread.c:60
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
int64_t timer_ticks(void)
Returns the number of timer ticks since the OS booted.
Definition: timer.c:71
int64_t timer_elapsed(int64_t then)
Returns the number of timer ticks elapsed since THEN, which should be a value once returned by timer_...
Definition: timer.c:82
void timer_sleep(int64_t ticks)
Sleeps for approximately TICKS timer ticks.
Definition: timer.c:90
#define TIMER_FREQ
Number of timer interrupts per second.
Definition: timer.h:8