Skip to content

Project 02. Light weight Process (milestone1)

Yooniversal edited this page Jul 26, 2022 · 1 revision

Process / Thread

Process

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.

Thread

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.

POSIX thread

  • 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 parameter arg
  • 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 calling pthread_exit() and free the memory
    • Pass return value of thread to ret_val
  • pthread_exit - void pthread_exit(void *ret_val)
    • Terminate the thread
    • Pass return value of thread to ret_val

Design Basic LWP Operations for xv6

Milestone 2 : Create, Join and Exit

  • 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 argument arg
      start_routine is executed after terminating thread_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 to ZOMBIE that makes being searched by thread_join()
    • Set retval as return value of the thread
  • 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 to ZOMBIE (terminated by thread_exit())
      If its state is ZOMBIE, clear up the resources
    • Set retval as return value of the thread
    • Return 0 if successful, or non-zero otherwise

Milestone 3 : Interaction with other services in xv6

Interaction with system calls in xv6

  • exit
    • Threads will be cleaned up with thread_join() and process is cleaned up with original method of exit() thereafter
  • 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
  • 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
  • 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
  • pipe
    • Not much different whether interact with process and thread or not

Interaction with the MLFQ and Stride schedulers

  • 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 change context and sp
  • 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