PKUOS - Pintos
Pintos source browser for PKU Operating System 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
31void
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. */
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
86static void
87simple_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}
#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
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
int printf(const char *format,...)
Writes formatted output to the console.
Definition: stdio.c:79
void msg(const char *format,...)
Definition: lib.c:28
void * malloc(size_t size)
Obtains and returns a new block of at least SIZE bytes.
Definition: malloc.c:90
static struct lock lock
static thread_func simple_thread_func
Definition: priority-fifo.c:29
#define THREAD_CNT
Definition: priority-fifo.c:26
void test_priority_fifo(void)
Definition: priority-fifo.c:32
#define ITER_CNT
Definition: priority-fifo.c:27
#define NULL
Definition: stddef.h:4
Lock.
Definition: synch.h:22
struct thread * holder
Thread holding lock (for debugging).
Definition: synch.h:23
Creates several threads all at the same priority and ensures that they consistently run in the same r...
Definition: priority-fifo.c:19
int ** op
Output buffer position.
Definition: priority-fifo.c:23
int iterations
Iterations so far.
Definition: priority-fifo.c:21
struct lock * lock
Lock on output.
Definition: priority-fifo.c:22
int id
Sleeper ID.
Definition: priority-fifo.c:20
void lock_release(struct lock *lock)
Releases LOCK, which must be owned by the current thread.
Definition: synch.c:229
void lock_init(struct lock *lock)
Initializes LOCK.
Definition: synch.c:176
void lock_acquire(struct lock *lock)
Acquires LOCK, sleeping until it becomes available if necessary.
Definition: synch.c:193
bool thread_mlfqs
If false (default), use round-robin scheduler.
Definition: thread.c:60
int thread_get_priority(void)
Returns the current thread's priority.
Definition: thread.c:343
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
void thread_yield(void)
Yields the CPU.
Definition: thread.c:302
#define PRI_DEFAULT
Default priority.
Definition: thread.h:24
void thread_func(void *aux)
Definition: thread.h:116