PKUOS - Pintos
Pintos source browser for PKU Operating System course
priority-sema.c
Go to the documentation of this file.
1/** Tests that the highest-priority thread waiting on a semaphore
2 is the first to wake up. */
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
13static struct semaphore sema;
14
15void
17{
18 int i;
19
20 /* This test does not work with the MLFQS. */
22
23 sema_init (&sema, 0);
25 for (i = 0; i < 10; i++)
26 {
27 int priority = PRI_DEFAULT - (i + 3) % 10 - 1;
28 char name[16];
29 snprintf (name, sizeof name, "priority %d", priority);
31 }
32
33 for (i = 0; i < 10; i++)
34 {
35 sema_up (&sema);
36 msg ("Back in main thread.");
37 }
38}
39
40static void
42{
43 sema_down (&sema);
44 msg ("Thread %s woke up.", thread_name ());
45}
#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
void test_priority_sema(void)
Definition: priority-sema.c:16
static thread_func priority_sema_thread
Tests that the highest-priority thread waiting on a semaphore is the first to wake up.
Definition: priority-sema.c:12
static struct semaphore sema
Definition: priority-sema.c:13
#define NULL
Definition: stddef.h:4
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