lesson-2 - devi ahilya vishwavidyalaya€¦ · memory from which the pages can be swapped into the...
TRANSCRIPT
2008 Chapter-7 L2: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
1
Inter-Process Communication and Synchronization of Processes,
Threads and Tasks:
Lesson-2: Thread
2008 Chapter-7 L2: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
2
Thread ConceptsThread Concepts
2008 Chapter-7 L2: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
3
ThreadThread� A thread consists of executable
program (codes), state of which is controlled by OS,
� The state information─ thread-status(running, blocked, or finished), thread-structure—its data, objects and a subset of the process resources, and thread-stack
2008 Chapter-7 L2: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
4
• Considered a lightweight process and a process level controlled entity.
[Light weight means its running does not depend on system resources]
Thread… Thread… lightweight
2008 Chapter-7 L2: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
5
• Process considered as a heavyweight process and a kernel-level controlled entity.
• Process thus can have codes in secondary memory from which the pages can be swapped into the physical primary memory during running of the process.
[Heavy weight means its running may depend on system resources]
Process… Process… heavyweight
2008 Chapter-7 L2: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
6
• May have process structure with the virtual memory map, file descriptors, user–ID, etc.
• Can have multiple threads, which share the process structure
Process …Process …
2008 Chapter-7 L2: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
7
Thread Thread � A process or sub-process within a process
that has its own program counter, its own stack pointer and stack, its own priority-parameter for its scheduling by a thread-scheduler
� Its’ variables that load into the processor registers on context switching.
� Has own signal mask at the kernel.
2008 Chapter-7 L2: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
8
Thread’s signal maskThread’s signal mask
� When unmasked lets the thread activate and run.
� When masked, the thread is put into a queue of pending threads.
2008 Chapter-7 L2: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
9
Thread’s StackThread’s Stack
� A thread stack is at a memory address block allocated by the OS.
2008 Chapter-7 L2: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
10
Threads of a Process sharing Process Threads of a Process sharing Process StructureStructure
2008 Chapter-7 L2: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
11
Application program can be said to Application program can be said to consist of number of threads or consist of number of threads or
processesprocesses
2008 Chapter-7 L2: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
12
Multiprocessing OSMultiprocessing OS
� A multiprocessing OS runs more than one processes.
� When a process consists of multiple threads, it is called multithreaded process.
� A thread can be considered as daughter process.
� A thread defines a minimum unit of a multithreaded process that an OS schedules onto the CPU and allocates other system resources.
2008 Chapter-7 L2: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
13
Example ─ Multiple threadsMultiple threads of Display process in Mobile Phone Device
� Display_Time_Date thread ─ for displaying clock time and date.
� Display_Battery thread ─ for displaying battery power.
� Display_Signal thread ─ for displaying signal power for communication with mobile service provider.
2008 Chapter-7 L2: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
14
� Display_Profile thread ─ for displaying silent or sound-active mode. A thread
� Display_Message thread ─ for displaying unread message in the inbox.
� Display_Call Status thread ─for displaying call status; whether dialing or call waiting
Exemplary threads of Exemplary threads of display_processdisplay_process at at the phone devicethe phone device
2008 Chapter-7 L2: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
15
� Display_Menu thread ─ for displaying menu.
� Display threads can share the common memory blocks and resources allocated to the Display_Process.
Exemplary processes at the phone deviceExemplary processes at the phone device
2008 Chapter-7 L2: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
16
Minimum computational unitMinimum computational unit
� A display thread is now the minimum computational unit controlled by the OS.
2008 Chapter-7 L2: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
17
Thread Parameters and StackThread Parameters and Stack
2008 Chapter-7 L2: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
18
Thread parametersThread parameters
� Each thread has independent parameters ID, priority, program counter, stack pointer, CPU registers and its present status.
• Thread states─ starting, running, blocked (sleep) and finished
2008 Chapter-7 L2: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
19
Thread’s stackThread’s stack
� When a function in a thread in OS is called, the calling function state is placed on the stack top.
� When there is return the calling function takes the state information from the stack top
2008 Chapter-7 L2: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
20
Thread Stack Thread Stack � A data structure having the information
using which the OS controls the thread state.
� Stores in protected memory area of the kernel.
� Consists of the information about the thread state
2008 Chapter-7 L2: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
21
Thread and Task Thread and Task
2008 Chapter-7 L2: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
22
Thread and TaskThread and Task� Thread is a concept used in Java or
Unix. � A thread can either be a sub-process
within a process or a process within an application program.
� To schedule the multiple processes, there is the concept of forming thread groups and thread libraries.
2008 Chapter-7 L2: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
23
Thread and taskThread and task� A task is a process and the OS does the
multitasking.� Task is a kernel-controlled entity while
thread is a process-controlled entity.
2008 Chapter-7 L2: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
24
Thread and Task analogyThread and Task analogy� A thread does not call another thread to run.
A task also does not directly call another task to run.
� Multithreading needs a thread-scheduler. Multitasking also needs a task-scheduler.
� There may or may not be task groups and task libraries in a given OS
2008 Chapter-7 L2: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
25
SummarySummary
2008 Chapter-7 L2: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
26
We learnt • A thread is a process or sub-process
within a process that has its own program counter, its own stack pointer and stack, its own priority-parameter for its scheduling by a thread-scheduler.
2008 Chapter-7 L2: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
27
We learnt • Thread is a concept in Java and Unix• Thread is a lightweight sub-process or
process in an application program. • Thread can share a process structure.• Thread has thread stack, at the memory.• It has a unique ID. • States of thread─ starting, running, sleeping
(blocked) and finished.
2008 Chapter-7 L2: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
28
End of Lesson 2 of Chapter 7End of Lesson 2 of Chapter 7