1 / 15

Operating System Concepts and Techniques Lecture 4

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.

pcoffey
Download Presentation

Operating System Concepts and Techniques Lecture 4

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 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

  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

  4. Multithread process Process Shared Items Thread 1 Private items Private items Private items Thread n … Thread 2

  5. Sibling threads common setting

  6. Thread attributes

  7. Process attributes in thread-based systems

  8. 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

  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

  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

  11. 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; }

  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.

  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

  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. Any questions?

More Related