CS318 - Pintos
Pintos source browser for JHU CS318 course
alarm-priority.c
Go to the documentation of this file.
1 /** Checks that when the alarm clock wakes up threads, the
2  higher-priority threads run first. */
3 
4 #include <stdio.h>
5 #include "tests/threads/tests.h"
6 #include "threads/init.h"
7 #include "threads/malloc.h"
8 #include "threads/synch.h"
9 #include "threads/thread.h"
10 #include "devices/timer.h"
11 
14 static struct semaphore wait_sema;
15 
16 void
18 {
19  int i;
20 
21  /* This test does not work with the MLFQS. */
23 
24  wake_time = timer_ticks () + 5 * TIMER_FREQ;
25  sema_init (&wait_sema, 0);
26 
27  for (i = 0; i < 10; i++)
28  {
29  int priority = PRI_DEFAULT - (i + 5) % 10 - 1;
30  char name[16];
31  snprintf (name, sizeof name, "priority %d", priority);
33  }
34 
36 
37  for (i = 0; i < 10; i++)
39 }
40 
41 static void
43 {
44  /* Busy-wait until the current time changes. */
46  while (timer_elapsed (start_time) == 0)
47  continue;
48 
49  /* Now we know we're at the very beginning of a timer tick, so
50  we can call timer_sleep() without worrying about races
51  between checking the time and a timer interrupt. */
53 
54  /* Print a message on wake-up. */
55  msg ("Thread %s woke up.", thread_name ());
56 
57  sema_up (&wait_sema);
58 }
name
char * name[]
Definition: insult.c:47
TIMER_FREQ
#define TIMER_FREQ
Number of timer interrupts per second.
Definition: timer.h:8
snprintf
int snprintf(char *buffer, size_t buf_size, const char *format,...)
Like printf(), except that output is stored into BUFFER, which must have space for BUF_SIZE character...
Definition: stdio.c:62
NULL
#define NULL
Definition: stddef.h:4
semaphore
A counting semaphore.
Definition: synch.h:8
UNUSED
#define UNUSED
GCC lets us add "attributes" to functions, function parameters, etc.
Definition: debug.h:7
thread_name
const char * thread_name(void)
Returns the name of the running thread.
Definition: thread.c:247
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
sema_up
void sema_up(struct semaphore *sema)
Up or "V" operation on a semaphore.
Definition: synch.c:109
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
sema_down
void sema_down(struct semaphore *sema)
Down or "P" operation on a semaphore.
Definition: synch.c:61
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
sema_init
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
PRI_MIN
#define PRI_MIN
Thread priorities.
Definition: thread.h:23
wait_sema
static struct semaphore wait_sema
Definition: alarm-priority.c:14
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
thread_set_priority
void thread_set_priority(int new_priority)
Sets the current thread's priority to NEW_PRIORITY.
Definition: thread.c:336
tests.h
thread_func
void thread_func(void *aux)
Definition: thread.h:116
thread.h
synch.h
alarm_priority_thread
static thread_func alarm_priority_thread
Checks that when the alarm clock wakes up threads, the higher-priority threads run first.
Definition: alarm-priority.c:12
wake_time
static int64_t wake_time
Definition: alarm-priority.c:13
PRI_DEFAULT
#define PRI_DEFAULT
Default priority.
Definition: thread.h:24
test_alarm_priority
void test_alarm_priority(void)
Definition: alarm-priority.c:17