-
Notifications
You must be signed in to change notification settings - Fork 1
Project 02. Light weight Process (milestone1)
Yooniversal edited this page Jul 26, 2022
·
1 revision
It is a program running in the computer and has many threads which work about the program. In context switch between processes, have to go through scheduler.
It is independent execution unit of the process and share resources with other threads of same group. Because of sharing resources among threads in same process except context
and stack
, It is called light weight process(LWP). In context switch between LWPs, it's not go through the scheduler but just change context
and sp
which are independent contents each LWP has.
- pthread_create -
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *),void *arg)
- Make new thread in the process
- Return the thread id in paramter
thread
- Execute
start_routine
with parameterarg
- pthread_join -
int pthread_join(pthread_t thread, void **ret_val)
- Wait thread having the same id with parameter
thread
until it is terminated by callingpthread_exit()
and free the memory - Pass return value of thread to
ret_val
- Wait thread having the same id with parameter
- pthread_exit -
void pthread_exit(void *ret_val)
- Terminate the thread
- Pass return value of thread to
ret_val
- Create -
int thread_create(thread_t * thread, void * (*start_routine)(void *), void *arg)
- It is the function that makes a thread within the process
- Set thread id in parameter
thread
- Use independent user stack allocated by
allockuvm()
, new context and share rest of data - Set eip of trap frame as
start_routine
with argumentarg
start_routine
is executed after terminatingthread_create
- Return 0 if successful, or non-zero otherwise
- Exit -
void thread_exit(void *retval)
- It is the function that terminates the thread but it is not cleared right after calling
thread_exit()
- Change
state
of current thread toZOMBIE
that makes being searched bythread_join()
- Set
retval
as return value of the thread
- It is the function that terminates the thread but it is not cleared right after calling
- Join -
int thread_join(thread_t thread, void **retval)
- It is the function that searches the thread which has the same thread id with parameter
thread
- If thread's state is not
ZOMBIE
, wait until thread's state is changed toZOMBIE
(terminated bythread_exit()
)
If its state isZOMBIE
, clear up the resources - Set
retval
as return value of the thread - Return 0 if successful, or non-zero otherwise
- It is the function that searches the thread which has the same thread id with parameter
- exit
- Threads will be cleaned up with
thread_join()
and process is cleaned up with original method ofexit()
thereafter
- Threads will be cleaned up with
- fork
- Process is created with original method of
fork()
and thread of the process has same address space by copying what the process has - Set pid as the pid of whether process or thread who calls
- Process is created with original method of
- exec
- For using new process, all threads are cleaned up and process is cleaned up thereafter
- sbrk
- Prevent working
sbrk()
simultaneously from multiple threads by using lock
- Prevent working
- kill
- If a thread is killed, kill all threads in the process and clean up the resources
- The killed threads should not be survived for a long time
- sleep
- Change state of thread to
SLEEPING
but it doesn't influence the process which thread belongs to
- Change state of thread to
- pipe
- Not much different whether interact with process and thread or not
- MLFQ and Stride scheduler doesn't pick thread but process as we saw in project 1
Threads are not go through the scheduler in context switch but changecontext
andsp
- After selecting the process to run, threads of the process do work following Round Robin policy
- time quantum of thread : 1 tick
- In MLFQ, all threads within the same group share their time quantum and time allotment
- In Stride queue, all threads within the same group share their time quantum
- If thread calls
set_cpu_share()
, all threads within the same group have to managed by the stride scheduler