stockhouse50
asked on
How do I use signals, interrupts and alarms in this program?
I am working on a school lab but I am stuck with understanding exactly how to handle signals.
In the lab I'm supposed to set an alarm based on the duration from the argument list. During the duration of the alarm, anytime I hit (Ctrl + C) a message should pop up saying "Interrupts disabled". I need to handle the alarm signal so when it goes off I will execute a command also from the argument list. The command should be a single word command such as "ls" which lists all directories or "pwd" which lists the present working directory. Also after the alarm goes off I need to reset the interrupts to the default which enables (Ctrl + C) to quit the process whenever pressed.
At the bottom is the code I have so far. My problem is trying to set the interrupt to default after the alarm goes off. I thought maybe in the alarm_hanlder I could reset it to default but that doesn't seem to work.
If I take the line ---> signal(SIGINT, interrupt); out of the while loop I can only use the interrupt once. The second time I press (Ctrl+c) it will actually quit the process.
The next thing that puzzles me is even though the alarm_handler handles the alarm how can I have the alarm_handler execute the command inputed by the user?
The user input is : a.out 4 ls <- argv[1] is "4" which is the duration of alarm and argv[2] is "ls" which is the command to be executed.
I know I need to use execlp(argv[1], argv[2], "NULL"); but I am not sure exactly how to use it in the alarm_handler because I need to pass the values argv[1] and argv[2] to the handler some how? or is the only way to do this by declaring global variables and make these values equal to argv[1] and argv[2]?
In order to not loss my current process I need to create a child process to run execlp();
Perhaps I am doing this completely wrong and I should have the execlp() in main(); although I would be very confused on how that would work?
Thanks in advance.
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
void alarm_handler(int);
void interrupt(int);
void alarm_handler(int dummy){
printf("I received an alarm %d\n", dummy);
signal(SIGINT,SIG_DFL);
/* if ( fork() == 0 )
execlp(argv[1], argv[2], "NULL");
*/
}
void interrupt(int signal){
printf("Interrupt disabled until delayed command is executed\n");
}
int main(int argc, char *argv[]){
int x = atoi( argv[1] );
signal(SIGALRM, alarm_handler);//install handler
alarm(x);
while(1){
signal(SIGINT, interrupt);
sleep(1);
}
}
In the lab I'm supposed to set an alarm based on the duration from the argument list. During the duration of the alarm, anytime I hit (Ctrl + C) a message should pop up saying "Interrupts disabled". I need to handle the alarm signal so when it goes off I will execute a command also from the argument list. The command should be a single word command such as "ls" which lists all directories or "pwd" which lists the present working directory. Also after the alarm goes off I need to reset the interrupts to the default which enables (Ctrl + C) to quit the process whenever pressed.
At the bottom is the code I have so far. My problem is trying to set the interrupt to default after the alarm goes off. I thought maybe in the alarm_hanlder I could reset it to default but that doesn't seem to work.
If I take the line ---> signal(SIGINT, interrupt); out of the while loop I can only use the interrupt once. The second time I press (Ctrl+c) it will actually quit the process.
The next thing that puzzles me is even though the alarm_handler handles the alarm how can I have the alarm_handler execute the command inputed by the user?
The user input is : a.out 4 ls <- argv[1] is "4" which is the duration of alarm and argv[2] is "ls" which is the command to be executed.
I know I need to use execlp(argv[1], argv[2], "NULL"); but I am not sure exactly how to use it in the alarm_handler because I need to pass the values argv[1] and argv[2] to the handler some how? or is the only way to do this by declaring global variables and make these values equal to argv[1] and argv[2]?
In order to not loss my current process I need to create a child process to run execlp();
Perhaps I am doing this completely wrong and I should have the execlp() in main(); although I would be very confused on how that would work?
Thanks in advance.
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
void alarm_handler(int);
void interrupt(int);
void alarm_handler(int dummy){
printf("I received an alarm %d\n", dummy);
signal(SIGINT,SIG_DFL);
/* if ( fork() == 0 )
execlp(argv[1], argv[2], "NULL");
*/
}
void interrupt(int signal){
printf("Interrupt disabled until delayed command is executed\n");
}
int main(int argc, char *argv[]){
int x = atoi( argv[1] );
signal(SIGALRM, alarm_handler);//install handler
alarm(x);
while(1){
signal(SIGINT, interrupt);
sleep(1);
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Actually I think signal(SIGINT,SIG_DFL); does work because I took out the execlp(); line to test if I could exit out of the loop in main() after the alarm goes off.
However, I am still having the problem with CTRL+C only giving me the interrupt message once. If I hit Ctrl+C more than once anytime before the alarm goes off the process will quit. When I tested to see if signal(SIGINT,SIG_DFL); actually enabled CTRL+C I waited for the alarm to go off because I pressed CTRL+C because of the issue above.
Thanks again for the help.
However, I am still having the problem with CTRL+C only giving me the interrupt message once. If I hit Ctrl+C more than once anytime before the alarm goes off the process will quit. When I tested to see if signal(SIGINT,SIG_DFL); actually enabled CTRL+C I waited for the alarm to go off because I pressed CTRL+C because of the issue above.
Thanks again for the help.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi, thanks for the help.
I tried doing exactly what you said but I now get this error when compiling the program.
"labalarm.c", line 19: function designator is not of function type
"labalarm.c", line 41: warning: statement not reached
cc: acomp failed for labalarm.c
Line 19 refers to the signal(SIGINT, interrupt); line inside the interrupt hanlder.
I ran the program even though I had the above errors and it still does the exact same thing.
This is the output when I run it:
>a.out 6
Inside loop
Inside loop
^CInterrupt disabled until delayed command is executed
Inside loop
^C
>
Basically, after I hit CTRL+C a second time the program once again exits.
I am sure exactly what the error message '"labalarm.c", line 19: function designator is not of function type' means but how do I get around this?
Thanks again.
This is what my codes looks like:
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
void alarm_handler(int);
void interrupt(int);
//char command[80];
void alarm_handler(int dummy){
printf("I received an alarm %d\n", dummy);
signal(SIGINT,SIG_DFL);
//execlp(command, "", NULL);
}
void interrupt(int signal){
signal(SIGINT, interrupt);
printf("Interrupt disabled until delayed command is executed\n");
}
int main(int argc, char *argv[]){
int x = atoi( argv[1] );
// Make a local copy of the argument
// strcpy (command, argv[2]);
signal(SIGALRM, alarm_handler);//install handler
alarm(x);
signal(SIGINT, interrupt);
while(1){
printf("Inside loop\n");
sleep(1);
}
return 0;
}
I tried doing exactly what you said but I now get this error when compiling the program.
"labalarm.c", line 19: function designator is not of function type
"labalarm.c", line 41: warning: statement not reached
cc: acomp failed for labalarm.c
Line 19 refers to the signal(SIGINT, interrupt); line inside the interrupt hanlder.
I ran the program even though I had the above errors and it still does the exact same thing.
This is the output when I run it:
>a.out 6
Inside loop
Inside loop
^CInterrupt disabled until delayed command is executed
Inside loop
^C
>
Basically, after I hit CTRL+C a second time the program once again exits.
I am sure exactly what the error message '"labalarm.c", line 19: function designator is not of function type' means but how do I get around this?
Thanks again.
This is what my codes looks like:
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
void alarm_handler(int);
void interrupt(int);
//char command[80];
void alarm_handler(int dummy){
printf("I received an alarm %d\n", dummy);
signal(SIGINT,SIG_DFL);
//execlp(command, "", NULL);
}
void interrupt(int signal){
signal(SIGINT, interrupt);
printf("Interrupt disabled until delayed command is executed\n");
}
int main(int argc, char *argv[]){
int x = atoi( argv[1] );
// Make a local copy of the argument
// strcpy (command, argv[2]);
signal(SIGALRM, alarm_handler);//install handler
alarm(x);
signal(SIGINT, interrupt);
while(1){
printf("Inside loop\n");
sleep(1);
}
return 0;
}
void interrupt(int signal){
needs to be :
void interrupt(int sig){
name clash between the parameter and the function ;)
needs to be :
void interrupt(int sig){
name clash between the parameter and the function ;)
ASKER
Cup helped partially on the question.
I've been off the Unix circuit for some time. I don't know whether people still use signal or not. I thought it had been replaced by sigset about 12 years ago.
ASKER
Also, how do I enable CTRL+C to work after the alarm goes off? I am not sure if signal(SIGINT,SIG_DFL); actually works in the alarm handler.
Thank you.