Laboratory # 9: Signal Handling

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

Laboratory # 9

Title: SIGNAL HANDLING


AIM:
To understand and use system calls and to understand signals and handle them.

SIGNAL HANDLING
Top Command: Top command displays processor activity of your Linux box and also
displays tasks managed by kernel in real-time. It'll show processor and memory are being used
and other information like running processes.

Output:

Ps Command: Report a snapshot of the current processes. Now to select a single required
process we use aux along with ps.
ps = process status
aux Command:
a = show processes for all users
u = display the process's user/owner
x = also show processes not attached to a terminal
To Search a Particular Process:
Open Firefox in background. To search its details, type:

Output:
You can see the PID and name of the process encircled in red above. With the grep command
you can filter a process you want. Note that it’s giving you two results. The last one is the id of
the grep itself. Grep returns PID of itself too so ignore that and copy the first one. Which is
3149 here in this case.

Sending a Kill signal to a process:


To kill a process many commands are used. Two of them are Kill -15 and Kill -9.
Kill -15 sends a signal called SIGTERM. It kills the process gracefully. It means that
SIGTERM gives time to the process to save its state and also give it the option to ignore the
signal. Kill and Kill -15 are same.
Kill -9 sends a signal call SIGKILL. It kills the process ruthlessly giving it no time to save.
Actually the process isn’t even made aware of the SIGKILL signal since the signal goes
straight to the kernel init. At that point, init will stop the process. The process never gets the
opportunity to catch the signal and act on it.
Now the send the signal using it’s PID like this and the process i.e firefox will be killed.

Compiling a C Program in Linux:


Type the command
gcc -o myProgram myProgram.c
This command will invoke the GNU C compiler to compile your c file myProgram.c and output
(-o) the result to an executable called myProgram.

Executing a C Program in Linux:


Type the command
./myProgram
Just like you use to run the shell script.

Task: Write a program in C with an infinite loop and a custom signal handler to handle at
least kill -15 (SIGTERM) and kill -9 (SIGKILL). Send both these signals (kill -9 and -15)
using your running process’s PID.

Algorithm:

Write an infinite loop in C

Include header file signal.h

Call signal(signal_name, your_custom_signal_Handler) to catch the signal you
will send.

Write your custom signal Handler function below main and write its
prototype before main.

Compile by typing “gcc –o filename filename.c” in shell.

Then execute it by “./filename”
Solution:
Code:
Main program:

Our Custom signal Handler:

Sending Signal:
Run the program in a shell. The infinite loop will start running. Now open another terminal and
search your process by typing the name of executable file you set by gcc command.
You can see an infinite loop going on in the first terminal at the back and the result of search
in the second terminal at front. Now you need the pid 3201 because it is the id of the running
process, ignore the others.
SIGTERM: Use process pid and send signal. Process will catch it. Since the infinite loop
is running the process will ignore the signal as it is busy and it has the option to ignore.

SIGKILL: Now send SIGKILL and kill it. You can see that the process has stopped execution
and has been killed. Also it couldn’t catch the signal SIGKILL.

PREPARED BY UMARAH QASEEM

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy