PKUOS - Pintos
Pintos source browser for PKU Operating System course
alarm-simultaneous.c
Go to the documentation of this file.
1/** Creates N threads, each of which sleeps a different, fixed
2 duration, M times. Records the wake-up order and verifies
3 that it is valid. */
4
5#include <stdio.h>
7#include "threads/init.h"
8#include "threads/malloc.h"
9#include "threads/synch.h"
10#include "threads/thread.h"
11#include "devices/timer.h"
12
13static void test_sleep (int thread_cnt, int iterations);
14
15void
17{
18 test_sleep (3, 5);
19}
20
21/** Information about the test. */
23 {
24 int64_t start; /**< Current time at start of test. */
25 int iterations; /**< Number of iterations per thread. */
26 int *output_pos; /**< Current position in output buffer. */
27 };
28
29static void sleeper (void *);
30
31/** Runs THREAD_CNT threads thread sleep ITERATIONS times each. */
32static void
33test_sleep (int thread_cnt, int iterations)
34{
35 struct sleep_test test;
36 int *output;
37 int i;
38
39 /* This test does not work with the MLFQS. */
41
42 msg ("Creating %d threads to sleep %d times each.", thread_cnt, iterations);
43 msg ("Each thread sleeps 10 ticks each time.");
44 msg ("Within an iteration, all threads should wake up on the same tick.");
45
46 /* Allocate memory. */
47 output = malloc (sizeof *output * iterations * thread_cnt * 2);
48 if (output == NULL)
49 PANIC ("couldn't allocate memory for test");
50
51 /* Initialize test. */
52 test.start = timer_ticks () + 100;
53 test.iterations = iterations;
54 test.output_pos = output;
55
56 /* Start threads. */
57 ASSERT (output != NULL);
58 for (i = 0; i < thread_cnt; i++)
59 {
60 char name[16];
61 snprintf (name, sizeof name, "thread %d", i);
63 }
64
65 /* Wait long enough for all the threads to finish. */
66 timer_sleep (100 + iterations * 10 + 100);
67
68 /* Print completion order. */
69 msg ("iteration 0, thread 0: woke up after %d ticks", output[0]);
70 for (i = 1; i < test.output_pos - output; i++)
71 msg ("iteration %d, thread %d: woke up %d ticks later",
72 i / thread_cnt, i % thread_cnt, output[i] - output[i - 1]);
73
74 free (output);
75}
76
77/** Sleeper thread. */
78static void
79sleeper (void *test_)
80{
81 struct sleep_test *test = test_;
82 int i;
83
84 /* Make sure we're at the beginning of a timer tick. */
85 timer_sleep (1);
86
87 for (i = 1; i <= test->iterations; i++)
88 {
89 int64_t sleep_until = test->start + i * 10;
90 timer_sleep (sleep_until - timer_ticks ());
91 *test->output_pos++ = timer_ticks () - test->start;
92 thread_yield ();
93 }
94}
static void test_sleep(int thread_cnt, int iterations)
Creates N threads, each of which sleeps a different, fixed duration, M times.
void test_alarm_simultaneous(void)
static void sleeper(void *)
Sleeper thread.
#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 PANIC(...)
Halts the OS, printing the source file name, line number, and function name, plus a user-specific mes...
Definition: debug.h:14
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 * malloc(size_t size)
Obtains and returns a new block of at least SIZE bytes.
Definition: malloc.c:90
void free(void *p)
Frees block P, which must have been previously allocated with malloc(), calloc(), or realloc().
Definition: malloc.c:219
#define NULL
Definition: stddef.h:4
signed long long int int64_t
Definition: stdint.h:16
Information about the test.
int * output_pos
Current position in output buffer.
int iterations
Number of iterations per thread.
int64_t start
Current time at start of test.
Definition: tests.c:7
bool thread_mlfqs
If false (default), use round-robin scheduler.
Definition: thread.c:60
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_yield(void)
Yields the CPU.
Definition: thread.c:302
#define PRI_DEFAULT
Default priority.
Definition: thread.h:24
int64_t timer_ticks(void)
Returns the number of timer ticks since the OS booted.
Definition: timer.c:71
void timer_sleep(int64_t ticks)
Sleeps for approximately TICKS timer ticks.
Definition: timer.c:90