LABORATORY-6
Avoiding Race Condition using System Call in Linux
A signal is just like an interrupt when it is generated at the user level a call is made to the kernel of the OS, which then acts accordingly
a.) Maskable
b.) Non-Maskable
System Call: A system call is a procedure that provide the interface between a process and the OS. it is the way by which a computer program requests a service from the kernel of the OS.
Types of System Calls:-
System Calls:-
a.) Process Control :- fork(), exit(), exec()
b.) Device Management:- loctl();
c.) File Management:- open(), read(), write(), close()
d.) Information Maintenance:- getpid(), alarm(), sleep()
e.) Communication:- pipe(), shmget(), mmap()
SOME IMPORTANT SIGNALS:-
1.) SIGPWR:- POWER FAILURE
2.) SIGINT:- INTERRUPT FROM KEYBOARD
3.) SIGILL:- ILLEGAL INSTRUCTION
4.) SIGFPE:- FLOATING POINT EXECUTION
5.) SIGSEGV:- INVALID MEMORY REFERENCE
6.) SIGBUS:- BUS ERROR[BAD MEMORY ACCESS]
7.) SIGIO:- I/O NOW POSSIBLE
8.) SIGPIPE:- BROKEN PIPE: WITE THE PIPE WITH NO READERS.
9.) SIGKILL:- KILL SIGNAL *
10.) SIGSTOP:- STOP PROCESS
11.) SIGTSTP:- STOPED TYPES AT TERMINAL
12.) SIGUSR1:- USER DEFINED SIGNAL-1
13.) SIGHUP:- HANGUP DETECTED ON CONTROLLING TERMINAL ON DEATH OF CONTROLLING PROCESS.
14.) SIGALRM:- TIMER SIGNAL
Code for SIGINT
#include "stdio.h"
#include "unistd.h"
#include "signal.h"
void oh(int sig){
printf("I got signal %d\n", sig);
signal(SIGINT,oh);
}
int main(){
signal(SIGINT,oh);
while(1){
printf("Hello World!");
sleep(1);
}
}
Code for SIGKILL
#include "stdio.h"
#include "unistd.h"
#include "signal.h"
int main(){
pid_t pid, ppid, cpid;
ppid = getpid();
pid = fork();
if(ppid == getpid()){
printf("PARENT");
}
else if(cpid == getpid()){
printf("CHILD");
}
if(pid > 0){
int i = 0;
while(i++ < 10){
printf("In the parent process");
sleep(1);
}
}
else if(pid == 0){
int i=0;
while(i++ < 10){
printf("In the child process");
sleep(1);
if(i == 3){
kill(ppid,SIGKILL);
printf("Parent Killed, I'm Orphan!!!!!!!!!!);
}
}
}
else{
printf("Something bad happened");
exit(EXIT_FAILURE);
}
return 0;
}
Code for SIGDFL
#include "stdio.h"
#include "unistd.h"
#include "signal.h"
void oh(int sig){
printf("I got signal",sig);
signal(SIGINT, SIG_DFL);
}
int main(){
signal(SIGINT,oh);
while(1){
printf("Hello World\n);
sleep(1);
}
}
Code for SIGQUIT
#include "stdio.h"
#include "unistd.h"
#include "signal.h"
void oh(int sig){
printf("I got signal",sig);
signal(SIGQUIT, oh);
}
int main(){
signal(SIGQUIT,oh);
while(1){
printf("Hello World\n);
sleep(1);
}
}
Code for SIGALRM
#include "stdio.h"
#include "unistd.h"
#include "signal.h"
void sig_oh(int sig){
printf("ALARM TING TING........");
signal(SIGALRM,sig_oh);
}
int main(){
signal(SIGALRM,sig_oh);
alarm(2);
while(1){
printf("Hello World\n");
sleep(1);
}
}