1 / 18

Operating Systems Chapter 5

Operating Systems Chapter 5. Threads. Benefits. Responsiveness Resource Sharing Economy Utilization of MP Architectures. Benefits of Threads / Performance implications. Less time to create a thread than a process Less time to terminate thread than a process

haruki
Download Presentation

Operating Systems Chapter 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Operating SystemsChapter 5 Threads

  2. Benefits • Responsiveness • Resource Sharing • Economy • Utilization of MP Architectures

  3. Benefits of Threads / Performance implications • Less time to create a thread than a process • Less time to terminate thread than a process • Less time to switch between two threads within the same process • Threads share memory and files; they can communicate without invoking the kernel • Higher speed (might reach a factor of 10)

  4. Single and Multithreaded Processes

  5. User Threads • Thread Management Done by User-Level Threads Library • Examples - POSIX Pthreads - Mach C-threads - Solaris threads - Windows NT - Fibers

  6. ULTs – Advantages and disadvantages • Advantages: • Thread switching does not require kernel mode privileges; faster • Scheduling is application specific; flexible • ULTs can run on any O.S. • Disadvantages: • System calls done by one thread cause the whole process to be blocked • Cannot take advantage of multi-processors. Kernel assigns each process to one processor

  7. Kernel Threads • Supported by the Kernel • Examples - Windows 95/98/NT - Solaris - Digital UNIX

  8. KLTs – Advantages and disadvantages • Advantages: • Threads do not block each others. Kernel schedules threads independently • Kernel can schedule different threads on different processors • Kernel routines can be multi-threaded • Disadvantage: • Transfer of control within one process requires a kernel mode switch

  9. Many-to-One • Many User-Level Threads Mapped to Single Kernel Thread. • Used on Systems That Do Not Support Kernel Threads.

  10. One-to-One • Each User-Level Thread Maps to Kernel Thread. • Examples - Windows 95/98/NT - OS/2

  11. Many-to-many Model

  12. Solaris Process

  13. Threads implementations in Linux • Linux uses the same internal representation for processes and threads; a thread is simply a new process that happens to share the same address space as its parent. • A distinction is only made when a new thread is created by the clone system call. • fork creates a new process with its own entirely new process context • clone creates a new process with its own identity, but that is allowed to share the data structures of its parent • Using clone gives an application exactly what is shared between two threads.

  14. Threads with WindowsNT Win32API • CreateProcess • CreateThread • CreateFiber • ExitProcess • ExitThread • ExitFiber • WaitForSingleObject

  15. POSIX Threads API • Don’t forget to compile and link with -lpthread • pthread_create – create a new thraed , just like fork create a new process #include <pthread.h> int pthread_create( pthread_t *thread, pthread_attr *attr, void *(*start_routine)(void *) , void *arg)

  16. More PThread Functions #include <pthread.h> void pthread_exit(void *retval); • Terminates a thread like exit does to Process #include <pthread.h> int pthread_join(pthread_t th,void **thread_return); • Just like wait is used to collect child process

  17. Threads in MicroThread /* pointer type to a thread function */ typedef int (far *PThreadFunc) (void *pArg); /* Adds a new thread to the READY queue */ int MTAddNewThread(PThreadFunc pThreadFunc,unsigned priority, unsigned sizeArg, void *pArg); /* Kill a thread */ void MTKillThread(unsigned threadID); /* Kills the current thread */ void MTEndThread(void); /* Kills all known threads dead*/ void MTEndMultiThreading(void); enum MTReturnCode { NORMAL, DEADLOCKED, CTRLBREAK, NOT_INITIALISED,TOO_DEEP_CRITICAL_NEST }; enum /* Starts multi-threading */ MTReturnCode MTStartMultiThreading(void);

  18. /* Thread execution context */ typedef struct { unsigned sp; /* stack pointer */ unsigned ss; /* stack segment */ unsigned priority; /* priority: 1,2,3,4 or 5 */ enum ThreadStatus status; unsigned semaphore; /* blocking semaphore */ void *pOwnStack; PThreadFunc pFunc; /* pointer to the thread function */ void *pArg; /* pointer argument passed to the thread function */ void *pMsg; unsigned msgSize; unsigned long wakeUpTime; /* time to wake the sleeping thread up */ unsigned prevID; /* ID of previous thread in the linked-list queue */ unsigned nextID; /* ID of next thread in the linked-list queue */ } ThreadContext; /* The Thread List - implemented as an array of thread execution contexts so that the thread IDs correspond to the array indices */ ThreadContext threads[MAX_THREADS]; MT – Thread Context

More Related