0% found this document useful (0 votes)
2 views

Process Signals Programs

The document provides examples of signal handling and process control in C on Linux, including a child process killing its parent, a stopwatch using signals, and a signal handler with sigaction. It demonstrates how to manage signals like SIGINT, SIGTSTP, and SIGKILL, along with sample outputs for each example. The code snippets illustrate the use of fork, kill, and signal functions in managing process behavior.

Uploaded by

naeemhuzaifah0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Process Signals Programs

The document provides examples of signal handling and process control in C on Linux, including a child process killing its parent, a stopwatch using signals, and a signal handler with sigaction. It demonstrates how to manage signals like SIGINT, SIGTSTP, and SIGKILL, along with sample outputs for each example. The code snippets illustrate the use of fork, kill, and signal functions in managing process behavior.

Uploaded by

naeemhuzaifah0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Signal Handling and Process Control in

C (Linux)
1. Child Kills Parent (fork + kill)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>

int main() {
pid_t pid = fork();

if (pid < 0) {
perror("Fork failed");
exit(1);
} else if (pid == 0) {
// Child process
printf("Child process started. PID: %d\n", getpid());
sleep(1); // Let the parent get ready
kill(getppid(), SIGKILL); // Kill parent
printf("Parent process killed by child.\n");
} else {
// Parent process
printf("Parent process PID: %d\n", getpid());
while (1); // Stay alive until killed
}

return 0;
}

2. Stopwatch using Signals


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>

void Times_Up() {
printf("\n( TIME FINISH )\n");
exit(0);
}

void Aborted(int signum) {


printf("\nOPERATION ABBORTED\n");
exit(0);
}

int main() {
signal(SIGTSTP, Aborted); // Ctrl+Z handling
pid_t pid = fork();

if (pid == 0) {
// Child process
int seconds;
printf("Enter time (in seconds): ");
scanf("%d", &seconds);
for (int i = seconds; i >= 0; i--) {
printf("Time Remaining:%d\n", i);
sleep(1);
}
kill(getppid(), SIGALRM);
exit(0);
} else {
// Parent process
signal(SIGALRM, Times_Up);
wait(NULL); // Wait for child
}

return 0;
}

Sample Output for Stopwatch


$ ./Stop_Watch
$ Enter time (in seconds): 5
$ Time Remaining:5
$ Time Remaining:4
$ Time Remaining:3
$ Time Remaining:2
$ Time Remaining:1
$ Time Remaining:0
$ ( TIME FINISH )

$ ./Stop_Watch
$ Enter time (in seconds): 5
$ Time Remaining:5
$ Time Remaining:4
$ Time Remaining:3 (Press Ctrl+Z)
$ OPERATION ABBORTED

3. P1.c - Signal Handler with sigaction


#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>

void handler(int signum) {


if (signum == SIGINT)
printf("SIGINT Received\n");
else if (signum == SIGTSTP)
printf("SIGTSTP Received\n");
}

int main() {
struct sigaction sa;
sa.sa_handler = handler;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);

sigaction(SIGINT, &sa, NULL);


sigaction(SIGTSTP, &sa, NULL);

printf("P1 Running with PID: %d\n", getpid());

while (1); // Infinite loop


return 0;
}

P2.c - Send signals to P1


#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>

int main() {
pid_t p1_pid;
char input;

printf("Enter PID of P1: ");


scanf("%d", &p1_pid);

while (1) {
printf("Enter signal (C=SIGINT, Z=SIGTSTP, K=SIGKILL): ");
scanf(" %c", &input);

if (input == 'C')
kill(p1_pid, SIGINT);
else if (input == 'Z')
kill(p1_pid, SIGTSTP);
else if (input == 'K') {
kill(p1_pid, SIGKILL);
kill(getpid(), SIGKILL);
}
}

return 0;
}

Sample Output for P1 and P2


$ ./P1
P1 Running with PID: 12345

$ ./P2
Enter PID of P1: 12345
Enter signal (C=SIGINT, Z=SIGTSTP, K=SIGKILL): C
-> SIGINT Received
Enter signal (C=SIGINT, Z=SIGTSTP, K=SIGKILL): Z
-> SIGTSTP Received
Enter signal (C=SIGINT, Z=SIGTSTP, K=SIGKILL): K
-> (P1 and P2 both killed)

You might also like