15
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

Operating System Concepts and Techniques Lecture 4 Thread M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and Techniques, First ed.,

Embed Size (px)

Citation preview

Page 1: Operating System Concepts and Techniques Lecture 4 Thread M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and Techniques, First ed.,

Operating System Concepts and

Techniques Lecture 4

Thread

M. Naghibzadeh

ReferenceM. Naghibzadeh, Operating System Concepts and Techniques, First ed., iUniverse Inc., 2011.

To order: www.iUniverse.com, www.barnesandnoble.com, or www.amazon.com

Page 2: Operating System Concepts and Techniques Lecture 4 Thread M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and Techniques, First ed.,

2

ThreadAnother entity which can run programs

using CPU...Memory efficient when creating many

entities from one program, simultaneouslySibling thread share the same executable

fileSibling threads share resources hence

reduce competitionSometimes called Light weight process

because its creation takes less time than process

Page 3: Operating System Concepts and Techniques Lecture 4 Thread M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and Techniques, First ed.,

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

Page 4: Operating System Concepts and Techniques Lecture 4 Thread M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and Techniques, First ed.,

Multithread process

4

Shared Items

Private items

Private items

Private items

Process

Thread 1

Thread 2

Thread n

Page 5: Operating System Concepts and Techniques Lecture 4 Thread M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and Techniques, First ed.,

Sibling threads common setting

5

Subject Description

Address space Code, (parts of) data, and (parts of) results

Open files Open files, file attributes, and means of referencing files

Resources Tapes, CDs, and many other hardware or software

resources that are assigned to the process

Page 6: Operating System Concepts and Techniques Lecture 4 Thread M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and Techniques, First ed.,

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

Page 7: Operating System Concepts and Techniques Lecture 4 Thread M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and Techniques, First ed.,

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

Page 8: Operating System Concepts and Techniques Lecture 4 Thread M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and Techniques, First ed.,

Thread state transition diagram in Windows

8

StandbyReady

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

Page 9: Operating System Concepts and Techniques Lecture 4 Thread M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and Techniques, First ed.,

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

Page 10: Operating System Concepts and Techniques Lecture 4 Thread M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and Techniques, First ed.,

How is a Thread Created?Make sure the total number of threads created

so far has not reached the limitAllocate a unique thread identificationAllocate space for the Thread Control

Block(TCB) and initialize appropriate fieldsAllocate space for thread context

Allocate space for other required structures such as stacks and stack pointers and initialize

proper fieldsPut the thread in one of the queues

corresponding to the thread state

10

Page 11: Operating System Concepts and Techniques Lecture 4 Thread M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and Techniques, First ed.,

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

11

Page 12: Operating System Concepts and Techniques Lecture 4 Thread M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and Techniques, First ed.,

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

Page 13: Operating System Concepts and Techniques Lecture 4 Thread M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and Techniques, First ed.,

SummaryThreads, in a thread-based operating system, are active

objects to rum programsThread 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 othersAnother difficulty is having a thread change a global variable

while its sibling threads are unaware of

13

Page 14: Operating System Concepts and Techniques Lecture 4 Thread M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and Techniques, First ed.,

14

Find outThe difference between copy-on-write method in Unix with thread concept in

WindowsHow you can create a thread in LinuxHow you can create a thread in UnixThe differences between Unix thread

and Windows threadWhy thread is called light weight

processBenefits of thread compared to process

Page 15: Operating System Concepts and Techniques Lecture 4 Thread M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and Techniques, First ed.,

15

Any questions?