Cs3691-Unit 2
Cs3691-Unit 2
UNIT-2
EMBEDDED C PROGRAMMING
IMPORTANT QUESTIONS
PART-A
An Embedded system is one that has computer hardware with software embedded in it as
one of its most important component.The three main components of an embedded system
are
1. Hardware
3. RTOS
2) What is an Assembler?
A computer program that translate an assembly language program from mnemonics to the
binary machine code of a computer.
3) How many memory locations can be addressed by a microprocessor with 14 address
lines?
The 8085 MPU with its 14-bit address is capable of addressing 214=16,384 (ie) 16K
memory locations.
4) What is simplex and duplex transmission?
Simplex transmission: data are transmitted in only one direction. Duplex transmission: data
flow in both directions. If the transmission goes one way at a time, it is called half duplex;
if it goes both way simultaneously, then it is called full duplex.
Page | 1
CS3691-Embedded Systems and IOT Prepared by: E U Iniyan,AP/ECE
5) What is RTOS?
Real-time operating systems (RTOS) are used in environments where a large number
of events, mostly external to the computer system, must be accepted and processed in a
short time or within certain deadlines
6) List the different kind of states in Task management.
Dormant : Task doesn’t require computer time
Ready: Task is ready to go active state, waiting processor time
Active: Task is running
Suspended: Task put on hold temporarily
Pending: Task waiting for resource
7) Explain the function of the pins PSEN and EA of 8051.
PSEN :
PSEN stands for program store enable. In 8051 based system in which an external ROM holds
the program code, this pin is connected to the OE pin of the ROM.
EA :
EA stands for external access. When the EA pin is connected to Vcc, program fetched to
addresses 0000H through 0FFFH are directed to the internal ROM and program fetches
toaddresses 1000H through FFFFH are directed to external ROM/EPROM. When the EA
pin is grounded, all addresses fetched by program are directed to the external
ROM/EPROM.
8) Define Semaphore.
Semaphores are just normal variables used to coordinate the activities of multiple processes
in a computer system. They are used to enforce mutual exclusion, avoid race conditions
and implement synchronization between processes.
9) List the different types of semaphores.What are its uses?
a)Binary Semaphore :
This is also known as a mutex lock. It can have only two values – 0 and 1. Its value is
initialized to 1. It is used to implement the solution of critical section problems with
multiple processes.
Page | 2
CS3691-Embedded Systems and IOT Prepared by: E U Iniyan,AP/ECE
b)Counting Semaphore ::
Its value can range over an unrestricted domain. It is used to control access to a
resource that has multiple instances.
10) Define Context Switching:
Context Switching is the switching of CPU from one process to another process. Context
switching means storing the process state so that we can reload the process when needed,
and the execution of the process can be resumed from the same point later.
11)What is Priority scheduling? Based on which factors the OS will choose the priority?
In priority scheduling in OS, processes are executed on the basis of their priority. The jobs
processes with higher priority are executed first. Naturally, you might want to know how
priority of processes is decided.
Priority of processes depends some factors such as:
• Time limit
• Memory requirements of the process
• Ratio of average I/O to average CPU burst time
Page | 3
CS3691-Embedded Systems and IOT Prepared by: E U Iniyan,AP/ECE
Complex Simple
1. Interrupts
2. Multitasking
3. Kernel/user switch
PART-B
The system designer is not limited by the amount of internal RAM and ROM available on chip.
Two separate external memory spaces are made available by the 16-bit PC and DPTR and by
different control pins for enabling external ROM and RAM chips. External RAM, which is
accessed by the DPTR, may also be needed when 128 bytes of internal data storage is not
Page | 4
CS3691-Embedded Systems and IOT Prepared by: E U Iniyan,AP/ECE
sufficient. External RAM, up to 64K bytes, may also be added to any chip in the 8051 family.
Connecting External Memory: Figures shows the connections between an 8051 and an external
memory configuration consisting of EPROM and static RAM. The 8051 accesses external RAM
whenever certain program instructions are executed. External ROM is accessed whenever the EA
(external access) pin is connected to ground or when the PC contains an address higher than the
last address in the internal 4K bytes ROM (OFFFh). 8051 designs can thus use internal and external
ROM automatically; the 8051 , having no internal ROM, must have (EA)' grounded
If the memory access is for a byte of program code in the ROM, the (PSEN)'(program store
enable) pin will go low to enable the ROM to place a byte of program code on the data bus. If the
access is for a RAM byte, the (WR)'(write) or (RD)'(read) pins will go low, enabling data to flow
between the RAM and the data bus. Note that the (WR)' and (RD)' signals are alternate uses for
port 3 pins 16 and 17. Also, port 0 is used for the lower address byte and data; port 2 is used for
upper address bits. The use of external memory consumes many of the port pins, leaving only
port 1 and parts of port 3 for general I/O.
2.Define Real Time Operating System and explain its types with an example.
Real-time operating systems (RTOS) are used in environments where a large number of
events, mostly external to the computer system, must be accepted and processed in a short
time or within certain deadlines such applications are industrial control, telephone switching
equipment, flight control, and real-time simulations. With an RTOS, the processing time is
Page | 5
CS3691-Embedded Systems and IOT Prepared by: E U Iniyan,AP/ECE
measured in tenths of seconds. This system is time-bound and has a fixed deadline. The
processing in this type of system must occur within the specified constraints. Otherwise, this
will lead to system failure.
Examples of the real-time operating systems: Airline traffic control systems, Command
Control Systems, Airlines reservation system, Heart Pacemaker, Network Multimedia
Systems, Robot etc.
The real-time operating systems can be of 3 types
1. Hard RTOS
2. Soft RTOS
3. Firm RTOS
Page | 6
CS3691-Embedded Systems and IOT Prepared by: E U Iniyan,AP/ECE
1. Maximum consumption –
Maximum utilization of devices and systems. Thus more output from all the resources.
2. Task Shifting –
Time assigned for shifting tasks in these systems is very less. For example, in older systems,
it takes about 10 microseconds. Shifting one task to another and in the latest systems, it takes
3 microseconds.
3. Focus On Application –
Focus on running applications and less importance to applications that are in the queue.
5. Error Free –
These types of systems are error-free.
6. Memory Allocation –
Memory allocation is best managed in these types of systems.
Page | 7
CS3691-Embedded Systems and IOT Prepared by: E U Iniyan,AP/ECE
Disadvantages:
The disadvantages of real-time operating systems are as follows-
1. Limited Tasks –
Very few tasks run simultaneously, and their concentration is very less on few applications to
avoid errors.
3. Complex Algorithms –
The algorithms are very complex and difficult for the designer to write on.
5. Thread Priority –
It is not good to set thread priority as these systems are very less prone to switching tasks.
For simpler applications, RTOS is usually a kernel but as complexity increases, various modules
like networking protocol stacks debugging facilities, device I/Os are includes in addition to the
kernel. The general architecture of RTOS is shown in the below
Page | 8
CS3691-Embedded Systems and IOT Prepared by: E U Iniyan,AP/ECE
Task Management:
In RTOS, The application is decomposed into small, schedulable, and sequential program units
known as “Task”, a basic unit of execution and is governed by three time critical properties;
release time, deadline and execution time. Release time refers to the point in time from which the
task can be executed. Deadline is the point in time by which the task must complete. Execution
time denotes the time the task takes to execute.
During the execution of an application program, individual tasks are continuously changing from
one state to another. However, only one task is in the running mode (i.e. given CPU control) at
any point of the execution. In the process where CPU control is change from one task to another,
context of the suspended task will be saved while context of the executed task will be retrieved,
the process referred to as context switching
Task Synchronization & inter task communication serves to pass information among tasks.
Task Synchronization
Synchronization is essential for tasks to share mutually exclusive resources (devices, buffers, etc)
and/or allow multiple concurrent tasks to be executed (e.g. Task A needs a result from task B, so
task A can only run till task B produces it.
Page | 10
CS3691-Embedded Systems and IOT Prepared by: E U Iniyan,AP/ECE
Event Objects
Event objects are used when task synchronization is required without resource sharing. They
allow one or more tasks to keep waiting for a specified event to occur. Event object can exist
either in triggered or non-triggered state. Triggered state indicates resumption of the task.
Semaphores.
A semaphore has an associated resource count and a wait queue. The resource count indicates
availability of resource. The wait queue manages the tasks waiting for resources from the
semaphore. A semaphore functions like a key that define whether a task has the access to the
resource. A task gets an access to the resource when it acquires the semaphore.
• Binary Semaphores
• Counting Semaphores
Memory Management
Two types of memory managements are provided in RTOS – Stack and Heap. Stack management
is used during context switching for TCBs. Memory other than memory used for program code,
Page | 11
CS3691-Embedded Systems and IOT Prepared by: E U Iniyan,AP/ECE
program data and system stack is called heap memory and it is used for dynamic allocation of data
space for tasks. Management of this memory is called heap management.
Timer Management
Tasks need to be performed after scheduled durations. To keep track of the delays,timers- relative
and absolute are provided in RTOS.
RTOS provides various functions for interrupt and event handling, viz., Defining interrupt handler,
creation and deletion of ISR, referencing the state of an ISR, enabling and disabling of an interrupt,
etc. It also restricts interrupts from occurring when modifying a data structure, minimize interrupt
latencies due to disabling of interrupts when RTOS is performing critical operations, minimizes
interrupt response times.
RTOS generally provides large number of APIs to support diverse hardware device drivers.
Tasks are units of sequential code implementing the system actions and executed
concurrently by an OS. Real time systems require that tasks be performed within a particular time
frame. Task is related to the performance of the real time systems.
A task, also called a thread, is a simple program that thinks it has the CPU all to itself. The
design process for a real-time application involves splitting the work to be done into tasks
responsible for a portion of the problem.\
Each task is assigned a priority, its own set of CPU registers and its own stack area.In the
specified time constraint, system must produce its correct output. If system fail to meet the
specified output, then the system is fail or quality decreases.
Real time systems are used for space flights, air traffic control, high speed aircraft,
telephone switching, electricity distribution, industrial processes etc.
Real time system must be 100 % responsive 100 % of the time. Response time is measured
in fractions of second, but this is an ideal not often achieved in the field.
Page | 12
CS3691-Embedded Systems and IOT Prepared by: E U Iniyan,AP/ECE
Real time database is updated continuously. In aircraft example, flight data is continuously
changing so it is necessary to update. It includes speed, direction, location, height etc.A process is
a sequential program in execution. Terms like job and task are also used to denote a process.
A process is a dynamic entity that executes a program on a particular set of data. Multiple
processes may be associated with one program.Task is a single instance of an executable
program.In a multiprogramming environment, usually more programs to be executed than could
possibly be rim at one time. In CPU scheduling, it switches from one process to another process.
CPU resource management is commonly known asscheduling.
Input and output of the compressor box is serial ports. It takes uncompressed data and processes
it. Output of the box is compressed data. Given data is compressed using predefined compression
table. Modem is used such type of box.
The program's need to receive and send data at different rates. It is an example of rate
control problems. It uses asynchronous input. You can provide a button for compressed mode and
Page | 13
CS3691-Embedded Systems and IOT Prepared by: E U Iniyan,AP/ECE
uncompressed mode.When user press uncompressed mode, the input data is passed through
unchanged.
The button will be depressed at a much lower rate than characters will be received, since it
is not physically possible for a person to repeatedly depress a button at even slow serial line rates.
Keeping up with the input and output data while checking on the button can introduce
some very complex control code into the program.
Sampling the button's state too slowly can cause the machine to miss a button depression
entirely, but sampling it too frequently and duplicating a data value can cause the machine to
incorrectly compress data.This problem is solved by maintaining counter.
6.Define Context Switching and list of steps involved in it with neat sketch.
Context Switching is the switching of CPU from one process to another process. Context switching
means storing the process state so that we can reload the process when needed, and the execution
of the process can be resumed from the same point later.
Interrupts
Multitasking
Kernel/user switch
Interrupts: - We require context switching if there is an interruption of CPU to get data from the
disk read.
Multitasking: - If the CPU has to move processes in and out of memory so that it can run more
than one operation.
Kernel/user switch: - We use kernel/user switch if we require switching between the user mode
to the kernel mode.
Steps Involved in Context Switching
With the help of the below figure, we describe the procedure of context switching between the
processes, which are P1 and P2.
Page | 14
CS3691-Embedded Systems and IOT Prepared by: E U Iniyan,AP/ECE
We can see in the following figure that initially, the P1 process is in running state, and the P2
process is in the ready state. If there occurs some interruption, then it is required to change the
state of the P1 process from running to the ready state. When the context of the process P1 is saved,
then change the state of the P2 process from ready to the running state.
There are various steps which are involved in the context switching:
1. The process P1 context, which is in the running state, will be stored in PCB (Program
Control Block). That is called PCB1.
2. Next, PCB1 is transferred to the appropriate queue, i.e., the I/O queue, ready queue, and
the waiting queue.
3. Then from the ready queue, we choose the new process which is to be executed i.e., the
process P2.
4. Next, we update the PCB (Program Control Block) of the P2 process called PCB2. It
includes switching the process state from one to another (ready, blocked, suspend, or exit).
If the CPU previously executed process P2, then we get the location of the last executed
process so that we can again proceed with the P2 process execution.
Page | 15
CS3691-Embedded Systems and IOT Prepared by: E U Iniyan,AP/ECE
5. In the same manner, if we again need to execute the process P1, then the same procedure
is followed.
Under process scheduling, the tasks that the operating system performs are:
In priority scheduling in OS, processes are executed on the basis of their priority. The jobs /
processes with higher priority are executed first. Naturally, you might want to know how priority
of processes is decided.
• Time limit
a) Non-Preemptive Scheduling
b) Preemptive Scheduling
Page | 16
CS3691-Embedded Systems and IOT Prepared by: E U Iniyan,AP/ECE
a) Non-Preemptive Scheduling
In the Non Preemptive Priority scheduling, The Processes are scheduled according to the priority
number assigned to them. Once the process gets scheduled, it will run till the completion.
Generally, the lower the priority number, the higher is the priority of the process.Example
In the Example, there are 7 processes P1, P2, P3, P4 and P5. Their priorities, Arrival Time and
burst time are given in the table.
1 2 2 2
2 0 1 3
3 1 3 4
4 3 5 1
5 4 4 5
We can prepare the Gantt chart according to the Non Preemptive priority scheduling.
The processor is allocated to process P2 since it arrives at 0 and takes one milliseconds to complete.
Process P1(3) arrived at the time 1 secs, while process P2 was completed its operation (executed
1 burst in 1 sec), and starts to process P1 processes until it processes all its bursts.
Then the CPU gives priority to Process P4 which is having highest priority and starts to execute
its bursts completely in 5S.
The same process will be followed to execute the remaining processes P1,P3 and P5
Page | 17
CS3691-Embedded Systems and IOT Prepared by: E U Iniyan,AP/ECE
At the arrival time of 3 Sec, P1 processes its one burst and start to process P4(5) because compare
to P1, P4 is having highest Priority. Since P4 is having highest priority amount all processes, it
will execute all its 5 bursts.
At the arrival time of 4 Sec, P4 executes its one burst and the ready queue looks like below.
Then the CPU gives priority to next highest priority and starts to execute the processes using the
same technique.
Page | 18
CS3691-Embedded Systems and IOT Prepared by: E U Iniyan,AP/ECE
b) Preemptive Scheduling
Preemptive Scheduling is a scheduling method where the tasks are mostly assigned with
their priorities. Sometimes it is important to run a task with a higher priority before another
lower priority task, even if the lower priority task is still running.
At that time, the lower priority task holds for some time and resumes when the higher
priority task finishes its execution.
Example
There are 5 processes P1, P2, P3, P4 and P5 given. Their respective priorities, Arrival Times and
Burst times are given in the table below.
1 2 2 2
2 0 1 3
3 1 3 4
4 3 5 1
5 4 4 5
To begin, the process P2 appears at time 0. As a result, the CPU is assigned to P2(1)
Process P1(2) arrived at the time one unit, while process P2 was completed its operation (executed
1 burst in 1 sec), and starts to process P3(3).
At the arrival time of 2 Sec,P3 processes its one burst and start to process P1(2) because compare
to P3, P1 is having highest Priority.
Page | 19
CS3691-Embedded Systems and IOT Prepared by: E U Iniyan,AP/ECE
At the arrival time of 3 Sec, P1 processes its one burst and start to process P4(5) because compare
to P1, P4 is having highest Priority. Since P4 is having highest priority amount all processes, it
will execute all its 5 bursts.
At the arrival time of 4 Sec, P4 executes its one burst then the CPU gives priority to next highest
priority and starts to execute the processes using the same technique.
Page | 20