CS318 - Pintos
Pintos source browser for JHU CS318 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 
20 static void block_thread (void *lock_);
21 
22 void
24 {
26  struct lock lock;
27 
29 
30  msg ("Main thread acquiring lock.");
31  lock_init (&lock);
32  lock_acquire (&lock);
33 
34  msg ("Main thread creating block thread, sleeping 25 seconds...");
36  timer_sleep (25 * TIMER_FREQ);
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.");
44  lock_release (&lock);
45 
46  msg ("Block thread should have already acquired lock.");
47 }
48 
49 static void
50 block_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 }
TIMER_FREQ
#define TIMER_FREQ
Number of timer interrupts per second.
Definition: timer.h:8
block_thread
static void block_thread(void *lock_)
Checks that recent_cpu and priorities are updated for blocked threads.
Definition: mlfqs-block.c:50
lock_release
void lock_release(struct lock *lock)
Releases LOCK, which must be owned by the current thread.
Definition: synch.c:229
lock_init
void lock_init(struct lock *lock)
Initializes LOCK.
Definition: synch.c:176
timer_elapsed
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
test_mlfqs_block
void test_mlfqs_block(void)
Definition: mlfqs-block.c:23
start_time
static int64_t start_time
Starts 60 threads that each sleep for 10 seconds, then spin in a tight loop for 60 seconds,...
Definition: mlfqs-load-60.c:108
int64_t
signed long long int int64_t
Definition: stdint.h:16
init.h
timer.h
timer_sleep
void timer_sleep(int64_t ticks)
Sleeps for approximately TICKS timer ticks.
Definition: timer.c:90
malloc.h
timer_ticks
int64_t timer_ticks(void)
Returns the number of timer ticks since the OS booted.
Definition: timer.c:71
ASSERT
#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
msg
void msg(const char *format,...)
Definition: lib.c:28
thread_mlfqs
bool thread_mlfqs
If false (default), use round-robin scheduler.
Definition: thread.c:60
thread_create
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
tests.h
lock_acquire
void lock_acquire(struct lock *lock)
Acquires LOCK, sleeping until it becomes available if necessary.
Definition: synch.c:193
thread.h
synch.h
lock
Lock.
Definition: synch.h:21
PRI_DEFAULT
#define PRI_DEFAULT
Default priority.
Definition: thread.h:24