150 likes | 155 Views
Learn about thread concepts, creation, and termination in operating systems. Understand the benefits of threads over processes and how to create and manage them. Explore the differences between thread types in Unix and Windows.
E N D
Operating System Concepts and Techniques Lecture 4 Thread M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and Techniques, First ed., iUniverse Inc., 2011. To order: www.iUniverse.com, www.barnesandnoble.com, or www.amazon.com
Thread • Another entity which can run programs using CPU... • Memory efficient when creating many entities from one program, simultaneously • Sibling thread share the same executable file • Sibling threads share resources hence reduce competition • Sometimes called Light weight process because its creation takes less time than process
Thread types • User level: Created and managed by user • OS does not recognize it • Kernel level: created and managed by OS • Users can order its creation • Hybrid: Created by user, managed jointly
Multithread process Process Shared Items Thread 1 Private items Private items Private items Thread n … Thread 2
Windows thread states The Corresponding state in the 3-state state model Transition Unblock, resource not assigned Assign resource Choose to run Ready Ready Standby Unblock, resource assigned Run Waiting Blocked Preempt Block/Suspend Running Running Completed Terminated Thread state transition diagram in Windows
Thread Creation and Termination • Why create threads? • To run programs • When is a thread created? • Whenever a process is created, its primary thread is created • Whenever a running thread, explicitly requests the execution of a service call that creates a thread
How is a Thread Created? • Make sure the total number of threads created so far has not reached the limit • Allocate a unique thread identification • Allocate space for the Thread Control Block(TCB) and initialize appropriate fields • Allocate space for thread context • Allocate space for other required structures such as stacks and stack pointers and initialize proper fields • Put the thread in one of the queues corresponding to the thread state
What are the Tools for Creating a Thread? #include <windows.h> #include <stdio.h> void RegAttendance (void) // Regular attendance { while (1) { Beep (500, 10); // Make a beep sound printf (“ It is the time for the regular patient attendance \n ”); Sleep (15*60*1000); // Sleep for 15 minutes } } void ScheAttendance (void) // Scheduled attendance { int TimeSpan; while (1) { Beep (500, 10); // Make a beep sound printf (“ Enter the time span before next scheduled attendance\n “); scanf (“%d”, &TimeSpan); Sleep (TimeSpan*60*1000); // Sleep for “TimeSpan” minutes } } int main(void) { ANDLE thread1, thread2; thread1=CreateThread(0,0,(LPTHREAD_START_ROUTINE) RegAttendance,NULL,0,0); thread2=CreateThread(0,0,(LPTHREAD_START_ROUTINE) ScheAttendance,NULL,0,0); Sleep(INFINITE); return 0; }
How is a Thread terminated? • All threads within the large body of a process are terminated when the process is terminated • A thread can terminate itself by explicitly calling proper system calls or Win32-API routines such as ExitThread. • By calling Win32-API routines such as TerminateThread form another thread in the same process, or another process if this process has the permission to do so, and has the handle to access the thread.
Summary • Threads, in a thread-based operating system, are active objects to rum programs • Thread methodology boosts operating system efficiency through better utilization of main memory and reduction of competing objects for resources • Thread methodology increases collaboration between related program objects • Threads of a process are not as independent as processes. Threads can share many items such as address space, resources, global variables, etc., • Threading technique has its own difficulties. One thread may try to free a resource although its sibling threads have yet not finished with the resource. A thread may try to close a file that is needed by others • Another difficulty is having a thread change a global variable while its sibling threads are unaware of
Find out • The difference between copy-on-write method in Unix with thread concept in Windows • How you can create a thread in Linux • How you can create a thread in Unix • The differences between Unix thread and Windows thread • Why thread is called light weight process • Benefits of thread compared to process