CS318 - Pintos
Pintos source browser for JHU CS318 course
priority-fifo.c
Go to the documentation of this file.
1 /** Creates several threads all at the same priority and ensures
2  that they consistently run in the same round-robin order.
3 
4  Based on a test originally submitted for Stanford's CS 140 in
5  winter 1999 by by Matt Franklin
6  <startled@leland.stanford.edu>, Greg Hutchins
7  <gmh@leland.stanford.edu>, Yu Ping Hu <yph@cs.stanford.edu>.
8  Modified by arens. */
9 
10 #include <stdio.h>
11 #include "tests/threads/tests.h"
12 #include "threads/init.h"
13 #include "devices/timer.h"
14 #include "threads/malloc.h"
15 #include "threads/synch.h"
16 #include "threads/thread.h"
17 
19  {
20  int id; /**< Sleeper ID. */
21  int iterations; /**< Iterations so far. */
22  struct lock *lock; /**< Lock on output. */
23  int **op; /**< Output buffer position. */
24  };
25 
26 #define THREAD_CNT 16
27 #define ITER_CNT 16
28 
30 
31 void
33 {
34  struct simple_thread_data data[THREAD_CNT];
35  struct lock lock;
36  int *output, *op;
37  int i, cnt;
38 
39  /* This test does not work with the MLFQS. */
41 
42  /* Make sure our priority is the default. */
44 
45  msg ("%d threads will iterate %d times in the same order each time.",
47  msg ("If the order varies then there is a bug.");
48 
49  output = op = malloc (sizeof *output * THREAD_CNT * ITER_CNT * 2);
50  ASSERT (output != NULL);
51  lock_init (&lock);
52 
54  for (i = 0; i < THREAD_CNT; i++)
55  {
56  char name[16];
57  struct simple_thread_data *d = data + i;
58  snprintf (name, sizeof name, "%d", i);
59  d->id = i;
60  d->iterations = 0;
61  d->lock = &lock;
62  d->op = &op;
64  }
65 
67  /* All the other threads now run to termination here. */
68  ASSERT (lock.holder == NULL);
69 
70  cnt = 0;
71  for (; output < op; output++)
72  {
73  struct simple_thread_data *d;
74 
75  ASSERT (*output >= 0 && *output < THREAD_CNT);
76  d = data + *output;
77  if (cnt % THREAD_CNT == 0)
78  printf ("(priority-fifo) iteration:");
79  printf (" %d", d->id);
80  if (++cnt % THREAD_CNT == 0)
81  printf ("\n");
82  d->iterations++;
83  }
84 }
85 
86 static void
87 simple_thread_func (void *data_)
88 {
89  struct simple_thread_data *data = data_;
90  int i;
91 
92  for (i = 0; i < ITER_CNT; i++)
93  {
94  lock_acquire (data->lock);
95  *(*data->op)++ = data->id;
96  lock_release (data->lock);
97  thread_yield ();
98  }
99 }
name
char * name[]
Definition: insult.c:47
malloc
void * malloc(size_t size)
Obtains and returns a new block of at least SIZE bytes.
Definition: malloc.c:90
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
lock_release
void lock_release(struct lock *lock)
Releases LOCK, which must be owned by the current thread.
Definition: synch.c:229
NULL
#define NULL
Definition: stddef.h:4
ITER_CNT
#define ITER_CNT
Definition: priority-fifo.c:27
lock::holder
struct thread * holder
Thread holding lock (for debugging).
Definition: synch.h:23
lock_init
void lock_init(struct lock *lock)
Initializes LOCK.
Definition: synch.c:176
simple_thread_data::iterations
int iterations
Iterations so far.
Definition: priority-fifo.c:21
simple_thread_data::op
int ** op
Output buffer position.
Definition: priority-fifo.c:23
thread_get_priority
int thread_get_priority(void)
Returns the current thread's priority.
Definition: thread.c:343
init.h
printf
int printf(const char *format,...)
Writes formatted output to the console.
Definition: stdio.c:79
lock
static struct lock lock
Definition: priority-condvar.c:13
timer.h
malloc.h
simple_thread_data::lock
struct lock * lock
Lock on output.
Definition: priority-fifo.c:22
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
test_priority_fifo
void test_priority_fifo(void)
Definition: priority-fifo.c:32
simple_thread_data::id
int id
Sleeper ID.
Definition: priority-fifo.c:20
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_CNT
#define THREAD_CNT
Definition: priority-fifo.c:26
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
simple_thread_func
static thread_func simple_thread_func
Definition: priority-fifo.c:29
thread_func
void thread_func(void *aux)
Definition: thread.h:116
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
thread_yield
void thread_yield(void)
Yields the CPU.
Definition: thread.c:302
lock
Lock.
Definition: synch.h:21
simple_thread_data
Creates several threads all at the same priority and ensures that they consistently run in the same r...
Definition: priority-fifo.c:18
PRI_DEFAULT
#define PRI_DEFAULT
Default priority.
Definition: thread.h:24