Compare commits
30 Commits
ethan-BSD
...
task1/them
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fd5143110f | ||
|
|
e38e1400a2 | ||
|
|
810a5376b9 | ||
|
|
88967acdaa | ||
|
|
e74ee59e17 | ||
|
|
53b296d6c4 | ||
|
|
0d6fb2f167 | ||
|
|
24900545d4 | ||
|
|
fb268cdef0 | ||
|
|
dbb7fd56e3 | ||
|
|
949455aae5 | ||
|
|
5c09ff0149 | ||
|
|
9f53040d27 | ||
|
|
62c8818c05 | ||
|
|
fda79173c0 | ||
|
|
1c53790ca7 | ||
|
|
9bb0b758c8 | ||
|
|
8b3f9e353f | ||
|
|
83910f945c | ||
|
|
9f71c989a9 | ||
|
|
c32a69a368 | ||
|
|
163b7f9016 | ||
|
|
3c1a26b668 | ||
|
|
54b46806ba | ||
|
|
6855a48603 | ||
|
|
79061fcb9b | ||
|
|
d4d5a7a937 | ||
|
|
f1fa7d2ffb | ||
|
|
1821d73b09 | ||
|
|
4ed64cf173 |
10
.gitignore
vendored
10
.gitignore
vendored
@@ -4,6 +4,13 @@
|
|||||||
#ignore pdf files (just keep source files)
|
#ignore pdf files (just keep source files)
|
||||||
*.pdf
|
*.pdf
|
||||||
|
|
||||||
|
#ignore Mac OS generated files
|
||||||
|
.DS_Store
|
||||||
|
|
||||||
|
#ignore code editor generated directories
|
||||||
|
.idea
|
||||||
|
.vscode
|
||||||
|
|
||||||
#ignore junk files from latex output
|
#ignore junk files from latex output
|
||||||
*.out
|
*.out
|
||||||
*.log
|
*.log
|
||||||
@@ -24,6 +31,3 @@
|
|||||||
*.nav
|
*.nav
|
||||||
*.toc
|
*.toc
|
||||||
|
|
||||||
#ignore files from CLion/VSCode IDEs
|
|
||||||
.idea
|
|
||||||
.vscode
|
|
||||||
@@ -1,95 +0,0 @@
|
|||||||
#include <stdint.h>
|
|
||||||
#ifndef FIXED_POINT_H
|
|
||||||
#define FIXED_POINT_H
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
int32_t raw;
|
|
||||||
} fp32_t;
|
|
||||||
|
|
||||||
/* Fixed Point Arithmetic bit count constants */
|
|
||||||
#define NUM_FRAC_BITS 14
|
|
||||||
#define NUM_INT_BITS (31 - NUM_FRAC_BITS)
|
|
||||||
#define CONVERSION_CONST (1 << NUM_FRAC_BITS) /* f = 2^q, (2^20) */
|
|
||||||
|
|
||||||
/* Fixed Point Arithmetic conversion operations */
|
|
||||||
/* Converts an integer n to a fixed point number */
|
|
||||||
inline fp32_t
|
|
||||||
int_to_fp (int32_t n)
|
|
||||||
{
|
|
||||||
return (fp32_t){ n * CONVERSION_CONST };
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Handles conversion of fixed point to integer. First version truncates, second one rounds */
|
|
||||||
inline int32_t
|
|
||||||
fp_floor (fp32_t x)
|
|
||||||
{
|
|
||||||
return x.raw / CONVERSION_CONST;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline int32_t
|
|
||||||
fp_round (fp32_t x)
|
|
||||||
{
|
|
||||||
if (x.raw >= 0)
|
|
||||||
return (x.raw + CONVERSION_CONST / 2) / CONVERSION_CONST;
|
|
||||||
else
|
|
||||||
return (x.raw - CONVERSION_CONST / 2) / CONVERSION_CONST;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Add two fixed points */
|
|
||||||
inline fp32_t
|
|
||||||
fp_add (fp32_t x, fp32_t y)
|
|
||||||
{
|
|
||||||
return (fp32_t){ x.raw + y.raw };
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Subtract two fixed points */
|
|
||||||
inline fp32_t
|
|
||||||
fp_sub (fp32_t x, fp32_t y)
|
|
||||||
{
|
|
||||||
return (fp32_t){ x.raw - y.raw };
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Add fixed point to integer */
|
|
||||||
inline fp32_t
|
|
||||||
fp_add_int (fp32_t x, int32_t n)
|
|
||||||
{
|
|
||||||
return (fp32_t){ x.raw + n * CONVERSION_CONST };
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Subtract integer from fixed point */
|
|
||||||
inline fp32_t
|
|
||||||
fp_sub_int (fp32_t x, int32_t n)
|
|
||||||
{
|
|
||||||
return (fp32_t){ x.raw - n * CONVERSION_CONST };
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Multiple two fixed points */
|
|
||||||
inline fp32_t
|
|
||||||
fp_mul (fp32_t x, fp32_t y)
|
|
||||||
{
|
|
||||||
return (fp32_t){ ((int64_t)x.raw) * y.raw / CONVERSION_CONST };
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Divide two fixed points */
|
|
||||||
inline fp32_t
|
|
||||||
fp_div (fp32_t x, fp32_t y)
|
|
||||||
{
|
|
||||||
return (fp32_t){ ((int64_t)x.raw) * CONVERSION_CONST / y.raw };
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Multiply fixed point and integer */
|
|
||||||
inline fp32_t
|
|
||||||
fp_mul_int (fp32_t x, int32_t n)
|
|
||||||
{
|
|
||||||
return (fp32_t){ x.raw * n };
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Divide fixed point by integer */
|
|
||||||
inline fp32_t
|
|
||||||
fp_div_int (fp32_t x, int32_t n)
|
|
||||||
{
|
|
||||||
return (fp32_t){ x.raw / n };
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif //FIXED_POINT_H
|
|
||||||
@@ -68,7 +68,8 @@ sema_down (struct semaphore *sema)
|
|||||||
old_level = intr_disable ();
|
old_level = intr_disable ();
|
||||||
while (sema->value == 0)
|
while (sema->value == 0)
|
||||||
{
|
{
|
||||||
list_push_back (&sema->waiters, &thread_current ()->elem);
|
list_insert_ordered(&sema->waiters, &thread_current ()->elem,
|
||||||
|
priority_more, NULL);
|
||||||
thread_block ();
|
thread_block ();
|
||||||
}
|
}
|
||||||
sema->value--;
|
sema->value--;
|
||||||
@@ -113,11 +114,18 @@ sema_up (struct semaphore *sema)
|
|||||||
ASSERT (sema != NULL);
|
ASSERT (sema != NULL);
|
||||||
|
|
||||||
old_level = intr_disable ();
|
old_level = intr_disable ();
|
||||||
if (!list_empty (&sema->waiters))
|
|
||||||
thread_unblock (list_entry (list_pop_front (&sema->waiters),
|
/* Wake up (unblock) the highest priority thread from the waiters list */
|
||||||
struct thread, elem));
|
struct thread *t = NULL;
|
||||||
|
if (!list_empty (&sema->waiters))
|
||||||
|
{
|
||||||
|
t = list_entry (list_pop_front (&sema->waiters), struct thread, elem);
|
||||||
|
thread_unblock (t);
|
||||||
|
}
|
||||||
|
|
||||||
sema->value++;
|
sema->value++;
|
||||||
intr_set_level (old_level);
|
intr_set_level (old_level);
|
||||||
|
thread_yield ();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void sema_test_helper (void *sema_);
|
static void sema_test_helper (void *sema_);
|
||||||
@@ -233,7 +241,6 @@ lock_release (struct lock *lock)
|
|||||||
|
|
||||||
lock->holder = NULL;
|
lock->holder = NULL;
|
||||||
sema_up (&lock->semaphore);
|
sema_up (&lock->semaphore);
|
||||||
thread_yield ();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Returns true if the current thread holds LOCK, false
|
/* Returns true if the current thread holds LOCK, false
|
||||||
@@ -254,6 +261,54 @@ struct semaphore_elem
|
|||||||
struct semaphore semaphore; /* This semaphore. */
|
struct semaphore semaphore; /* This semaphore. */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* Function that compares the two *semaphores* associated with the provided
|
||||||
|
list_elem structures. [i.e., takes list_elem of semaphore_elem, and]
|
||||||
|
Returns true if the thread associated with the semaphore associated with a_
|
||||||
|
has a higher priority than that of b_.
|
||||||
|
|
||||||
|
If aux is provided, then it is a pointer to an integer representing the
|
||||||
|
priority of the first semaphore. This is useful when the thread has not been
|
||||||
|
sema'd down yet. */
|
||||||
|
static bool
|
||||||
|
sema_priority_more(const struct list_elem *a_, const struct list_elem *b_,
|
||||||
|
void *aux)
|
||||||
|
{
|
||||||
|
int a_priority, b_priority;
|
||||||
|
|
||||||
|
/* If an aux is provided, then use it as the priority of the first semaphore.
|
||||||
|
Otherwise, get the priority of the first semaphore. */
|
||||||
|
if (aux != NULL)
|
||||||
|
a_priority = *(int *) aux;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
struct semaphore_elem *a = list_entry(a_, struct semaphore_elem, elem);
|
||||||
|
|
||||||
|
/* If waiters list is empty, return false (i.e., a has lower priority) */
|
||||||
|
if (list_empty(&a->semaphore.waiters))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
/* Otherwise, get the thread with the highest priority from the waiters
|
||||||
|
list. By design, this is the first one in the list (See sema_down). */
|
||||||
|
struct thread *a_thread =
|
||||||
|
list_entry(list_front(&a->semaphore.waiters), struct thread, elem);
|
||||||
|
|
||||||
|
a_priority = a_thread->priority;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct semaphore_elem *b = list_entry(b_, struct semaphore_elem, elem);
|
||||||
|
|
||||||
|
/* If waiters list is empty, return true (i.e., a has higher priority) */
|
||||||
|
if (list_empty(&b->semaphore.waiters))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
struct thread *b_thread =
|
||||||
|
list_entry(list_front(&b->semaphore.waiters), struct thread, elem);
|
||||||
|
|
||||||
|
b_priority = b_thread->priority;
|
||||||
|
|
||||||
|
return a_priority > b_priority;
|
||||||
|
}
|
||||||
|
|
||||||
/* Initializes condition variable COND. A condition variable
|
/* Initializes condition variable COND. A condition variable
|
||||||
allows one piece of code to signal a condition and cooperating
|
allows one piece of code to signal a condition and cooperating
|
||||||
code to receive the signal and act upon it. */
|
code to receive the signal and act upon it. */
|
||||||
@@ -265,6 +320,38 @@ cond_init (struct condition *cond)
|
|||||||
list_init (&cond->waiters);
|
list_init (&cond->waiters);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Returns true iff the priority of the only thread in the first singleton
|
||||||
|
semaphore is greater than the priority of the only thread in the second
|
||||||
|
singleton semaphore.
|
||||||
|
|
||||||
|
Where this function is used for insertion in a singleton semaphore list, the
|
||||||
|
third argument may specify a list_elem * to assume corresponds to the thread
|
||||||
|
waiting for the inserting semaphore. For correctness, ensure this thread
|
||||||
|
calls sema_down () for this semaphore before future list accesses. */
|
||||||
|
|
||||||
|
static bool
|
||||||
|
singleton_sema_priority_greater (const struct list_elem *a,
|
||||||
|
const struct list_elem *b,
|
||||||
|
void *insertingThread)
|
||||||
|
{
|
||||||
|
struct list_elem *te_a, *te_b;
|
||||||
|
|
||||||
|
te_b = list_front (
|
||||||
|
&list_entry (b, struct semaphore_elem, elem)->semaphore.waiters);
|
||||||
|
|
||||||
|
if (insertingThread == NULL)
|
||||||
|
{
|
||||||
|
te_a = list_front (
|
||||||
|
&list_entry (a, struct semaphore_elem, elem)->semaphore.waiters);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
te_a = insertingThread;
|
||||||
|
}
|
||||||
|
|
||||||
|
return thread_priority_greater (te_a, te_b, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
/* Atomically releases LOCK and waits for COND to be signaled by
|
/* Atomically releases LOCK and waits for COND to be signaled by
|
||||||
some other piece of code. After COND is signaled, LOCK is
|
some other piece of code. After COND is signaled, LOCK is
|
||||||
reacquired before returning. LOCK must be held before calling
|
reacquired before returning. LOCK must be held before calling
|
||||||
@@ -296,7 +383,14 @@ cond_wait (struct condition *cond, struct lock *lock)
|
|||||||
ASSERT (lock_held_by_current_thread (lock));
|
ASSERT (lock_held_by_current_thread (lock));
|
||||||
|
|
||||||
sema_init (&waiter.semaphore, 0);
|
sema_init (&waiter.semaphore, 0);
|
||||||
list_push_back (&cond->waiters, &waiter.elem);
|
|
||||||
|
/* Insert the semaphore_elem into the waiters list in order of priority.
|
||||||
|
We pass the priority of the current thread as aux to sema_priority_more
|
||||||
|
because the thread has not been sema'd down yet (See sema_priority_more) */
|
||||||
|
int priority = thread_current ()->priority;
|
||||||
|
list_insert_ordered (&cond->waiters, &waiter.elem, sema_priority_more,
|
||||||
|
&priority);
|
||||||
|
|
||||||
lock_release (lock);
|
lock_release (lock);
|
||||||
sema_down (&waiter.semaphore);
|
sema_down (&waiter.semaphore);
|
||||||
lock_acquire (lock);
|
lock_acquire (lock);
|
||||||
|
|||||||
@@ -4,8 +4,6 @@
|
|||||||
#include <random.h>
|
#include <random.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "devices/timer.h"
|
|
||||||
#include "threads/fixed-point.h"
|
|
||||||
#include "threads/flags.h"
|
#include "threads/flags.h"
|
||||||
#include "threads/interrupt.h"
|
#include "threads/interrupt.h"
|
||||||
#include "threads/intr-stubs.h"
|
#include "threads/intr-stubs.h"
|
||||||
@@ -51,7 +49,6 @@ struct kernel_thread_frame
|
|||||||
static long long idle_ticks; /* # of timer ticks spent idle. */
|
static long long idle_ticks; /* # of timer ticks spent idle. */
|
||||||
static long long kernel_ticks; /* # of timer ticks in kernel threads. */
|
static long long kernel_ticks; /* # of timer ticks in kernel threads. */
|
||||||
static long long user_ticks; /* # of timer ticks in user programs. */
|
static long long user_ticks; /* # of timer ticks in user programs. */
|
||||||
static fp32_t load_avg = { 0 }; /* System load average. */
|
|
||||||
|
|
||||||
/* Scheduling. */
|
/* Scheduling. */
|
||||||
#define TIME_SLICE 4 /* # of timer ticks to give each thread. */
|
#define TIME_SLICE 4 /* # of timer ticks to give each thread. */
|
||||||
@@ -67,14 +64,9 @@ static void kernel_thread (thread_func *, void *aux);
|
|||||||
static void idle (void *aux UNUSED);
|
static void idle (void *aux UNUSED);
|
||||||
static struct thread *running_thread (void);
|
static struct thread *running_thread (void);
|
||||||
static struct thread *next_thread_to_run (void);
|
static struct thread *next_thread_to_run (void);
|
||||||
static void init_thread (struct thread *, const char *name, int nice,
|
static void init_thread (struct thread *, const char *name, int priority);
|
||||||
int priority, fp32_t recent_cpu);
|
|
||||||
static bool is_thread (struct thread *) UNUSED;
|
static bool is_thread (struct thread *) UNUSED;
|
||||||
static void *alloc_frame (struct thread *, size_t size);
|
static void *alloc_frame (struct thread *, size_t size);
|
||||||
static int calculate_bsd_priority (fp32_t recent_cpu, int nice);
|
|
||||||
static void thread_update_recent_cpu (struct thread *t, void *aux UNUSED);
|
|
||||||
static bool thread_priority_less (const struct list_elem *a,
|
|
||||||
const struct list_elem *b, void *aux UNUSED);
|
|
||||||
static void schedule (void);
|
static void schedule (void);
|
||||||
void thread_schedule_tail (struct thread *prev);
|
void thread_schedule_tail (struct thread *prev);
|
||||||
static tid_t allocate_tid (void);
|
static tid_t allocate_tid (void);
|
||||||
@@ -103,9 +95,7 @@ thread_init (void)
|
|||||||
|
|
||||||
/* Set up a thread structure for the running thread. */
|
/* Set up a thread structure for the running thread. */
|
||||||
initial_thread = running_thread ();
|
initial_thread = running_thread ();
|
||||||
fp32_t initial_thread_recent_cpu = { 0 };
|
init_thread (initial_thread, "main", PRI_DEFAULT);
|
||||||
init_thread (initial_thread, "main", NICE_DEFAULT, PRI_DEFAULT,
|
|
||||||
initial_thread_recent_cpu);
|
|
||||||
initial_thread->status = THREAD_RUNNING;
|
initial_thread->status = THREAD_RUNNING;
|
||||||
initial_thread->tid = allocate_tid ();
|
initial_thread->tid = allocate_tid ();
|
||||||
}
|
}
|
||||||
@@ -155,28 +145,6 @@ thread_tick (void)
|
|||||||
else
|
else
|
||||||
kernel_ticks++;
|
kernel_ticks++;
|
||||||
|
|
||||||
/* Update system load_avg and all threads recent_cpu every second. */
|
|
||||||
int64_t ticks = timer_ticks ();
|
|
||||||
if (thread_mlfqs && (ticks % TIMER_FREQ == 0))
|
|
||||||
{
|
|
||||||
size_t ready = threads_ready ();
|
|
||||||
if (t != idle_thread)
|
|
||||||
ready++;
|
|
||||||
fp32_t old_coeff = fp_mul (fp_div_int (int_to_fp (59), 60), load_avg);
|
|
||||||
fp32_t new_coeff = fp_div_int (int_to_fp (ready), 60);
|
|
||||||
load_avg = fp_add (old_coeff, new_coeff);
|
|
||||||
|
|
||||||
thread_foreach (thread_update_recent_cpu, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Update current thread's recent_cpu. */
|
|
||||||
if (thread_mlfqs && (t != idle_thread))
|
|
||||||
{
|
|
||||||
t->recent_cpu = fp_add_int (t->recent_cpu, 1);
|
|
||||||
if (ticks % 4 == 0) // recent_cpu was updated, update priority.
|
|
||||||
t->priority = calculate_bsd_priority (t->recent_cpu, t->nice);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Enforce preemption. */
|
/* Enforce preemption. */
|
||||||
if (++thread_ticks >= TIME_SLICE)
|
if (++thread_ticks >= TIME_SLICE)
|
||||||
intr_yield_on_return ();
|
intr_yield_on_return ();
|
||||||
@@ -224,8 +192,7 @@ thread_create (const char *name, int priority,
|
|||||||
return TID_ERROR;
|
return TID_ERROR;
|
||||||
|
|
||||||
/* Initialize thread. */
|
/* Initialize thread. */
|
||||||
struct thread *pt = thread_current ();
|
init_thread (t, name, priority);
|
||||||
init_thread (t, name, pt->nice, priority, pt->recent_cpu);
|
|
||||||
tid = t->tid = allocate_tid ();
|
tid = t->tid = allocate_tid ();
|
||||||
|
|
||||||
/* Prepare thread for first run by initializing its stack.
|
/* Prepare thread for first run by initializing its stack.
|
||||||
@@ -252,6 +219,11 @@ thread_create (const char *name, int priority,
|
|||||||
|
|
||||||
/* Add to run queue. */
|
/* Add to run queue. */
|
||||||
thread_unblock (t);
|
thread_unblock (t);
|
||||||
|
thread_yield ();
|
||||||
|
|
||||||
|
/* Yield if the new thread has a higher priority than the current thread. */
|
||||||
|
if (priority > thread_get_priority ())
|
||||||
|
thread_yield ();
|
||||||
|
|
||||||
return tid;
|
return tid;
|
||||||
}
|
}
|
||||||
@@ -289,10 +261,10 @@ thread_unblock (struct thread *t)
|
|||||||
|
|
||||||
old_level = intr_disable ();
|
old_level = intr_disable ();
|
||||||
ASSERT (t->status == THREAD_BLOCKED);
|
ASSERT (t->status == THREAD_BLOCKED);
|
||||||
if (thread_mlfqs)
|
|
||||||
list_insert_ordered (&ready_list, &t->elem, thread_priority_less, NULL);
|
/* Insert the thread back into the ready list in priority order. */
|
||||||
else
|
list_insert_ordered(&ready_list, &t->elem, priority_more, NULL);
|
||||||
list_push_back (&ready_list, &t->elem);
|
|
||||||
t->status = THREAD_READY;
|
t->status = THREAD_READY;
|
||||||
intr_set_level (old_level);
|
intr_set_level (old_level);
|
||||||
}
|
}
|
||||||
@@ -362,14 +334,11 @@ thread_yield (void)
|
|||||||
ASSERT (!intr_context ());
|
ASSERT (!intr_context ());
|
||||||
|
|
||||||
old_level = intr_disable ();
|
old_level = intr_disable ();
|
||||||
if (cur != idle_thread)
|
|
||||||
{
|
/* Insert the thread back into the ready list in priority order. */
|
||||||
if (thread_mlfqs)
|
if (cur != idle_thread)
|
||||||
list_insert_ordered (&ready_list, &cur->elem, thread_priority_less,
|
list_insert_ordered(&ready_list, &cur->elem, priority_more, NULL);
|
||||||
NULL);
|
|
||||||
else
|
|
||||||
list_push_back (&ready_list, &cur->elem);
|
|
||||||
}
|
|
||||||
cur->status = THREAD_READY;
|
cur->status = THREAD_READY;
|
||||||
schedule ();
|
schedule ();
|
||||||
intr_set_level (old_level);
|
intr_set_level (old_level);
|
||||||
@@ -392,13 +361,29 @@ thread_foreach (thread_action_func *func, void *aux)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Function that compares the two threads associated with the provided
|
||||||
|
list_elem structures. Returns true if the thread associated with a_ has
|
||||||
|
a higher priority than that of b_. */
|
||||||
|
bool
|
||||||
|
priority_more (const struct list_elem *a_, const struct list_elem *b_,
|
||||||
|
void *aux UNUSED)
|
||||||
|
{
|
||||||
|
struct thread *a = list_entry (a_, struct thread, elem);
|
||||||
|
struct thread *b = list_entry (b_, struct thread, elem);
|
||||||
|
|
||||||
|
return a->priority > b->priority;
|
||||||
|
}
|
||||||
|
|
||||||
/* Sets the current thread's priority to NEW_PRIORITY. */
|
/* Sets the current thread's priority to NEW_PRIORITY. */
|
||||||
void
|
void
|
||||||
thread_set_priority (int new_priority)
|
thread_set_priority (int new_priority)
|
||||||
{
|
{
|
||||||
if (thread_mlfqs)
|
ASSERT (PRI_MIN <= new_priority && new_priority <= PRI_MAX);
|
||||||
return;
|
int old_priority = thread_get_priority ();
|
||||||
|
|
||||||
thread_current ()->priority = new_priority;
|
thread_current ()->priority = new_priority;
|
||||||
|
if (new_priority < old_priority)
|
||||||
|
thread_yield ();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Returns the current thread's priority. */
|
/* Returns the current thread's priority. */
|
||||||
@@ -408,54 +393,35 @@ thread_get_priority (void)
|
|||||||
return thread_current ()->priority;
|
return thread_current ()->priority;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Updates recent_cpu for a thread. */
|
|
||||||
static void
|
|
||||||
thread_update_recent_cpu (struct thread *t, void *aux UNUSED)
|
|
||||||
{
|
|
||||||
fp32_t curr_recent_cpu = t->recent_cpu;
|
|
||||||
fp32_t dbl_load_avg = fp_mul_int (load_avg, 2);
|
|
||||||
fp32_t recent_cpu_coeff
|
|
||||||
= fp_div (dbl_load_avg, fp_add_int (dbl_load_avg, 1));
|
|
||||||
t->recent_cpu
|
|
||||||
= fp_add_int (fp_mul (recent_cpu_coeff, curr_recent_cpu), t->nice);
|
|
||||||
// recent_cpu was updated, update priority.
|
|
||||||
t->priority = calculate_bsd_priority (t->recent_cpu, t->nice);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Sets the current thread's nice value to NICE. */
|
/* Sets the current thread's nice value to NICE. */
|
||||||
void
|
void
|
||||||
thread_set_nice (int nice)
|
thread_set_nice (int nice UNUSED)
|
||||||
{
|
{
|
||||||
ASSERT (NICE_MIN <= nice && nice <= NICE_MAX);
|
/* Not yet implemented. */
|
||||||
|
|
||||||
struct thread *t = thread_current ();
|
|
||||||
t->nice = nice;
|
|
||||||
int priority = calculate_bsd_priority (t->recent_cpu, t->nice);
|
|
||||||
struct thread *next_t
|
|
||||||
= list_entry (list_begin (&ready_list), struct thread, elem);
|
|
||||||
if (priority < next_t->priority)
|
|
||||||
thread_yield ();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Returns the current thread's nice value. */
|
/* Returns the current thread's nice value. */
|
||||||
int
|
int
|
||||||
thread_get_nice (void)
|
thread_get_nice (void)
|
||||||
{
|
{
|
||||||
return thread_current ()->nice;
|
/* Not yet implemented. */
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Returns 100 times the system load average. */
|
/* Returns 100 times the system load average. */
|
||||||
int
|
int
|
||||||
thread_get_load_avg (void)
|
thread_get_load_avg (void)
|
||||||
{
|
{
|
||||||
return fp_round (fp_mul_int (load_avg, 100));
|
/* Not yet implemented. */
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Returns 100 times the current thread's recent_cpu value. */
|
/* Returns 100 times the current thread's recent_cpu value. */
|
||||||
int
|
int
|
||||||
thread_get_recent_cpu (void)
|
thread_get_recent_cpu (void)
|
||||||
{
|
{
|
||||||
return fp_round (fp_mul_int (thread_current ()->recent_cpu, 100));
|
/* Not yet implemented. */
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Idle thread. Executes when no other thread is ready to run.
|
/* Idle thread. Executes when no other thread is ready to run.
|
||||||
@@ -531,8 +497,7 @@ is_thread (struct thread *t)
|
|||||||
/* Does basic initialization of T as a blocked thread named
|
/* Does basic initialization of T as a blocked thread named
|
||||||
NAME. */
|
NAME. */
|
||||||
static void
|
static void
|
||||||
init_thread (struct thread *t, const char *name, int nice, int priority,
|
init_thread (struct thread *t, const char *name, int priority)
|
||||||
fp32_t recent_cpu)
|
|
||||||
{
|
{
|
||||||
enum intr_level old_level;
|
enum intr_level old_level;
|
||||||
|
|
||||||
@@ -544,10 +509,7 @@ init_thread (struct thread *t, const char *name, int nice, int priority,
|
|||||||
t->status = THREAD_BLOCKED;
|
t->status = THREAD_BLOCKED;
|
||||||
strlcpy (t->name, name, sizeof t->name);
|
strlcpy (t->name, name, sizeof t->name);
|
||||||
t->stack = (uint8_t *) t + PGSIZE;
|
t->stack = (uint8_t *) t + PGSIZE;
|
||||||
t->priority
|
t->priority = priority;
|
||||||
= thread_mlfqs ? calculate_bsd_priority (recent_cpu, nice) : priority;
|
|
||||||
t->nice = nice;
|
|
||||||
t->recent_cpu = recent_cpu;
|
|
||||||
t->magic = THREAD_MAGIC;
|
t->magic = THREAD_MAGIC;
|
||||||
|
|
||||||
old_level = intr_disable ();
|
old_level = intr_disable ();
|
||||||
@@ -628,30 +590,8 @@ thread_schedule_tail (struct thread *prev)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Calculates BSD priority for a thread */
|
/* Schedules a new process. At entry, interrupts must be off and
|
||||||
static int
|
the running process's state must have been changed from
|
||||||
calculate_bsd_priority (fp32_t recent_cpu, int nice)
|
|
||||||
{
|
|
||||||
int priority = PRI_MAX - (fp_round (recent_cpu) / 4) - (nice * 2);
|
|
||||||
if (priority < PRI_MIN)
|
|
||||||
return PRI_MIN;
|
|
||||||
if (priority > PRI_MAX)
|
|
||||||
return PRI_MAX;
|
|
||||||
return priority;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Returns true if thread a's priority is strictly greater than
|
|
||||||
thread b's priority. */
|
|
||||||
static bool
|
|
||||||
thread_priority_less (const struct list_elem *a, const struct list_elem *b,
|
|
||||||
void *aux UNUSED)
|
|
||||||
{
|
|
||||||
struct thread *ta = list_entry (a, struct thread, elem);
|
|
||||||
struct thread *tb = list_entry (b, struct thread, elem);
|
|
||||||
return ta->priority > tb->priority;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ss's state must have been changed from
|
|
||||||
running to some other state. This function finds another
|
running to some other state. This function finds another
|
||||||
thread to run and switches to it.
|
thread to run and switches to it.
|
||||||
|
|
||||||
@@ -687,6 +627,17 @@ allocate_tid (void)
|
|||||||
return tid;
|
return tid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Returns true iff the priority of the first list element's thread is greater
|
||||||
|
than that of the second list element's thread. */
|
||||||
|
bool
|
||||||
|
thread_priority_greater (const struct list_elem *a, const struct list_elem *b,
|
||||||
|
void *aux UNUSED)
|
||||||
|
{
|
||||||
|
struct thread *ta = list_entry (a, struct thread, elem);
|
||||||
|
struct thread *tb = list_entry (b, struct thread, elem);
|
||||||
|
return ta->priority > tb->priority;
|
||||||
|
}
|
||||||
|
|
||||||
/* Offset of `stack' member within `struct thread'.
|
/* Offset of `stack' member within `struct thread'.
|
||||||
Used by switch.S, which can't figure it out on its own. */
|
Used by switch.S, which can't figure it out on its own. */
|
||||||
uint32_t thread_stack_ofs = offsetof (struct thread, stack);
|
uint32_t thread_stack_ofs = offsetof (struct thread, stack);
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
#include <debug.h>
|
#include <debug.h>
|
||||||
#include <list.h>
|
#include <list.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include "threads/fixed-point.h"
|
|
||||||
|
|
||||||
/* States in a thread's life cycle. */
|
/* States in a thread's life cycle. */
|
||||||
enum thread_status
|
enum thread_status
|
||||||
@@ -25,10 +24,6 @@ typedef int tid_t;
|
|||||||
#define PRI_DEFAULT 31 /* Default priority. */
|
#define PRI_DEFAULT 31 /* Default priority. */
|
||||||
#define PRI_MAX 63 /* Highest priority. */
|
#define PRI_MAX 63 /* Highest priority. */
|
||||||
|
|
||||||
#define NICE_MIN -20 /* Lowest niceness. */
|
|
||||||
#define NICE_DEFAULT 0 /* Default niceness. */
|
|
||||||
#define NICE_MAX 20 /* Highest niceness. */
|
|
||||||
|
|
||||||
/* A kernel thread or user process.
|
/* A kernel thread or user process.
|
||||||
|
|
||||||
Each thread structure is stored in its own 4 kB page. The
|
Each thread structure is stored in its own 4 kB page. The
|
||||||
@@ -98,10 +93,6 @@ struct thread
|
|||||||
/* Shared between thread.c and synch.c. */
|
/* Shared between thread.c and synch.c. */
|
||||||
struct list_elem elem; /* List element. */
|
struct list_elem elem; /* List element. */
|
||||||
|
|
||||||
/* MLFQS items */
|
|
||||||
int nice; /* Nice value for this thread */
|
|
||||||
fp32_t recent_cpu; /* Amount of time this process received */
|
|
||||||
|
|
||||||
#ifdef USERPROG
|
#ifdef USERPROG
|
||||||
/* Owned by userprog/process.c. */
|
/* Owned by userprog/process.c. */
|
||||||
uint32_t *pagedir; /* Page directory. */
|
uint32_t *pagedir; /* Page directory. */
|
||||||
@@ -140,6 +131,8 @@ void thread_yield (void);
|
|||||||
typedef void thread_action_func (struct thread *t, void *aux);
|
typedef void thread_action_func (struct thread *t, void *aux);
|
||||||
void thread_foreach (thread_action_func *, void *);
|
void thread_foreach (thread_action_func *, void *);
|
||||||
|
|
||||||
|
bool priority_more (const struct list_elem *a_, const struct list_elem *b_,
|
||||||
|
void *aux UNUSED);
|
||||||
int thread_get_priority (void);
|
int thread_get_priority (void);
|
||||||
void thread_set_priority (int);
|
void thread_set_priority (int);
|
||||||
|
|
||||||
@@ -148,4 +141,7 @@ void thread_set_nice (int);
|
|||||||
int thread_get_recent_cpu (void);
|
int thread_get_recent_cpu (void);
|
||||||
int thread_get_load_avg (void);
|
int thread_get_load_avg (void);
|
||||||
|
|
||||||
|
/* Returns true iff the priority of the first list element's thread is greater
|
||||||
|
than that of the second list element's thread. */
|
||||||
|
list_less_func thread_priority_greater;
|
||||||
#endif /* threads/thread.h */
|
#endif /* threads/thread.h */
|
||||||
|
|||||||
Reference in New Issue
Block a user