<<

Processes, Threads and Tasks, Inter- Communication and their :

Lesson-1: Processes and Threads

Chapter-9 L1: "Embedded Systems - Architecture, Programming and Design", 2015 1 Raj Kamal, Publs.: McGraw-Hill Education 1. Process

Chapter-9 L1: "Embedded Systems - Architecture, Programming and Design", 2015 2 Raj Kamal, Publs.: McGraw-Hill Education Process

 A process consists of executable program (codes), of which is controlled by OS,  The state during running of a process─ represented by process-status (running, blocked, or finished), process- structure—its data, objects and resources, and process control block (PCB).

Chapter-9 L1: "Embedded Systems - Architecture, Programming and Design", 2015 3 Raj Kamal, Publs.: McGraw-Hill Education State of a Process

 Represented by process-status  State of running or blocked or waiting or finished  Process-structure  its data, objects and resources, and  process control block (PCB).

Chapter-9 L1: "Embedded Systems - Architecture, Programming and Design", 2015 4 Raj Kamal, Publs.: McGraw-Hill Education Process

Chapter-9 L1: "Embedded Systems - Architecture, Programming and Design", 2015 5 Raj Kamal, Publs.: McGraw-Hill Education Process….

• Runs when it is scheduled to run by the OS (kernel) • OS gives the control of the CPU and resources on on a process’s request • Runs by executing the instructions and the continuous changes of its state takes place as the (PC) changes Chapter-9 L1: "Embedded Systems - Architecture, Programming and Design", 2015 6 Raj Kamal, Publs.: McGraw-Hill Education Process … • Process is that executing unit of computation, which is controlled by • (i) a process (of the OS) mechanism that lets it execute on the CPU, • (ii) a process resource-management mechanism that lets it use the system- memory and other system-resources such as network, file, display or printer Chapter-9 L1: "Embedded Systems - Architecture, Programming and Design", 2015 7 Raj Kamal, Publs.: McGraw-Hill Education A program can be said to consist of number of processes

Chapter-9 L1: "Embedded Systems - Architecture, Programming and Design", 2015 8 Raj Kamal, Publs.: McGraw-Hill Education Example ─ Mobile Phone Device embedded software

 Software highly complex.

 Number of functions, ISRs, processes threads, multiple physical and virtual device drivers, and several program objects that must be concurrently processed on a single .

Chapter-9 L1: "Embedded Systems - Architecture, Programming and Design", 2015 9 Raj Kamal, Publs.: McGraw-Hill Education Exemplary processes at the phone device  Voice encoding and convoluting process─ the device captures the spoken words through a speaker and generates the digital signals after analog to digital conversion, the digits are encoded and convoluted using a CODEC,  Modulating process,  Display process,

Chapter-9 L1: "Embedded Systems - Architecture, Programming and Design", 2015 10 Raj Kamal, Publs.: McGraw-Hill Education Exemplary processes at the phone device

 GUIs (graphic user interfaces), and

 Key input process ─ for provisioning of the user

Chapter-9 L1: "Embedded Systems - Architecture, Programming and Design", 2015 11 Raj Kamal, Publs.: McGraw-Hill Education 2. Process Control Block

Chapter-9 L1: "Embedded Systems - Architecture, Programming and Design", 2015 12 Raj Kamal, Publs.: McGraw-Hill Education Process Control Block

 Consists of the information about the

 A data structure having the information of the process (not accessible to other process) other than the kernel

 OS controls the state of the process using that information

 The information stores in protected memory area Chapter-9 L1: "Embedded Systems - Architecture, Programming and Design", 2015 13 Raj Kamal, Publs.: McGraw-Hill Education Information about the process state at Process Control Block…

 Process ID,

 process priority,

(if any),

(if any), and

 address to the next process PCB which will run,

Chapter-9 L1: "Embedded Systems - Architecture, Programming and Design", 2015 14 Raj Kamal, Publs.: McGraw-Hill Education Information about the process state at Process Control Block…

 allocated program blocks in physical memory and in secondary (virtual) memory for the process-codes,

 allocated process-specific data address- blocks

Chapter-9 L1: "Embedded Systems - Architecture, Programming and Design", 2015 15 Raj Kamal, Publs.: McGraw-Hill Education Information about the process state at Process Control Block…

 allocated process-heap (data generated during the program run) addresses,

 allocated process-stack addresses for the functions called during running of the process,

Chapter-9 L1: "Embedded Systems - Architecture, Programming and Design", 2015 16 Raj Kamal, Publs.: McGraw-Hill Education Information about the process state at Process Control Block…

 allocated addresses of CPU register-save area as a process represents by CPU registers, which include the program counter and stack pointer  allocated addresses of CPU register-save area as a process context

[Register-contents (define process context) include the program counter and stack pointer contents]

Chapter-9 L1: "Embedded Systems - Architecture, Programming and Design", 2015 17 Raj Kamal, Publs.: McGraw-Hill Education Information about the process state at Process Control Block…

 process-state mask [when mask is set to 0 (active) the process is inhibited from running and when reset to 1, the process is allowed to run],

 Signals (messages) dispatch table [process IPC functions],

Chapter-9 L1: "Embedded Systems - Architecture, Programming and Design", 2015 18 Raj Kamal, Publs.: McGraw-Hill Education Information about the process state at Process Control Block…

 OS allocated resources’ descriptors (for example, file descriptors for open files, device descriptors for open (accessible) devices, device-buffer addresses and status, socket-descriptor for open socket), and

 Security restrictions and permissions.

Chapter-9 L1: "Embedded Systems - Architecture, Programming and Design", 2015 19 Raj Kamal, Publs.: McGraw-Hill Education Context  Context loads into the CPU registers from memory when process starts running, and the registers save at the addresses of register-save area on the to another process

Chapter-9 L1: "Embedded Systems - Architecture, Programming and Design", 2015 20 Raj Kamal, Publs.: McGraw-Hill Education Context

 The present CPU registers, which include program counter and stack pointer are called context

 When context saves on the PCB pointed process-stack and register-save area addresses, then the running process stops.

 Other process context now loads and that process runs─ This means that the context has switched

Chapter-9 L1: "Embedded Systems - Architecture, Programming and Design", 2015 21 Raj Kamal, Publs.: McGraw-Hill Education

Chapter-9 L1: "Embedded Systems - Architecture, Programming and Design", 2015 22 Raj Kamal, Publs.: McGraw-Hill Education Thread

 A thread runs like a process and 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 Chapter-9 L2: "Embedded Systems - Architecture, Programming and Design", 2015 23 Raj Kamal, Publs.: McGraw-Hill Education Thread State

 thread-status (running, blocked, or finished)

 Thread Data, objects and a subset of the process resources, and

 Thread-stack

Chapter-9 L2: "Embedded Systems - Architecture, Programming and Design", 2015 24 Raj Kamal, Publs.: McGraw-Hill Education Threads of a Process Process Structure

Chapter-9 L2: "Embedded Systems - Architecture, Programming and Design", 2015 25 Raj Kamal, Publs.: McGraw-Hill Education Thread… lightweight • Considered a lightweight process and a process level controlled entity.

[Light weight means its running does not depend on system resources]

Chapter-9 L2: "Embedded Systems - Architecture, Programming and Design", 2015 26 Raj Kamal, Publs.: McGraw-Hill Education Process… heavyweight • 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]

Chapter-9 L2: "Embedded Systems - Architecture, Programming and Design", 2015 27 Raj Kamal, Publs.: McGraw-Hill Education An Application program can be said to consist of number of threads

Chapter-9 L2: "Embedded Systems - Architecture, Programming and Design", 2015 28 Raj Kamal, Publs.: McGraw-Hill Education A process can be said to consist of number of threads in , and Other OSes

Chapter-9 L2: "Embedded Systems - Architecture, Programming and Design", 2015 29 Raj Kamal, Publs.: McGraw-Hill Education 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.

Chapter-9 L2: "Embedded Systems - Architecture, Programming and Design", 2015 30 Raj Kamal, Publs.: McGraw-Hill Education Example ─ Multiple 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.

Chapter-9 L2: "Embedded Systems - Architecture, Programming and Design", 2015 31 Raj Kamal, Publs.: McGraw-Hill Education Exemplary threads of display_process at the phone device  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

Chapter-9 L2: "Embedded Systems - Architecture, Programming and Design", 2015 32 Raj Kamal, Publs.: McGraw-Hill Education Exemplary processes at the phone device

 Display_Menu thread ─ for displaying menu.

 Display threads can share the common memory blocks and resources allocated to the Display_Process.

Chapter-9 L2: "Embedded Systems - Architecture, Programming and Design", 2015 33 Raj Kamal, Publs.: McGraw-Hill Education Minimum computational unit

 A display thread is now the minimum computational unit controlled by the OS.

Chapter-9 L2: "Embedded Systems - Architecture, Programming and Design", 2015 34 Raj Kamal, Publs.: McGraw-Hill Education Thread Parameters and Stack

Chapter-9 L2: "Embedded Systems - Architecture, Programming and Design", 2015 35 Raj Kamal, Publs.: McGraw-Hill Education Thread parameters

 Each thread has independent parameters ID, priority, program counter, stack pointer, CPU registers and its present status. • Thread states─ starting, running, blocked () and finished

Chapter-9 L2: "Embedded Systems - Architecture, Programming and Design", 2015 36 Raj Kamal, Publs.: McGraw-Hill Education Thread’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

Chapter-9 L2: "Embedded Systems - Architecture, Programming and Design", 2015 37 Raj Kamal, Publs.: McGraw-Hill Education 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

Chapter-9 L2: "Embedded Systems - Architecture, Programming and Design", 2015 38 Raj Kamal, Publs.: McGraw-Hill Education Thread and

Chapter-9 L2: "Embedded Systems - Architecture, Programming and Design", 2015 39 Raj Kamal, Publs.: McGraw-Hill Education Thread and Task

 Thread is a concept used in 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.

Chapter-9 L2: "Embedded Systems - Architecture, Programming and Design", 2015 40 Raj Kamal, Publs.: McGraw-Hill Education Thread 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.

Chapter-9 L2: "Embedded Systems - Architecture, Programming and Design", 2015 41 Raj Kamal, Publs.: McGraw-Hill Education Thread 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

Chapter-9 L2: "Embedded Systems - Architecture, Programming and Design", 2015 42 Raj Kamal, Publs.: McGraw-Hill Education Summary

Chapter-9 L1: "Embedded Systems - Architecture, Programming and Design", 2015 43 Raj Kamal, Publs.: McGraw-Hill Education We learnt

 Application program can be said to consist of number of processes

 Process defined as that executing unit of of computation that processes on a CPU and state of which is under the control of kernel of an .

Chapter-9 L1: "Embedded Systems - Architecture, Programming and Design", 2015 44 Raj Kamal, Publs.: McGraw-Hill Education We learnt • OS lets a process execute on the CPU─ some process at OS for a resource- management mechanism lets it use the system-memory and other system- resources such as network, file, display or printer

Chapter-9 L1: "Embedded Systems - Architecture, Programming and Design", 2015 45 Raj Kamal, Publs.: McGraw-Hill Education We learnt • PCB─ a data structure having the information using which the OS controls the process state • PCB consists of the information about the process state • PCB stores in protected memory area of the kernel

Chapter-9 L1: "Embedded Systems - Architecture, Programming and Design", 2015 46 Raj Kamal, Publs.: McGraw-Hill Education 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.

Chapter-9 L2: "Embedded Systems - Architecture, Programming and Design", 2015 47 Raj Kamal, Publs.: McGraw-Hill Education 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.

Chapter-9 L2: "Embedded Systems - Architecture, Programming and Design", 2015 48 Raj Kamal, Publs.: McGraw-Hill Education 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

Chapter-9 L2: "Embedded Systems - Architecture, Programming and Design", 2015 49 Raj Kamal, Publs.: McGraw-Hill Education End of Lesson 1 of Chapter 9 on “Processes and Threads”

Chapter-9 L1: "Embedded Systems - Architecture, Programming and Design", 2015 50 Raj Kamal, Publs.: McGraw-Hill Education