added more code
This commit is contained in:
BIN
CPE435/Lab8/1646972007.png
Normal file
BIN
CPE435/Lab8/1646972007.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 7.3 KiB |
BIN
CPE435/Lab8/Lab08.pdf
Normal file
BIN
CPE435/Lab8/Lab08.pdf
Normal file
Binary file not shown.
BIN
CPE435/Lab8/lab08
Executable file
BIN
CPE435/Lab8/lab08
Executable file
Binary file not shown.
36
CPE435/Lab8/lab08_demo.c
Normal file
36
CPE435/Lab8/lab08_demo.c
Normal file
@ -0,0 +1,36 @@
|
||||
#include<stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
#include <cstdlib>
|
||||
|
||||
//This function kills the process that makes a call to this function
|
||||
void kill_func(int killSignal)
|
||||
{
|
||||
printf("Received kill signal %d\n",killSignal);
|
||||
printf("\tDying process %d\n",getpid());
|
||||
exit(0);
|
||||
}
|
||||
//this function prints a message and changes the function to handle the SIGINT command to kill_func()
|
||||
void myFunction(int sigVal)
|
||||
{
|
||||
printf("Received signal %d\n", sigVal);
|
||||
printf("Now you can kill me..\n");
|
||||
signal(SIGINT,kill_func);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
//ignore the SIGINT signal
|
||||
// .. SIGINT is ctrl+c
|
||||
// .. SIG_IGN is signal ignore
|
||||
// .since we ignore the signal ctrl+c, you cannot terminate this process with ctrl+c
|
||||
signal(SIGINT,SIG_IGN);
|
||||
//for alarm signal, call function myFunction()
|
||||
// .. means when alarm goes off, myFunction will be called
|
||||
signal(SIGALRM,myFunction);
|
||||
//set alarm for 15 seconds from now
|
||||
alarm(15);
|
||||
printf("This gets printed as soon as alarm is called\n");
|
||||
//just running infinitely
|
||||
while(1);
|
||||
}
|
36
CPE435/Lab8/lab08_demo.cpp
Normal file
36
CPE435/Lab8/lab08_demo.cpp
Normal file
@ -0,0 +1,36 @@
|
||||
#include<stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
#include <cstdlib>
|
||||
|
||||
//This function kills the process that makes a call to this function
|
||||
void kill_func(int killSignal)
|
||||
{
|
||||
printf("Received kill signal %d\n",killSignal);
|
||||
printf("\tDying process %d\n",getpid());
|
||||
exit(0);
|
||||
}
|
||||
//this function prints a message and changes the function to handle the SIGINT command to kill_func()
|
||||
void myFunction(int sigVal)
|
||||
{
|
||||
printf("Received signal %d\n", sigVal);
|
||||
printf("Now you can kill me..\n");
|
||||
signal(SIGINT,kill_func);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
//ignore the SIGINT signal
|
||||
// .. SIGINT is ctrl+c
|
||||
// .. SIG_IGN is signal ignore
|
||||
// .since we ignore the signal ctrl+c, you cannot terminate this process with ctrl+c
|
||||
signal(SIGINT,SIG_IGN);
|
||||
//for alarm signal, call function myFunction()
|
||||
// .. means when alarm goes off, myFunction will be called
|
||||
signal(SIGALRM,myFunction);
|
||||
//set alarm for 15 seconds from now
|
||||
alarm(15);
|
||||
printf("This gets printed as soon as alarm is called\n");
|
||||
//just running infinitely
|
||||
while(1);
|
||||
}
|
BIN
CPE435/Lab8/program1
Executable file
BIN
CPE435/Lab8/program1
Executable file
Binary file not shown.
107
CPE435/Lab8/program1.cpp
Normal file
107
CPE435/Lab8/program1.cpp
Normal file
@ -0,0 +1,107 @@
|
||||
#include<stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
using namespace std;
|
||||
|
||||
//this function prints a message and changes the function to handle the SIGINT command to kill_func()
|
||||
void myFunction(int sigVal);
|
||||
|
||||
// handles after alarm alarm
|
||||
void alarmFunction(int ignore);
|
||||
int child;
|
||||
|
||||
// handles SIG_INT while the alarm is active
|
||||
void ignoreFunc(int ignore);
|
||||
//This function kills the process that makes a call to this function
|
||||
void kill_func(int killSignal);
|
||||
|
||||
// handles killing the child
|
||||
void childKillFunc(int kill);
|
||||
|
||||
int main()
|
||||
{
|
||||
child = fork();
|
||||
// ignore the SIGINT signal
|
||||
// .. SIGINT is ctrl+c
|
||||
// .. ignoreFunc is the user-defined signal handler
|
||||
//
|
||||
signal(SIGINT,ignoreFunc);
|
||||
// for alarm signal, call function alarmFunction()
|
||||
// .. means when alarm goes off, alarmFunction will be called
|
||||
|
||||
signal(SIGALRM,alarmFunction);
|
||||
//set alarm for 15 seconds from now
|
||||
alarm(10);
|
||||
//printf("This gets printed as soon as alarm is called\n");
|
||||
// just running infinitely
|
||||
while(1);
|
||||
}
|
||||
|
||||
void myFunction(int sigVal)
|
||||
{
|
||||
printf("Received signal %d\n", sigVal);
|
||||
printf("Now you can kill me..\n");
|
||||
signal(SIGINT,kill_func);
|
||||
}
|
||||
|
||||
void alarmFunction(int ignore)
|
||||
{
|
||||
if (child== 0)
|
||||
{
|
||||
//define user signal and kill(child, USRSIG1) to send signal to child
|
||||
|
||||
signal(SIGINT,SIG_IGN);
|
||||
signal(SIGUSR1, childKillFunc);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Received signal %d\n", ignore);
|
||||
printf("Now you can kill me..\n");
|
||||
signal(SIGINT,kill_func);
|
||||
}
|
||||
}
|
||||
|
||||
void kill_func(int killSignal)
|
||||
{
|
||||
if(child == 0)
|
||||
{
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
//define user signal and kill(child, USRSIG1) to send signal to child
|
||||
|
||||
printf("Sending signal to child\n");
|
||||
kill(child, SIGUSR1);
|
||||
printf("Received kill signal %d\n",killSignal);
|
||||
printf("\tDying process %d\n",getpid());
|
||||
exit(0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void childKillFunc(int kill)
|
||||
{
|
||||
if (child == 0)
|
||||
{
|
||||
cout << "Received kill signal" << endl;
|
||||
cout << "Child Process " << getpid() << " is dying" << endl;
|
||||
exit(0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void ignoreFunc(int ignore)
|
||||
{
|
||||
|
||||
if(child == 0)
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "Can't kill processes now\n";
|
||||
}
|
||||
}
|
BIN
CPE435/Lab8/program1.png
Normal file
BIN
CPE435/Lab8/program1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 6.7 KiB |
BIN
CPE435/Lab8/program2
Executable file
BIN
CPE435/Lab8/program2
Executable file
Binary file not shown.
39
CPE435/Lab8/program2.cpp
Normal file
39
CPE435/Lab8/program2.cpp
Normal file
@ -0,0 +1,39 @@
|
||||
#include<stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
using namespace std;
|
||||
|
||||
// function that prints gibberish
|
||||
// for 10 seconds and then kills
|
||||
// the program
|
||||
void alarmFunction(int alarmVal);
|
||||
|
||||
|
||||
// kills program
|
||||
void killFunction(int killPID);
|
||||
|
||||
int main()
|
||||
{
|
||||
signal(SIGINT,alarmFunction);
|
||||
while (1);
|
||||
}
|
||||
|
||||
void alarmFunction(int alarmVal)
|
||||
{
|
||||
signal(SIGINT, SIG_IGN);
|
||||
signal(SIGALRM,killFunction);
|
||||
alarm(10);
|
||||
while (1)
|
||||
{
|
||||
printf(" I love coding! ");
|
||||
}
|
||||
}
|
||||
|
||||
void killFunction(int killPID)
|
||||
{
|
||||
printf("\n");
|
||||
exit(0);
|
||||
}
|
BIN
CPE435/Lab8/program2Output.png
Normal file
BIN
CPE435/Lab8/program2Output.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 7.3 KiB |
Reference in New Issue
Block a user