Systems Programming - Lab8
Systems Programming - Lab8
______________________________________________________________________________
Write C program for each of the problem below. After executing each of the C
program, you will need to capture the output in text file format.
C program file will be named as StudentID_Lab8_x.c
Text file with output captured will be names as StudentID_Lab8_x.txt
Problem 1: This problem teaches how to use alarm signal (SIGALRM). Write a C
program StudentID_Lab8_1.c to do the following:
• main() will wait for user to input the string value using scanf.
• main() will set the signal handler for SIGALRM signal which will be called
when program has waited 10 seconds for user to enter the string.
• Implement alarm handler which will display the message to user that “it took
too long to enter the string” and then exit the program with -1 exist status.
• If user enters string within 10 seconds, main() should exit with status 0
(success)
Problem 3: This exercise teach you how to send signal from one process another to
process or process group belonging to different process groups then the one
sending signal
• Write C program StudentID_Lab8_3_sendsignal.c
o Accept from user process id to which you need to send signal to
o Accept from user choice of whether the signal needs to send to process
or process group (e.g. 1 indicates process and 2 indicates process group)
o Using kill() system call send signal to either process or process group
depending on the 2nd input above. (Hint kill(pid) will send signal to a
specific process with pid as process id and kill(-pid) will send signal to all
the processes in process group of process pid)
• Write C program StudentID_Lab8_3_receivesignal_samepgrp.c
o Implement signal handler for SIGINT signal which just prints the message
using → printf(“Process id %d received SIGINT signal”, getpid());
o Register signal handler for SIGINT using signal() system call
o Create child process and print the pid and process group id for both
parent and child processes using getpid() and getpgid(0) systems calls.
You will notice that getpgid(0) system call print the same process group
number as pid of parent which means parent and child below to the same
process groups
• Write C program StudentID_Lab8_3_receivesignal_diffpgrp.c
o Implement signal handler for SIGINT signal which just prints the message
using → printf(“Process id %d received SIGINT signal”, getpid());
o Register signal handler for SIGINT using signal() system call
o Create child process but this time set new process group of child as child’s
pid using setpgid(0).
o Now print the pid and process group id for both parent and child
processes using getpid() and getpgid(0) systems calls. You will notice that
getpgid(0), prints different process group numbers
• Execution 1:
o First execute StudentID_Lab8_3_receivesignal_samepgrp executable
which is waiting for signal to be received
o Now on different terminal window run executable for
StudentID_Lab8_3_sendsignal
▪ First select process id of parent process running
StudentID_Lab8_3_receivesignal_same and choose option 2 for
sending signal to process group.
▪ You should get the message from signal handler for both parent
and the child process and they should both terminate since they
belong to the same process group. Check using ps commnd
• Execution 2:
o First execute StudentID_Lab8_3_receivesignal_samepgrp executable
which is waiting for signal to be received
o Now on different terminal window run executable for
StudentID_Lab8_3_sendsignal
▪ First select process id of parent (or child) process running
StudentID_Lab8_3_receivesignal_samepgrp and choose option 1
for sending signal to individual process.
▪ You should get the message from signal handler for parent (or
child) process depending which process id you entered and only
that process will terminate. Note that the other process child (or
parent) is still running. Check using ps commnd and kill it.
• Execution 3:
o First execute StudentID_Lab8_3_receivesignal_diffpgrp executable which
is waiting for signal to be received
o Now on different terminal window run executable for
StudentID_Lab8_3_sendsignal
▪ First select process id of parent process running
StudentID_Lab8_3_receivesignal_diffpgrp and choose option 2 for
sending signal to process group.
▪ Even though you selected process id of parent and sending signal
to process group child will not see that signal and will not terminate
because it is in separate process group Hence, you should get the
message from signal handler for parent only and parent process
will terminate. Note that the child process is still running. Check
using ps commnd and kill it.