LP Unit 4
LP Unit 4
• Maskable
• Non-Maskable
• Maskable: - Maskable Signals are the signals that the user can
change or ignore, for example, Ctrl +C.
Non-Maskable: - Non-Maskable Signals are the signals that
the users cannot change or ignore. Non-Maskable signals
mainly occur if a user is signaled for non-recoverable
hardware errors.
What is the Typical Lifecycle of a Signal?
• A signal goes through three stages:
• Generation
• Delivery
• Processing
A List of Common Signals
Signal Signal
Description Default Action
Number Name
Hangup detected on controlling
1 SIGHUP Terminate
terminal
int main() {
signal(SIGCHLD, handle_sigchld); //
Parent process waiting...
Handle SIGCHLD
Child process running...
pid_t pid = fork();
Child process terminated
Suspend and Resume a Process
• In Linux, suspending and resuming processes can be done
using signals. Here’s how you can suspend and resume
processes programmatically or from the command line:
• 1. Suspending a Process:
• SIGSTOP: This signal is used to suspend a process. It cannot
be caught or ignored by the process. When a process receives
this signal, it stops executing until it is resumed.
• SIGTSTP: This signal is usually sent when the user presses
Ctrl+Z in the terminal to stop a process. Unlike SIGSTOP, the
process can catch and handle this signal.
• 2. Resuming a Process
• SIGCONT: This signal is used to resume a process that was
stopped with SIGSTOP or SIGTSTP.
Suspend and Resume a Process
#include <stdio.h> kill(pid, SIGSTOP); // Send SIGSTOP
#include <signal.h> signal to suspend child
#include <unistd.h>
#include <stdlib.h> sleep(3); // Wait for a bit
int main() printf("Resuming child process with
{ SIGCONT...\n");
pid_t pid = fork(); kill(pid, SIGCONT); // Send SIGCONT
if (pid == 0) { signal to resume child
// Child process sleep(3); // Wait and observe the resumed
printf("Child process running with PID: child
%d\n", getpid()); printf("Terminating child process...\n");
while (1) { kill(pid, SIGTERM); // Terminate the child
printf("Child is still running...\n"); process
Child process running with PID: 1234
sleep(2); }
Child is still running...
}
Child is still running...
} return 0;
Suspending child process with
else { }
SIGSTOP...
// Parent process
Resuming child process with
sleep(3); // Allow child to run for a bit
SIGCONT...
Child is still running...
printf("Suspending child process with
Child is still running...
SIGSTOP...\n");
Terminating child process...
Inter-Process Communication (IPC)
• Inter-Process Communication (IPC) is a crucial mechanism in operating systems that
allows different processes to communicate and synchronize their actions. IPC is essential
in multi-process or multi-threaded environments, enabling independent processes to work
together, share data, coordinate actions, and access shared resources.
• Data Sharing: IPC enables processes to share data, which is especially useful when
processes work collaboratively to perform tasks that require access to the same
information.
• Resource Sharing: IPC helps manage access to shared resources, such as files or memory,
preventing conflicts and data corruption.
• Synchronization: IPC provides methods for synchronizing processes, ensuring that tasks
happen in the correct order, especially in time-sensitive applications.
• Efficiency: IPC reduces redundancy by enabling processes to use a single source of data,
which can reduce memory usage and improve system performance.
Common IPC Mechanisms
• Pipes: Pipes are one of the simplest IPC mechanisms. They create a unidirectional
communication channel between processes (typically between a parent and child
process). Pipes are temporary and only exist during the life of the processes that use
them.
• Named Pipes (FIFOs): Named pipes, or FIFOs, are similar to pipes but have a name
in the filesystem, making them accessible by unrelated processes. FIFOs remain in the
system even after processes terminate, allowing different programs to communicate
through a persistent file-like structure.
• Message Queues: Message queues enable processes to send and receive messages in
a queue format, allowing asynchronous communication. Each message can have an
identifier, and processes can retrieve messages based on priority or ID. Message
queues are suitable for structured communication between processes without the need
for direct connections.
Common IPC Mechanisms
• Semaphores: Semaphores are synchronization primitives that help control access to
shared resources. They are often used to avoid race conditions by signaling when a
resource is available or being used. Semaphores can enforce mutual exclusion,
allowing only one process to access a critical section of code at a time.