lesson-2 - devi ahilya vishwavidyalaya€¦ · memory from which the pages can be swapped into the...

28
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

Upload: dangnga

Post on 25-Apr-2018

217 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Lesson-2 - Devi Ahilya Vishwavidyalaya€¦ · memory from which the pages can be swapped into the physical primary memory during running of the process. ... End of Lesson 2 of Chapter

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

Page 2: Lesson-2 - Devi Ahilya Vishwavidyalaya€¦ · memory from which the pages can be swapped into the physical primary memory during running of the process. ... End of Lesson 2 of Chapter

2008 Chapter-7 L2: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.

2

Thread ConceptsThread Concepts

Page 3: Lesson-2 - Devi Ahilya Vishwavidyalaya€¦ · memory from which the pages can be swapped into the physical primary memory during running of the process. ... End of Lesson 2 of Chapter

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

Page 4: Lesson-2 - Devi Ahilya Vishwavidyalaya€¦ · memory from which the pages can be swapped into the physical primary memory during running of the process. ... End of Lesson 2 of Chapter

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

Page 5: Lesson-2 - Devi Ahilya Vishwavidyalaya€¦ · memory from which the pages can be swapped into the physical primary memory during running of the process. ... End of Lesson 2 of Chapter

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

Page 6: Lesson-2 - Devi Ahilya Vishwavidyalaya€¦ · memory from which the pages can be swapped into the physical primary memory during running of the process. ... End of Lesson 2 of Chapter

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 …

Page 7: Lesson-2 - Devi Ahilya Vishwavidyalaya€¦ · memory from which the pages can be swapped into the physical primary memory during running of the process. ... End of Lesson 2 of Chapter

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.

Page 8: Lesson-2 - Devi Ahilya Vishwavidyalaya€¦ · memory from which the pages can be swapped into the physical primary memory during running of the process. ... End of Lesson 2 of Chapter

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.

Page 9: Lesson-2 - Devi Ahilya Vishwavidyalaya€¦ · memory from which the pages can be swapped into the physical primary memory during running of the process. ... End of Lesson 2 of Chapter

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.

Page 10: Lesson-2 - Devi Ahilya Vishwavidyalaya€¦ · memory from which the pages can be swapped into the physical primary memory during running of the process. ... End of Lesson 2 of Chapter

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

Page 11: Lesson-2 - Devi Ahilya Vishwavidyalaya€¦ · memory from which the pages can be swapped into the physical primary memory during running of the process. ... End of Lesson 2 of Chapter

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

Page 12: Lesson-2 - Devi Ahilya Vishwavidyalaya€¦ · memory from which the pages can be swapped into the physical primary memory during running of the process. ... End of Lesson 2 of Chapter

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.

Page 13: Lesson-2 - Devi Ahilya Vishwavidyalaya€¦ · memory from which the pages can be swapped into the physical primary memory during running of the process. ... End of Lesson 2 of Chapter

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.

Page 14: Lesson-2 - Devi Ahilya Vishwavidyalaya€¦ · memory from which the pages can be swapped into the physical primary memory during running of the process. ... End of Lesson 2 of Chapter

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

Page 15: Lesson-2 - Devi Ahilya Vishwavidyalaya€¦ · memory from which the pages can be swapped into the physical primary memory during running of the process. ... End of Lesson 2 of Chapter

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

Page 16: Lesson-2 - Devi Ahilya Vishwavidyalaya€¦ · memory from which the pages can be swapped into the physical primary memory during running of the process. ... End of Lesson 2 of Chapter

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.

Page 17: Lesson-2 - Devi Ahilya Vishwavidyalaya€¦ · memory from which the pages can be swapped into the physical primary memory during running of the process. ... End of Lesson 2 of Chapter

2008 Chapter-7 L2: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.

17

Thread Parameters and StackThread Parameters and Stack

Page 18: Lesson-2 - Devi Ahilya Vishwavidyalaya€¦ · memory from which the pages can be swapped into the physical primary memory during running of the process. ... End of Lesson 2 of Chapter

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

Page 19: Lesson-2 - Devi Ahilya Vishwavidyalaya€¦ · memory from which the pages can be swapped into the physical primary memory during running of the process. ... End of Lesson 2 of Chapter

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

Page 20: Lesson-2 - Devi Ahilya Vishwavidyalaya€¦ · memory from which the pages can be swapped into the physical primary memory during running of the process. ... End of Lesson 2 of Chapter

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

Page 21: Lesson-2 - Devi Ahilya Vishwavidyalaya€¦ · memory from which the pages can be swapped into the physical primary memory during running of the process. ... End of Lesson 2 of Chapter

2008 Chapter-7 L2: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.

21

Thread and Task Thread and Task

Page 22: Lesson-2 - Devi Ahilya Vishwavidyalaya€¦ · memory from which the pages can be swapped into the physical primary memory during running of the process. ... End of Lesson 2 of Chapter

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.

Page 23: Lesson-2 - Devi Ahilya Vishwavidyalaya€¦ · memory from which the pages can be swapped into the physical primary memory during running of the process. ... End of Lesson 2 of Chapter

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.

Page 24: Lesson-2 - Devi Ahilya Vishwavidyalaya€¦ · memory from which the pages can be swapped into the physical primary memory during running of the process. ... End of Lesson 2 of Chapter

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

Page 25: Lesson-2 - Devi Ahilya Vishwavidyalaya€¦ · memory from which the pages can be swapped into the physical primary memory during running of the process. ... End of Lesson 2 of Chapter

2008 Chapter-7 L2: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.

25

SummarySummary

Page 26: Lesson-2 - Devi Ahilya Vishwavidyalaya€¦ · memory from which the pages can be swapped into the physical primary memory during running of the process. ... End of Lesson 2 of Chapter

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.

Page 27: Lesson-2 - Devi Ahilya Vishwavidyalaya€¦ · memory from which the pages can be swapped into the physical primary memory during running of the process. ... End of Lesson 2 of Chapter

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.

Page 28: Lesson-2 - Devi Ahilya Vishwavidyalaya€¦ · memory from which the pages can be swapped into the physical primary memory during running of the process. ... End of Lesson 2 of Chapter

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