Download presentation
Presentation is loading. Please wait.
Published byAmelia Welch Modified over 9 years ago
1
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.comwww.iUniverse.comwww.barnesandnoble.comwww.amazon.com
2
2 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
3
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 3
4
Multithread process 4 Shared Items Private items … Process Thread 1 Thread 2 Thread n
5
Sibling threads common setting 5 SubjectDescription Address spaceCode, (parts of) data, and (parts of) results Open filesOpen files, file attributes, and means of referencing files ResourcesTapes, CDs, and many other hardware or software resources that are assigned to the process
6
Thread attributes 6 Thread identification information Thread ID Thread state information Thread-visible registers Location counter Condition code flags Stack pointers Thread control information Base priority Dynamic priority Thread processor affinity Thread execution time Suspension count Impersonation token Alert status Termination port Thread exit status
7
Process attributes in thread-based systems 7 Process identification information Process ID Process control information Security descriptor Base priority Default processor affinity Quota limit Execution time I/O counters VM operation counters Exception/debugging port Exit status
8
Thread state transition diagram in Windows 8 Standby Ready Waiting Transition Running Windows thread states The Corresponding state in the 3-state state model Ready Blocked Running Terminated Block/Suspend Run Assign resource Choose to run Preempt Unblock, resource assigned Completed Unblock, resource not assigned
9
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 9
10
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 10
11
What are the Tools for Creating a Thread? #include 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; } 11
12
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. 12
13
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 13
14
14 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
15
15 Any questions?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.