Compare commits

...

20 Commits

Author SHA1 Message Date
Demetriades, Themis
fd5143110f Merge branch 'task1/merged/priority-scheduling' into 'task1/priority-scheduling'
# Conflicts:
#   .gitignore
#   src/threads/synch.c
#   src/threads/thread.c
2024-10-17 17:58:13 +00:00
Themis Demetriades
e38e1400a2 Update singleton_sema_priority_greater function description for clarity 2024-10-17 09:24:49 +01:00
Themis Demetriades
810a5376b9 Implement priority scheduling for condition variables 2024-10-17 08:55:29 +01:00
sBubshait
88967acdaa Update cond_wait to insert into waiters list in sorted order of priority 2024-10-17 08:53:53 +01:00
sBubshait
e74ee59e17 Update sema priority compare to take aux optionally as priority of first sema 2024-10-17 08:46:14 +01:00
sBubshait
53b296d6c4 Add function to compare the priority of two semaphores 2024-10-17 08:36:29 +01:00
sBubshait
0d6fb2f167 Update sema_up to yield CPU if unblocked has higher priority 2024-10-17 07:15:10 +01:00
sBubshait
24900545d4 Update sema_down to insert into waiters list in priority sorted order 2024-10-17 06:54:10 +01:00
sBubshait
fb268cdef0 Update thread make priority_more public 2024-10-17 06:47:58 +01:00
sBubshait
dbb7fd56e3 Update set priority to reorder the list if priority is changed 2024-10-17 06:38:27 +01:00
sBubshait
949455aae5 Update set priority to eliminate race condition when accessing ready list 2024-10-17 06:35:39 +01:00
sBubshait
5c09ff0149 Updated set priority to yield if priority is lowered 2024-10-17 06:27:59 +01:00
sBubshait
9f53040d27 Update thread_set_priority to not do anything if priority is unchanged 2024-10-17 06:00:34 +01:00
sBubshait
62c8818c05 Update thread_yield to add thread back in sorted order of priority 2024-10-16 08:18:51 +01:00
sBubshait
fda79173c0 Update thread_unblock to maintain priority order in ready_list 2024-10-16 08:14:00 +01:00
sBubshait
1c53790ca7 Update set_priority to ensure that the new priority is within bounds 2024-10-16 08:13:56 +01:00
sBubshait
9bb0b758c8 Fix Error missing semicolon after function signature 2024-10-16 07:40:41 +01:00
sBubshait
8b3f9e353f Update creating thread to yield if the new thread has higher priority 2024-10-16 07:33:24 +01:00
sBubshait
83910f945c Add priority comparator function for thread list sorting 2024-10-16 07:26:02 +01:00
sBubshait
9f71c989a9 Update gitignore to ignore temporary Mac OS files and code editor folders 2024-10-16 07:10:31 +01:00
4 changed files with 132 additions and 11 deletions

8
.gitignore vendored
View File

@@ -4,8 +4,12 @@
#ignore pdf files (just keep source files)
*.pdf
#ignore shell scripts (only used for testing)
*.sh
#ignore Mac OS generated files
.DS_Store
#ignore code editor generated directories
.idea
.vscode
#ignore junk files from latex output
*.out

View File

@@ -68,8 +68,8 @@ sema_down (struct semaphore *sema)
old_level = intr_disable ();
while (sema->value == 0)
{
list_insert_ordered (&sema->waiters, &thread_current ()->elem,
&thread_priority_greater, NULL);
list_insert_ordered(&sema->waiters, &thread_current ()->elem,
priority_more, NULL);
thread_block ();
}
sema->value--;
@@ -114,9 +114,15 @@ sema_up (struct semaphore *sema)
ASSERT (sema != NULL);
old_level = intr_disable ();
if (!list_empty (&sema->waiters))
thread_unblock (list_entry (list_pop_front (&sema->waiters),
struct thread, elem));
/* Wake up (unblock) the highest priority thread from the waiters list */
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++;
intr_set_level (old_level);
thread_yield ();
@@ -255,6 +261,54 @@ struct semaphore_elem
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
allows one piece of code to signal a condition and cooperating
code to receive the signal and act upon it. */
@@ -266,6 +320,38 @@ cond_init (struct condition *cond)
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
some other piece of code. After COND is signaled, LOCK is
reacquired before returning. LOCK must be held before calling
@@ -297,7 +383,14 @@ cond_wait (struct condition *cond, struct lock *lock)
ASSERT (lock_held_by_current_thread (lock));
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);
sema_down (&waiter.semaphore);
lock_acquire (lock);

View File

@@ -221,6 +221,10 @@ thread_create (const char *name, int priority,
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;
}
@@ -257,7 +261,10 @@ thread_unblock (struct thread *t)
old_level = intr_disable ();
ASSERT (t->status == THREAD_BLOCKED);
list_insert_ordered (&ready_list, &t->elem, &thread_priority_greater, NULL);
/* Insert the thread back into the ready list in priority order. */
list_insert_ordered(&ready_list, &t->elem, priority_more, NULL);
t->status = THREAD_READY;
intr_set_level (old_level);
}
@@ -327,9 +334,11 @@ thread_yield (void)
ASSERT (!intr_context ());
old_level = intr_disable ();
/* Insert the thread back into the ready list in priority order. */
if (cur != idle_thread)
list_insert_ordered (&ready_list, &cur->elem, thread_priority_greater,
NULL);
list_insert_ordered(&ready_list, &cur->elem, priority_more, NULL);
cur->status = THREAD_READY;
schedule ();
intr_set_level (old_level);
@@ -352,6 +361,19 @@ 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. */
void
thread_set_priority (int new_priority)

View File

@@ -131,6 +131,8 @@ void thread_yield (void);
typedef void thread_action_func (struct thread *t, void *aux);
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);
void thread_set_priority (int);