CS318 - Pintos
Pintos source browser for JHU CS318 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>
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 
13 static struct semaphore sema;
14 
15 void
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 
40 static void
42 {
43  sema_down (&sema);
44  msg ("Thread %s woke up.", thread_name ());
45 }
name
char * name[]
Definition: insult.c:47
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
priority_sema_thread
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
thread_name
const char * thread_name(void)
Returns the name of the running thread.
Definition: thread.c:247
sema_up
void sema_up(struct semaphore *sema)
Up or "V" operation on a semaphore.
Definition: synch.c:109
test_priority_sema
void test_priority_sema(void)
Definition: priority-sema.c:16
init.h
timer.h
sema_down
void sema_down(struct semaphore *sema)
Down or "P" operation on a semaphore.
Definition: synch.c:61
malloc.h
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
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
PRI_DEFAULT
#define PRI_DEFAULT
Default priority.
Definition: thread.h:24
sema
static struct semaphore sema
Definition: priority-sema.c:13