580 likes | 915 Views
INTER-PROCESS COMMUNICATION AND SYNCHRONISATION :. Lesson-1: Process, Thread and Task. Application software of an Embedded System consists or number of processes or threads or tasks. 1. Process. Process can be defined as a program unit in execution the state of which is controlled by OS
E N D
INTER-PROCESS COMMUNICATION AND SYNCHRONISATION: Lesson-1: Process, Thread and Task
Application software of an Embedded System consists or number of processes or threads or tasks Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
1. Process • Process can be defined as a program unit in execution the state of which is controlled by OS • OS gives a process the control of CPU either on a process-request or on a system-call Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Operating System Number of processes of a program Process n Process 1 Process 2 Only one process (or its thread) can run at an instance in single CPU system. Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Process is a concept used in Unix OS and many OSs. • System call means a call for running of the process by an OS function. • Process-request means a process sending a request to OS for initiating running of another process. Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
OS has a scheduling function in the system software that lets a process execute at the given instances and • OS has a resource-management function in the system software that allows a process to access the system memory and other resources (for example, access to the network, file, device). Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Process is also defined as computational unit, which is scheduled and runs on the CPU by a kernel. • Kernel controls the creation of the process and states of the process. [Kernel is in the operating system (OS)] Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Process States - The state of a process is represented by its current status (created, activated, running,blocked, delayed or inactivated or deleted) and its context consisting of the process program counter, process stack pointer, data, objects and resource information. Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Process Program Counter - Program counter points to memory address in the process from where next instruction is to be fetched. It thus represent current state of the process. Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Process Stack Pointer - Program stack pointer points to a memory address in the process from where the Pop operations retrieve the saved data or to where the push operations save data. Saving is required when system switches to another process. Retrieving is required when system switches from another process. Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
A process state is defined at a process structure at the memory • The structure is called process control block (PCB). Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
PCB of a process A shared memory block between process and OS Process ID Program Counter Context Present state Process’s Stack Pointer Stack Size Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Multiprocessing operations take place in a system by context switching between the various processes Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
2. Thread • A multitasking or multi-processes OS runs more than one process. Further, a process may consist of one or multiple threads. A thread then defines a minimum unit for a scheduler to schedule the CPU and allocate system resources. Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Thread is a concept used in Unix, Java, Windows CE and many OSs Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
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, has its local variables that load into the processor registers on context switching to the thread. Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Operating System Number of threads of a process Process 1 Process n Thread 1 Thread j Only one thread can run at an instance in a single CPU system. Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Thread - a light weight process • Thread is a light weight process. For example, thread does not require the virtual memory management and other functions of the OS. • Process is a heavy weight process. For example, process may require the memory management functions and other functions of the OS. Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Running of Thread • Thread is a sequentially executing (running) program such that an OS controlling its state and it runs when it is scheduled to run by the OS kernel, which gives the control of the CPU on a process or thread request or on a system call. When a thread is put to sleep (wait) or is waiting for input, the other thread runs. Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Thread states are - started,- running,- sleeping (blocked or waiting) and- finished Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Thread has its own signal mask at the kernel. The signal mask when unmasked lets the thread activate and run. When masked, the thread is put into a queue of pending threads. • Different threads of a process may share a common structure of the process. Multiple threads can thus share the data of process. Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Threads and PCB of a process Process ID and priority A shared memory block at PCB between OS and the threads Signal mask Present state Memory and stack block pointer Kernel stack Memory and Stack Sizes Threads 1 to q stack pointer Priority and states of Threads 1 to q Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Thread Stack Thread ID and priority Signal mask Thread running or blocked Thread Program counter Thread stack Pointer CPU Registers Local variables Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Multithreading operations take place in a system by context switching between the various threads Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
3. Task • An application program can also be defined as a program consisting of the tasks • Various states of task are controlled by an OS. • A task has therefore a similarity to process. Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Task is a concept used in VxWorks, COS and many OSs Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Operating System Number of Tasks of a program Task n Task 1 Task 2 Only one task can run at an instance in single CPU system. Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Task Characteristics • Each task takes control of the CPU when scheduled by a scheduler at the OS. The scheduler controls and runs the tasks. • No task can directly make a call to another task. [It is unlike a C (or C++) function, which can call another function.] Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
A task is an independent process. The OS can block a running task and let another task gain access of CPU to run the task-codes. Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Each task is usually coded such that it is in endless loop. An event starts the running of the codes and another event stops the running of the codes within the loop. When the running stops, another task can run. • Event can be an IPC (inter process communication) or interrupt. • An IPC can be a signal, token (flag), message into queue or mailbox,... Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Each task must be either reentrant routine (Chapter 5) or must have a way to solve the shared data problem (Lesson 2) Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
The task returns to the either ready state from the idle state on registering or attaching and retirns to idle state on deregistering or detaching. Registering or attaching or deregistering or detaching is at the task-scheduler. Ready and Idle States Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
States of a Task in a system (i) Idle state [Not attached or not registered] (ii) Ready State [Attached or registered] (iii) Running state (iv) Blocked (waiting) state (v) Delayed for a preset period Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Task Parameters • Parameters are at a TCB (Task Control Block), also called task information block), as at a PCB • TCB is at a memory block shared by the OS and task. Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
-Each task is recognised by a TCB or TIB. TCB is a memory block to holds the information for use by the OS and task. TCB Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Each task has an ID just as each function has a name. The ID, task IDi is usually a byte and is between 0 and m 1. ID is also an index of the task Task Parameters Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Each task has a priority parameter p. The priority,if between 0 and n 1. p is usually a byte. - Two or more tasks can be assigned same priority in time slicing mode of running the tasks • Each task has a signal mask, which when unmasked by the OS, then the task runs and when masked then the task blocks. Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
A signal (message) dispatch table (a table of signals sent to or received from the OS) for the OS actions and task messages • A context to define the CPU state (registers, program counter and CPU stack pointer), • A kernel stack (of executing system- functions) • Memory-block allocation information • Stack allocation information Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
TCB A shared memory area between task and OS Process ID and priority Signal mask, signal dispatch table Present state Present Context Memory and Stack Sizes Memory and stack block pointer Kernel stack Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Task-Context Context memory Task Program Counter Task Stack Pointer CPU register values Task local variable values Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
(i) program counter (from where task also gets the saved parameters after the scheduler granting access of the CPU to it). Program counter points to current instruction of the ant program and it indicates after an instruction fetch, the address of the next instruction to be executed for this task), Task Context... Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
(ii) task’s stack pointer (memory address from where it gets the saved parameters after the scheduler granting access of the CPU to task). These values are the part of its context of a task. Task Context Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
(iii) Context has a record that reflects the CPU state (reflected by the CPU registers) just before OS blocks one task and initiates another task into running state. The context thus continuously updates when a task runs and the context is saved before switching occurs to another task Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Context Switch Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
The context saves on each blocking of a running task A. Then the CPU control switches to other process or task B. • The context retrieves on transfer of program control from B to the CPU for running back A after the OS unblocks A and A enters into the running state again. Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Multitasking operations take place by context switching between the various tasks Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Summary Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
We learnt • (i) Process is a computational unit that processes on a CPU under the control of a scheduling kernel of operating system. It has a process structure, called process control block at the memory. Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
We learnt • (ii) Task is similar to a process and it can be considered as a process of an application software • (iii) Task has a set of computationsor actions that processes on a CPU under control of a scheduling kernel. It also has a process structure, called task control block that saves at the memory. Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
(iv) Task has a unique ID. It has states in the system as follows: idle, ready, running, blocked, delayed and deleted. It is in ready state again after finish when it has infinite waiting loop – an important feature in embedded system design. Chapter-8 L1: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.