B.N.M.
Institute of Technology
An Autonomous Institution under VTU, Approved by AICTE
Department of Electrical and Electronics Engineering
Course Name: REAL TIME OPERATING SYSTEM
Course Code: 23EEE153
Module-1 Laboratory Programs
Program 1: Hello Process
Description:
Create a simple child process that prints a Hello message and exits. Use fork to create child
process.
Expected Output:
This is the original (parent) process. PID: [parent_pid]
Hello from the parent process! Created a child with PID: [Child_pid]
Hello from the child process! PID: 540693, Parent PID: [Child_pid]
Program
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main() {
pid_t pid;
printf("This is the original (parent) process. PID: %d\n", getpid());
pid = fork(); // Create a child process
if (pid < 0) {
// fork failed
perror("fork failed");
return 1;
} else if (pid == 0) {
// Child process
printf("Hello from the child process! PID: %d, Parent PID: %d\n", getpid(), getppid());
} else {
// Parent process
printf("Hello from the parent process! Created a child with PID: %d\n", pid);
}
return 0;
}
Program 2: Write a program to demonstrate Parent-Child Process Synchronization in
C using fork() and wait()
Description:
This C program demonstrates **process creation and synchronization** using the `fork()`
and `wait()` system calls in QNX RTOS>
1. The **parent process** starts and prints its PID.
2. It then creates a **child process** using `fork()`.
3. The child process:
- Prints its own PID.
- Simulates some work using `sleep(3)`.
- Prints a completion message.
4. The parent process:
- Waits for the child to finish using `wait()`.
- Checks the exit status of the child.
- Prints a message indicating the child’s exit status.
- Then prints its own completion message.
This program is a basic example of how a parent process can manage and synchronize with
its child process.
Expected Output:
The output will look something like this (note: PIDs will vary):
Parent process started. PID: 12345
Child process running. PID: 12346
Child process completed.
Parent detected child exited with status: 0
Parent process finishing.
Note: The child process output may appear before the parent’s final messages due to the
asynchronous nature of process execution.
Program2:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
pid_t pid;
printf("Parent process started. PID: %d\n", getpid());
pid = fork();
if (pid < 0) {
perror("fork failed");
return 1;
}
else if (pid == 0) {
// Child process
printf("Child process running. PID: %d\n", getpid());
sleep(3); // Simulate some work
printf("Child process completed.\n");
}
else {
// Parent process
int status;
wait(&status); // Wait for child to finish
if (WIFEXITED(status)) {
printf("Parent detected child exited with status: %d\n", WEXITSTATUS(status));
} else {
printf("Child did not terminate normally.\n");
}
printf("Parent process finishing.\n");
}
return 0;
}
Program3: The program that demonstrates process creation and execution using fork()
and execl() in a QNX environment:
Descrption:
Starts a parent process.
Creates a child process using fork().
The child process uses execl() to execute the ls -l command.
The parent waits for the child to finish using wait().
Expected output
Parent process started. PID: 827394
Child process executing 'ls' command:
total 25
-rwxr-xr-x 1 root root 7512 2025-07-13 15:22 Program3
-rw-r--r-- 1 root root 36 2025-06-21 05:06 keep_files
-rwxr-xr-x 1 root root 11704 2025-06-22 04:53 nameServer
-rwxr-xr-x 1 root root 7520 2025-07-02 15:00 server_Simple_iov
Parent process detected child finished.
Program3:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
pid_t pid;
printf("Parent process started. PID: %d\n", getpid());
pid = fork();
if (pid < 0) {
perror("fork failed");
return 1;
}
else if (pid == 0) {
// Child process
printf("Child process executing 'ls' command:\n");
execl("/proc/boot/ls", "ls", "-l", NULL); // Replace with ls -l
perror("execl failed"); // Only runs if exec fails
return 1;
}
else {
// Parent process
int status;
wait(&status); // Wait for child to finish
printf("Parent process detected child finished.\n");
}
return 0;
}
Program 4: Write a C program that demonstrates how to create and manage multiple
child processes using fork() and wait() in a QNX Neutrino RTOS environment.
Description/Algorithm:
This program demonstrates process creation and management using the POSIX API,
which is supported by QNX. It creates a fixed number of child processes using the fork()
system call and ensures that the parent process waits for all child processes to complete using
wait().The key components used in the program are
1. Header Files:
o stdio.h and stdlib.h: For input/output and general utilities.
o unistd.h: Provides access to the fork() and sleep() functions.
o sys/wait.h: Provides the wait() function to synchronize with child processes.
2. Constants:
o NUM_CHILDREN: Defines how many child processes will be created.
3. Process Creation:
o A for loop runs NUM_CHILDREN times.
o Each iteration calls fork():
▪ If fork() returns 0, it's a child process.
▪ If fork() returns a positive value, it's the parent process.
▪ If fork() returns a negative value, an error occurred.
4. Child Process Behavior:
o Prints its own PID and its parent’s PID.
o Simulates work using sleep(2).
o Prints a message before exiting.
5. Parent Process Behavior:
o After creating all children, it enters another loop to wait for each child to
finish using wait(NULL).
o Once all children have exited, it prints a final message.
Expected output
Child 1 (PID: 974869) created by parent (PID: 974850)
Child 2 (PID: 974871) created by parent (PID: 974850)
Child 3 (PID: 974872) created by parent (PID: 974850)
Child 1 (PID: 974869) exiting
Child 3 (PID: 974872) exiting
Child 2 (PID: 974871) exiting
Parent process (PID: 974850) finished waiting for all children.
Program4:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#define NUM_CHILDREN 3
int main() {
pid_t pid;
int i;
for (i = 0; i < NUM_CHILDREN; i++) {
pid = fork();
if (pid < 0) {
perror("Fork failed");
exit(EXIT_FAILURE);
} else if (pid == 0) {
// Child process
printf("Child %d (PID: %d) created by parent (PID: %d)\n", i + 1, getpid(), getppid());
sleep(2); // Simulate some work
printf("Child %d (PID: %d) exiting\n", i + 1, getpid());
exit(EXIT_SUCCESS);
}
// Parent continues to next iteration
}
// Parent waits for all children to finish
for (i = 0; i < NUM_CHILDREN; i++) {
wait(NULL);
}
printf("Parent process (PID: %d) finished waiting for all children.\n", getpid());
return 0;
}
Program 5: Write a QNX program that creates and detects the termination
of only one child process
Description/Algorithm:
This program demonstrates how to create a single child process and detect its termination
using the POSIX-compliant system calls available in QNX Neutrino RTOS.
Key Functionalities
1. Process Creation:
o The program uses fork() to create one child process.
o If fork() fails, the program prints an error and exits.
2. Child Process Behavior:
o The child prints a message indicating it has started.
o It simulates work using sleep(2).
o It then prints a message before exiting with a specific exit status (42).
3. Parent Process Behavior:
o The parent uses wait() to block until the child process terminates.
o It checks the termination status using macros:
▪ WIFEXITED(status) confirms normal termination.
▪ WEXITSTATUS(status) retrieves the exit code.
4. Output:
o The program prints messages showing the lifecycle of the child process and
the parent’s detection of its termination.
Expected output:
Child process started (PID: 1347605)
Child process exiting (PID: 1347605)
Child with PID 1347605 terminated normally with exit status 42
Parent process finished.
Program5:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
pid_t pid, terminated_pid;
int status;
// Create one child process
pid = fork();
if (pid < 0) {
perror("Fork failed");
exit(EXIT_FAILURE);
} else if (pid == 0) {
// Child process
printf("Child process started (PID: %d)\n", getpid());
sleep(2); // Simulate some work
printf("Child process exiting (PID: %d)\n", getpid());
exit(42); // Exit with a specific status
}
// Parent process: wait for child to terminate
terminated_pid = wait(&status);
if (terminated_pid > 0) {
if (WIFEXITED(status)) {
printf("Child with PID %d terminated normally with exit status %d\n",
terminated_pid, WEXITSTATUS(status));
}
}
else {
perror("wait failed");
}
printf("Parent process finished.\n");
return 0;
}
Program 6: Example Program on signals to waits for `SIGUSR1` and prints
a message when it receives it.
Description/Algorithm:
This program demonstrates how to implement basic signal handling in a QNX Neutrino RTOS
environment using the signal() function. It registers a custom handler for the SIGUSR1 signal,
which is a user-defined signal typically used for inter-process communication or custom
notifications.
Upon execution, the program:
• Prints its process ID (PID) so that another process or user can send SIGUSR1 to it.
• Enters an infinite loop, periodically sleeping to reduce CPU usage.
• Waits for the SIGUSR1 signal.
• When the signal is received, the registered handler function is invoked, printing a
message indicating the signal was received.
Expected output:
Process ID: 1470466
Waiting for SIGUSR1...
Received signal 16
Program6:
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
void handler(int sig) {
printf("Received signal %d\n", sig);
}
int main() {
// Register signal handler
signal(SIGUSR1, handler);
printf("Process ID: %d\n", getpid());
printf("Waiting for SIGUSR1...\n");
// Infinite loop to keep the program running
while (1) {
sleep(1); // Sleep to reduce CPU usage
}
return 0;
}