CO4 CHAP 9 - (13-24) - Practice
CO4 CHAP 9 - (13-24) - Practice
CO4 CHAP 9 - (13-24) - Practice
13. A starting task is first created, which creates all the (asks needed, initiates the system clock and then
that task is suspended. Why must this strategy be used?
14. VxWorks kernel includes both POSIX standard interfaces and VxWorks special interfaces. What are
the advantages of special interfaces for the semaphores and queues? \
• VxWorks special interfaces are often designed with a focus on efficiency and
performance, tailored specifically for embedded systems. This optimization is crucial in
real-time environments where minimal latency and predictable response times are
essential.
2. Fine-Tuned Control:
• Special interfaces allow for more fine-tuned control over the behavior and
characteristics of semaphores and queues. This level of control is valuable in scenarios
where specific real-time requirements or constraints need to be met.
• Embedded systems often have unique requirements and resource constraints. VxWorks
special interfaces are designed with these considerations in mind, allowing developers
to tailor the implementation to suit the specific needs of the embedded platform.
4. RTOS-Specific Features:
• VxWorks special interfaces may expose features and functionalities that are specific to
the real-time operating system. These features may not be available in a generic POSIX
implementation, providing additional capabilities for developers working in the VxWorks
environment.
15. How do you initiate round robin time-slice scheduling? Give 5 examples of the need for round robin
scheduling.
ANS - To initiate Round Robin time-slice scheduling, you typically follow these steps:
16. How Do you initiate pre-emptive scheduling and assign priorities to lhe tasks for scheduling'.’ Give 10
examples of the need for pre-emptive scheduling.
1. Real-Time Systems:
• In real-time systems, where tasks have strict deadlines, preemptive scheduling
ensures that higher-priority tasks are executed promptly, meeting their deadlines.
2. Interactive Systems:
• Preemptive scheduling is crucial in interactive systems to provide responsiveness.
Higher-priority tasks, such as user input handling, can preempt lower-priority
background tasks.
3. Embedded Systems:
• In embedded systems, where tasks need to respond to external events or stimuli,
preemptive scheduling ensures that critical tasks are given immediate attention.
4. Multitasking Environments:
• Preemptive scheduling is essential in multitasking environments to prevent a
single long-running task from monopolizing the CPU, allowing other tasks to get
their fair share of execution time.
5. Server Environments:
• Servers handling multiple requests benefit from preemptive scheduling to
prioritize critical server processes over less critical ones, ensuring smooth and
responsive service.
6. Prioritizing Background Tasks:
• Background tasks, such as maintenance or periodic updates, can be assigned
lower priorities to prevent them from impacting the performance of foreground
tasks.
7. Time-Critical Applications:
• Applications with time-critical components, such as audio and video processing,
benefit from preemptive scheduling to ensure that tasks are executed within
specified time constraints.
8. Resource Allocation:
• Preemptive scheduling allows for better resource allocation by giving priority to
tasks that require immediate access to resources, preventing resource contention.
9. Handling Interrupts:
• In systems that handle hardware interrupts, preemptive scheduling is necessary
to respond quickly to external events, ensuring minimal latency in handling
interrupts.
10. Multithreaded Environments:
• Preemptive scheduling is crucial in multithreaded environments where threads
with higher priority may need to preempt threads with lower priority to maintain
system responsiveness.
17. How do you use signals and use function void sigHandler (int signal (sigNum, siglSR) and intConnect
|I_NUM_TO_IVEC (sigNum), siglSR. sigArg]? Give five examples of their uses.
#include <stdio.h>
#include <signal.h>
int main() {
return 0;
#include <signal.h>
if (sigNum == SIGINT)
printf("Received SIGINT.\n");
printf("Received SIGTERM.\n");
int main() {
signal(SIGINT, sigHandler);
signal(SIGTERM, sigHandler);
return 0;
}
#include <stdio.h>
#include <taskLib.h>
#include <intLib.h>
void isrHandler() {
int main() {
return 0;
1. Terminating a Process:
• The SIGTERM signal can be used to request the termination of a process
gracefully.
2. Interrupting a Process:
• The SIGINT signal (Ctrl+C) can be used to interrupt a running process.
3. Handling Errors:
• A custom signal handler can be used to handle specific errors gracefully,
providing more information or taking appropriate actions.
4. Reloading Configuration:
• The SIGHUP signal can be used to request a process to reload its configuration.
5. Real-Time Event Handling:
• In real-time systems, custom signals can be used to handle specific events or
conditions, ensuring timely responses.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
sem_t countSemaphore;
sem_wait(&countSemaphore);
// Critical section: Access the shared resource
sem_post(&countSemaphore);
pthread_exit(NULL);
int main() {
int initialCount = 5; // You can set this to the number of available resources
sem_init(&countSemaphore, 0, initialCount);
pthread_t threads[3];
pthread_join(threads[i], NULL);
}
// Destroy the counting semaphore when done
sem_destroy(&countSemaphore);
return 0;
19. OS provides that all ISRs share a single stack. What are the limitations it imposes?
20. How do you create, remove, open, close, read, write and IO control a device using RTOS functions?
Take an Sample of a pipe delivering an IO stream from a network device.
ANS - Creating, removing, opening, closing, reading, writing, and performing I/O control
operations on a device in a real-time operating system (RTOS) typically involves using functions
provided by the RTOS API. The exact functions and procedures can vary based on the specific
RTOS you are working with
Creating a Pipe:
// Create a pipe to deliver an I/O stream
pipe_handle = rtos_create_pipe();
Removing a Pipe:
// Remove the pipe when it is no longer needed
rtos_remove_pipe(pipe_handle);
Opening a Pipe:
// Open the pipe for reading or writing
Closing a Pipe:
// Close the pipe when finished reading or writing
rtos_close_pipe(pipe_fd);
char buffer[100];
Writing to a Pipe:
// Write data to the pipe
21. Explain the use of file descriptor for IO devices and files.
• File descriptors are primarily used to perform I/O operations on files and devices. When
a file or device is opened, the operating system assigns a unique file descriptor to it.
Subsequent I/O operations, such as reading, writing, and seeking, are performed using
this file descriptor.
• File descriptors act as identifiers for open files and devices within a process. Each open
file or device is associated with a specific file descriptor, allowing the process to
distinguish between different files and devices it has opened.
• In Unix-like systems, three standard file descriptors are predefined for every process:
• Standard Input (stdin): File descriptor 0
• Standard Output (stdout): File descriptor 1
• Standard Error (stderr): File descriptor 2 These standard descriptors are
automatically opened when a process starts and are used for input and output.
4. File Table:
• The operating system maintains a file table that maps file descriptors to file or device
structures. This table keeps track of open files and devices for each process, storing
information such as the file position, access mode, and other relevant details.
5. Resource Management:
• File descriptors play a role in resource management. The operating system uses them to
keep track of open files and devices, ensuring that resources are appropriately allocated
and deallocated as files are opened and closed.
22. How do you let a lower priority task execute in a pre-emptive scheduler? Give four coding examples.
ANS - In a preemptive scheduler, a lower priority task can be allowed to execute by adjusting the
priority of tasks dynamically or by yielding the CPU voluntarily. Here are four coding examples
illustrating different ways to let a lower priority task execute in a preemptive scheduler. Keep in
mind that the actual code may depend on the programming language and the specific
scheduler or operating system being used.
return NULL;
pthread_setschedprio(pthread_self(), sched_get_priority_min(SCHED_FIFO));
return NULL;
int main() {
// Create threads
pthread_join(lowerPriorityThread, NULL);
pthread_join(higherPriorityThread, NULL);
return 0;
return NULL;
pthread_yield();
return NULL;
int main() {
// Create threads
pthread_create(&lowerPriorityThread, NULL, lowerPriorityTask, NULL);
pthread_join(lowerPriorityThread, NULL);
pthread_join(higherPriorityThread, NULL);
return 0;
#include <unistd.h>
return NULL;
usleep(1000); // 1 millisecond
return NULL;
int main() {
pthread_t lowerPriorityThread, higherPriorityThread;
// Create threads
pthread_join(lowerPriorityThread, NULL);
pthread_join(higherPriorityThread, NULL);
return 0;
#include <semaphore.h>
sem_t semaphore;
return NULL;
sem_post(&semaphore);
return NULL;
int main() {
// Initialize semaphore
sem_init(&semaphore, 0, 0);
// Create threads
sem_wait(&semaphore);
pthread_join(lowerPriorityThread, NULL);
pthread_join(higherPriorityThread, NULL);
// Destroy semaphore
sem_destroy(&semaphore);
return 0;
23. How do you spawn tasks? Why should you not delete a task unless memory constraint exists?
ANS - Spawning tasks refers to creating or initiating new tasks in a concurrent or multitasking
environment. The concept is commonly associated with real-time operating systems (RTOS) and
general-purpose operating systems where tasks or threads can be created to perform specific
functions. The spawning of tasks is typically done using functions provided by the operating
system or the RTOS.
#include <pthread.h>
return NULL;
int main() {
pthread_t taskId;
pthread_join(taskId, NULL);
return 0;
there are reasons to be cautious about deleting tasks, especially in embedded or real-
time systems:
1. Resource Cleanup:
• Deleting a task often involves cleaning up resources associated with that task,
such as memory, file handles, or synchronization primitives. If not done carefully,
it can lead to resource leaks.
2. Unpredictable State:
•Deleting a task abruptly may leave shared data or critical sections in an
unpredictable state. Proper synchronization mechanisms and cleanup procedures
should be followed to ensure the system's stability.
3. Race Conditions:
• If other tasks are still actively using or depending on the resources managed by
the task being deleted, it can lead to race conditions, data corruption, or
undefined behavior.
4. Task Dependencies:
• Tasks in a system often have dependencies on each other. Deleting a task may
disrupt the expected flow of execution and cause unexpected behavior in other
tasks.
24. Write exemplary codes for using the POSIX functions for timer, semaphores and queues
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <time.h>
timer_t timerid;
int main() {
sa.sa_sigaction = timer_handler;
its.it_value.tv_sec = 1;
its.it_value.tv_nsec = 0;
its.it_interval.tv_sec = 1;
its.it_interval.tv_nsec = 0;
sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = SIGRTMIN;
sev.sigev_value.sival_ptr = &timerid;
sleep(5);
timer_delete(timerid);
return 0;
. Semaphore Example:
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
sem_t semaphore;
sem_wait(&semaphore);
sem_post(&semaphore);
return NULL;
int main() {
sem_init(&semaphore, 0, 1);
// Create threads
pthread_create(&thread1, NULL, thread_function, &thread_id1);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
sem_destroy(&semaphore);
return 0;
#include <stdlib.h>
#include <mqueue.h>
int main() {
mqd_t mq;
attr.mq_flags = 0;
attr.mq_maxmsg = 10;
attr.mq_msgsize = MAX_MSG_SIZE;
attr.mq_curmsgs = 0;
if (mq == (mqd_t)-1) {
perror("mq_open");
exit(EXIT_FAILURE);
if (bytes_received > 0) {
buffer[bytes_received] = '\0';
mq_close(mq);
mq_unlink(QUEUE_NAME);
return 0;
4. Search lhe web (e.g.. www.eet.com) and find the latest top RTOS products.
5. Draw five figures showing models for five examples 9.16 to 9.20 in Section 9.2 for event-flag
semaphore, mutex, counting semaphore, mailbox and queue interprocess communication
. 6. Draw the figures lo show lhe models for interprocess communication al processes in digiial camera.
ACVM and orchestra playing robot examples in Sections 1.10.4. 1.10.2 and 1.10.7. respectively.
6. Classify and list the source files, which depend on the processor and those that are are
processor-independent?
|-----------------------------------------|-----------------------------------------------------------|
| **Timer Services** | Provides timer services for timed delays and timeouts. |
| **File System Support (optional)** | May include support for file systems on external storage. |
| **Security Features (optional)** | Includes security measures for secure embedded systems. |
MUCOS has one type of semaphore for using as resource key. as flag, as counting semaphore and
mutex. What is the advantage of (his simplicity? I ’.
How do you set lhe system clock using function void OSTimeSet (unsigned int counts)? :
i. What are the advantages of a well-tested and debugged broad-focussed RTOS, which is
also well trusted and popular? (Him: Embedded software has to be ofthe highest quality and
there should be fastersoftware development. Need for complex coding skills required in the
development team for device drivers, memory and device managers, networking tasks,
exception handling, test vectors, APIs and so on.)
2. How does a mailbox message differ from a queue message? Can you use message queue
as a counting semaphore?
A mailbox typically refers to a communication mechanism where tasks can send and
receive messages. The main characteristics of a mailbox message include:
1. Point-to-Point Communication:
• A mailbox is often designed for point-to-point communication between two
tasks. One task sends a message to a specific mailbox, and another task receives
that message.
2. Asynchronous Communication:
• Tasks can send and receive messages asynchronously. The sender doesn't need
to wait for the receiver to be ready, and vice versa.
3. Sender and Receiver Identification:
• In a mailbox, there's a clear identification of the sender and the receiver.
Messages are explicitly addressed to a particular mailbox, and tasks can check
their own mailboxes for incoming messages.
Message Queue:
A message queue, on the other hand, is a more general communication mechanism that
allows multiple tasks to exchange messages. Key characteristics of a message queue
include:
1. One-to-Many Communication:
• Multiple tasks can send messages to and receive messages from a shared
message queue. It supports one-to-many communication.
2. FIFO Order:
• Messages in a queue are typically processed in a first-in-first-out (FIFO) order.
The first message added to the queue is the first one to be processed.
3. Blocking and Non-blocking Operations:
• Message queue operations can be blocking or non-blocking. A task may be
blocked until it can send or receive a message, or it may proceed without waiting.
4. Priority Support (Dependent on Implementation):
• Some message queue implementations support message priorities, allowing
higher-priority messages to be processed before lower-priority ones.
Message Queue as a Counting Semaphore:
4. Explain ECB.
ANS -