PKUOS - Pintos
Pintos source browser for PKU Operating System 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>
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
14static struct semaphore wait_sema;
15
16void
18{
19 int i;
20
21 /* This test does not work with the MLFQS. */
23
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
41static 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
58}
void test_alarm_priority(void)
static struct semaphore wait_sema
static int64_t wake_time
static thread_func alarm_priority_thread
Checks that when the alarm clock wakes up threads, the higher-priority threads run first.
#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
#define UNUSED
GCC lets us add "attributes" to functions, function parameters, etc.
Definition: debug.h:7
char * name[]
Definition: insult.c:47
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
void msg(const char *format,...)
Definition: lib.c:28
static int64_t start_time
Starts 60 threads that each sleep for 10 seconds, then spin in a tight loop for 60 seconds,...
#define NULL
Definition: stddef.h:4
signed long long int int64_t
Definition: stdint.h:16
A counting semaphore.
Definition: synch.h:9
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 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
const char * thread_name(void)
Returns the name of the running thread.
Definition: thread.c:247
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
void thread_set_priority(int new_priority)
Sets the current thread's priority to NEW_PRIORITY.
Definition: thread.c:336
#define PRI_DEFAULT
Default priority.
Definition: thread.h:24
void thread_func(void *aux)
Definition: thread.h:116
#define PRI_MIN
Thread priorities.
Definition: thread.h:23
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