180 likes | 245 Views
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
E N D
Operating SystemsChapter 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 • 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)
User Threads • Thread Management Done by User-Level Threads Library • Examples - POSIX Pthreads - Mach C-threads - Solaris threads - Windows NT - Fibers
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
Kernel Threads • Supported by the Kernel • Examples - Windows 95/98/NT - Solaris - Digital UNIX
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
Many-to-One • Many User-Level Threads Mapped to Single Kernel Thread. • Used on Systems That Do Not Support Kernel Threads.
One-to-One • Each User-Level Thread Maps to Kernel Thread. • Examples - Windows 95/98/NT - OS/2
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.
Threads with WindowsNT Win32API • CreateProcess • CreateThread • CreateFiber • ExitProcess • ExitThread • ExitFiber • WaitForSingleObject
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)
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
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);
/* 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