RTS MID Answers

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

1. Explain fork and vfork commands.

1. fork() :
Fork() is system call which is used to create new process. New process created by fork()
system call is called child process and process that invoked fork() system call is called parent
process. Code of child process is same as code of its parent process. Once child process is
created, both parent and child processes start their execution from next statement after
fork() and both processes get executed simultaneously.
The syntax used for the fork system call is as below, pid_t fork(void);
2. vfork() :
Vfork() is also system call which is used to create new process. New process created by
vfork() system call is called child process and process that invoked vfork() system call is
called parent process. Code of child process is same as code of its parent process. Child
process suspends execution of parent process until child process completes its execution as
both processes share the same address space.
#include<stdio.h>
#include<unistd.h>
int main(void)
{
printf("Before fork\n"); vfork();
printf("after fork\n");
}
2. What is meant by synchronization? Explain.
Synchronization is classified into two categories: resource synchronization and activity
synchronization. Resource synchronization determines whether access to a shared resource
is safe, and, if not, when it will be safe. Activity synchronization determines whether the
execution of a multithreaded program has reached a certain state and, if it hasn't, how to
wait for and be notified when this state is reached.
Resource Synchronization
Access by multiple tasks must be synchronized to maintain the integrity of a shared
resource. This process is called resource synchronization, a term closely associated with
critical sections and mutual exclusions.
Mutual exclusion is a provision by which only one task at a time can access a shared
resource. A critical section is the section of code from which the shared resource is
accessed.
As an example, consider two tasks trying to access shared memory. One task (the sensor
task) periodically receives data from a sensor and writes the data to shared memory.
Meanwhile, a second task (the display task) periodically reads from shared memory and
sends the data to a display. The common design pattern of using shared memory is
illustrated in Figure.

Problems arise if access to the shared memory is not exclusive, and multiple tasks can
simultaneously access it. For example, if the sensor task has not completed writing data to
the shared memory area before the display task tries to display the data, the display would
contain a mixture of data extracted at different times, leading to erroneous data
interpretation.
The section of code in the sensor task that writes input data to the shared memory is a
critical section of the sensor task. The section of code in the display task that reads data
from the shared memory is a critical section of the display task. These two critical sections
are called competing critical sections because they access the same shared resource.

Activity Synchronization
In general, a task must synchronize its activity with other tasks to execute a multithreaded
program properly. Activity synchronization is also called condition synchronization or
sequence control . Activity synchronization ensures that the correct execution order among
cooperating tasks is used. Activity synchronization can be either synchronous or
asynchronous.
One representative of activity synchronization methods is barrier synchronization . For
example, in embedded control systems, a complex computation can be divided and
distributed among multiple tasks. Some parts of this complex computation are I/O bound,
other parts are CPU intensive, and still others are mainly floating-point operations that rely
heavily on specialized floating-point coprocessor hardware. These partial results must be
collected from the various tasks for the final calculation. The result determines what other
partial computations each task is to perform next.
The point at which the partial results are collected and the duration of the final computation
is a barrier . One task can finish its partial computation before other tasks complete theirs,
but this task must wait for all other tasks to complete their computations before the task
can continue.
Barrier synchronization comprises three actions:
* a task posts its arrival at the barrier,
* the task waits for other participating tasks to reach the barrier, and
* the task receives notification to proceed beyond the barrier.

3. Define RTOS.
Real Time Operating System (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. such applications are industrial control, telephone
switching equipment, flight control, and real-time simulations. With an RTOS, the
processing time is 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 Peacemaker, Network Multimedia
Systems, Robot etc.
The real-time operating systems can be of 3 types –
1.Hard Real-Time operating system:These operating systems guarantee that critical tasks be
completed within a range of time. For example, a robot is hired to weld a car body. If the
robot welds too early or too late, the car cannot be sold, so it is a hard real-time system that
requires complete car welding by robot hardly on the time. 

2.Soft real-time operating system: This operating system provides some relaxation in the
time limit. For example – Multimedia systems, digital audio systems etc. Explicit,
programmer-defined and controlled processes are encountered in real-time systems. A
separate process is changed with handling a single external event. The process is activated
upon occurrence of the related event signalled by an interrupt.
3. Firm Real-time Operating System:
RTOS of this type have to follow deadlines as well. In spite of its small impact, missing a
deadline can have unintended consequences, including a reduction in the quality of the
product. Example: Multimedia applications.

Advantages:
The advantages of real-time operating systems are as follows-
1. Maximum consumption
2. Task Shifting
3. Focus On Application
4. Real-Time Operating System In Embedded System
5. Error free
6. Memory allocation
Disadvantages:
The disadvantages of real-time operating systems are as follows-
1. Limited Tasks
2. Use Heavy System
3. Complex Algorithms
4. Device Driver And Interrupt signals
4. List and explain file I/O commands.
I/o commands:
The I/O commands allow you to own, use, read from, write to, and close devices. To direct
I/O operations to a device, first issue the following commands:
* Issue an OPEN command to establish ownership, unless the device is your principal device.
* Issue a USE command to make the device the current device.
* Subsequent READ and WRITE commands read from and write to that device.
* A CLOSE command releases ownership of the device so that other processes can use the
device.
The following sections give an overview of the InterSystems IRIS I/O commands.
General I/O Syntax
The following general syntax applies to I/O commands that support I/O command keywords
in ObjectScript:
OPEN device:paramlist:timeout:"mnespace"
USE device:paramlist:"mnespace"
CLOSE device:paramlist
where paramlist is either a single parameter, or a list of parameters enclosed in parentheses
and separated by colons:
parameter (parameter:parameter[:...])
A parameter can either be a positional parameter or a keyword parameter. A keyword
parameter has the following syntax:
/keyword[=value]
The leading slash distinguishes a keyword parameter from a positional parameter value. The
meaning of a positional parameter value is derived from its position in the colon-delimited
list. The meaning of a keyword parameter value is derived from the specified keyword.
Note that both positional and keyword parameters can be specified in the same paramlist.
For example, the following example mixes positional and keyword parameters to open a
new file named test.dat in write/sequential mode with JIS I/O translation:
OPEN "test.dat":("NWS":/IOTABLE="JIS")
OPEN Command
OPEN establishes ownership of, and opens an I/O channel to, the device specified. This
ownership persists until you issue a CLOSE command, your process terminates, or some
physical operation closes the device. For physical I/O devices or for interprocess
communications (such as TCP connections), this ownership prevents all other processes
from accessing the device. For logical I/O devices (such as sequential files), this ownership
may allow other processes some form of shared access to the file. The handling of multiple
processes that open the same sequential file is highly platform-dependent. Use of the LOCK
command to restrict access to sequential files is strongly advised.
Syntax
OPEN device{:{(parameters)}{:{timeout}{:"mnespace"}}}
USE Command
This command makes the specified device the current device, and sets the special variable
$IO to that device. To USE a device other than your principal device, you must first issue an
OPEN command for it; otherwise, you will receive a <NOTOPEN> error. Arguments have the
same meaning as in the OPEN command.
Syntax
USE device:(args):"mnespace"
READ Command
This command reads data from the current device. For some devices, arguments that begin
with asterisks return ASCII numeric information; for others, they indicate control functions.
Syntax
READ variable:timeout
WRITE Command
This command writes data to the current device. For some devices, arguments that begin
with asterisks let you write ASCII characters using their ASCII numeric values; for others,
they indicate control functions. For some devices, arguments that begin with the # character
indicate the number of times to write that character.
Syntax
WRITE variable
CLOSE Command
The CLOSE command releases ownership of the specified device. CLOSE reverses the effect
of the OPEN command.
Syntax
CLOSE device[:params]
5. Describe exit, wait and waitpid commands.
• The wait() system call suspends execution of the current process until one of its
children terminates. The call wait(&status) is equivalent to:

waitpid(-1, &status, 0);

• The waitpid() system call suspends execution of the current process until a child
specified by pid argument has changed state. By default, waitpid() waits only for
terminated children, but this behaviour is modifiable via the options argument, as
described below.
The value of pid can be:

Tag Description
meaning wait for any child process whose process group
< -1
ID is equal to the absolute value of pid.

-1 meaning wait for any child process.

meaning wait for any child process whose process group


0
ID is equal to that of the calling process.

meaning wait for the child whose process ID is equal to


>0
the value of pid.

• exit command in linux is used to exit the shell where it is currently running. It takes
one more parameter as [N] and exits the shell with a return of status N. If n is not
provided, then it simply returns the status of last command that is executed.
Syntax:
Options for exit command –
• exit: Exit Without Parameter
After pressing enter, the terminal will simply close.
• exit [n]
• exit n
• exit –help: It displays help information.
6. Write about pipes.
Pipes provide a more flexible means of passing simple messages between tasks than
mailboxes or queues.
A pipe is simply a set of storage locations, each big enough to hold a single data item of
user-defined byte length, access to which is controlled so that it may be safely utilized by
multiple tasks. Tasks can write to a pipe repeatedly until all the locations are full. Tasks can
read from a pipe and data is normally received on a first in, first out (FIFO) basis. Trying to
send to a full pipe or read from an empty one may result in an error or task suspension,
depending on options selected in the API call.
The following set of operations can be performed on a pipe:
· create and destroy a pipe,

Operation Description
Pipe Creates a pipe

Open Opens a pipe

Close Deletes or closes a pipe

· read from or write to a pipe,

Operation Description

Read Reads from the pipe

Write Writes to a pipe

· issue control commands on the pipe, and

Operation Description

Fcntl Provides control over the pipe descriptor

· select on a pipe.

Operation Description

Select Waits for conditions to occur on a pipe

7. What is message queues?


A message queue is a kernel object (i.e., a data structure) through which messages are sent
(i.e., posted) from either interrupt service routines (ISRs) or tasks to another task (i.e.,
pending). An application can have any number of message queues, each one having its own
purpose. For example, a message queue can be used to pass packets received from a
communication interface ISR to a task, which in turn would be responsible for processing
the packet. Another queue can be used to pass content to a display task that will be
responsible for properly updating a display.
Figure 1. Message queues are kernel objects used to pass content to a task.
Messages are typically void pointers to a storage area containing the actual message.
However, the pointer can point to anything, even a function for the receiving task to
execute. The meaning of the message is thus application-dependent. Each message queue is
configurable in the amount of storage it will hold. A message queue can be configured to
hold a single message (a.k.a., a mailbox) or N messages. The size of the queue depends on
the application and how fast the receiving task can process messages before the queue fills
up.

Figure 2. Pool of message storage areas


In many implementations of message queues in an RTOS, a message being sent to a queue is
discarded if the queue is already full. Oftentimes this is not an issue and the logic of the
application can recover from such situations.

Figure 3. Blocking a sender if the queue is full.


8. Write about event registers.
This register, called an event register, is an object belonging to a task and consists of a group of
binary event flags used to track the occurrence of specific events. ... Each bit in the event register is
treated like a binary flag (also called an event flag) and can be either set or cleared.

Through the event register, a task can check for the presence of particular events that can control its
execution. An external source, such as another task or an ISR, can set bits in the event register to
inform the task that a particular event has occurred.

Applications define the event associated with an event flag. This definition must be agreed upon
between the event sender and receiver using the event register.

Event Register Control Blocks

Typically, when the underlying kernel supports the event register mechanism, the kernel creates an
event register control block as part of the task control block when creating a task, as shown

Figure: Event register control block.

The task specifies the set of events it wishes to receive. This set of events is maintained in the
wanted events register. Similarly, arrived events are kept in the received events register. The task
indicates a timeout to specify how long it wishes to wait for the arrival of certain events. The kernel
wakes up the task when this timeout has elapsed if no specified events have arrived at the task.

Typical Event Register Operations


Two main operations are associated with an event register, the sending and the receiving
operations, as shown

Operation Description

Send Sends events to a task

Receive Receives events

You might also like