Project 1. Threads - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Project 1. Threads

Description:

Threads share the process instructions and most of its data. ... Also, when threads are waiting for a lock, semaphore, or condition variable, the ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 14
Provided by: camarsK
Category:
Tags: project | threads

less

Transcript and Presenter's Notes

Title: Project 1. Threads


1
Project 1. Threads
  • Youngjae Lee
  • (Division of Computer Science, KAIST)

2
What is a Thread?
  • A thread of control (or a thread)
  • A sequence of instructions being executed in a
    program.
  • Threads share the process instructions and most
    of its data.
  • A change in shared data by one thread can be seen
    by the other threads in the process
  • Threads also share most of the OS state of a
    process.

threads
1 process
From Prof. Jin-Soo Kims slides
3
To do
  • Alarm Clock
  • Priority Scheduler

4
Alarm Clock
  • To suspend threads for approximately timer ticks
  • Threads call timer_sleep(TICKS) in device/timer.c
  • ex) pthread_create(thread_id, NULL, sleeper,
    tickes)

5
Alarm Clock
  • Requirement
  • Must not be busy waiting
  • timer_sleep() in device/timer.c
  • Now, implemented by busy waiting
  • Reimplement timer.c to avoid busy waiting
  • make thread_list for waiting threads.
  • when timer interrupt occurs, wake threads
  • Timer interrupt occurred every tick
  • ticks
  • timer_interrupt() in device/timer.c called

6
Priority Scheduling
  • Now, Round-Robin scheduling
  • ready_list in thread.c
  • schedule(), next_thread_to_run() in thread.c
  • Priority scheduling
  • Thread which has the highest priority in the
    ready_list gets CPU.

7
Priority Scheduling
  • Requirement
  • If a thread is added to the ready list which has
    a higher priority than the currently running
    thread, immediately yield the processor to the
    new thread.
  • Also, when threads are waiting for a lock,
    semaphore, or condition variable, the highest
    priority waiting thread should be woken up first.
  • Slove the priority inversion problem of Lock.
  • Priority inversion problem -gt effective priority
  • thread_create(), next_thread_to_run()...etc in
    thread.c
  • sema_up(), lock_acquire(), cond_signal()etc in
    synch.c

8
Priority Scheduling
  • Priority inversion problem
  • A situation where a higher-priority job is unable
    to run because a lower-priority job is holding a
    resource it needs, such as a lock
  • Example
  • There are three threads.(H, M, L)
  • H gt M gt L (the order of priority). H, L need
    same resourse(A) to be executed and L is a As
    holder. Execution order expected is L, H, M.
  • In real, H is blocked and M is firstly executed
    because Ms priority is larger than that of L.
    After M is finished, L is executed. Then, after L
    is finished, H is executed.
  • Consequently, the execution order is M, L, H.

9
Priority Scheduling
  • Priority inversion problem
  • A situation where a higher-priority job is unable
    to run because a lower-priority job is holding a
    resource it needs, such as a lock.
  • Solution Priority Inheritance
  • The higher-priority job can donate its priority
    to the lower-priority job holding the resource it
    requires (effective priority).
  • The beneficiary of the inheritance will now have
    a higher scheduling priority and thus get
    scheduled to run sooner.
  • It can finish its work and release the resource,
    at which point the original priority is returned
    to the job.

10
Lock
  • synch.h synch.c
  • Usage

critical section
11
Semaphore
  • synch.h synch.c
  • Usage
  • sema_down()
  • critical section.
  • sema_up()

12
Condition variable
  • synch.h synch.c

13
Tips
  • Strongly RECOMMEND to use ctags
  • Dont make list/hash structure.
  • use list.c/list.h/hash.c/hash.h at lib/kernel/
  • Check progress yourself by using make check
  • alarm-single, alarm-multiple, alarm-priority,
    alarm-zero, alarm-negative, priority-preempt,
    priority-donate-one, priority-donate-multiple,
    priority-donate-nest, priority-change,
    priority-fifo, priority-sema, priority-condvar
  • TAs
  • Noah course board
  • ???? ???, x3585, yjlee_at_camars.kaist.ac.kr
Write a Comment
User Comments (0)
About PowerShow.com