How to send signals? - c

I created signals. One of them prints "1" 20 times. Another prints "2" 20 times.I want these signals to print their numbers in turn:1,2,1,2...But the program prints only "1".Can someone help me with that?

Run this code and see what when you send SIGUSR1 or SIGUSR2 to the pid printed.
#include <stdio.h>
#include <signal.h>
void handler1(int signal)
{
printf("Foo\n");
}
void handler2(int signal)
{
printf("Bar\n");
}
int main()
{
printf("PID: %d\n", getpid());
sigset(SIGUSR1,handler1);
sigset(SIGUSR2,handler2);
while (1);
return(0);
}

Related

Continue executing a stopped process with SIGALRM in C

the job is to continue executing a child process I stopped when I receive a SIGALRM signal.
so far I did the following, which doesn't seems to work:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
void handler(int sig)
{
printf("hello from the handler\n");
kill(getpid(),SIGCONT);
printf("child is continuing executing");
}
int main()
{
int pid1=fork();
signal(SIGALRM,handler);
if (pid1==0) {
kill(getpid(),SIGTSTP);
printf(" I am in the child\n");
} else {
printf("i am in the parent \n");
kill(pid1,SIGALRM);
}
}
I've tried many variations of the code, but printf("I am in the child"); is never executed.
Re: the question asked in a comment is ("how can I make the kill(pid1,SIGALRM) send the signal to the child?"). The call kill(pid1,SIGALRM) does send the signal to the child, but the child does not respond to it because it is stopped. The question asked is somewhat ambiguous, as it is not clear who "I" refers to in the phrase "when I receive a SIGALRM signal". If you want to have the child continue when the child receives a SIGALRM, you can't. You must send the child a SIGCONT before it will do anything. If you want the child to continue when the parent receives the SIGALRM, you could so something like:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
pid_t pid1;
void handler(int sig)
{
(void)sig;
if( pid1 ) {
kill(pid1, SIGCONT);
}
}
int main(void)
{
signal(SIGALRM, handler);
pid1 = fork();
if( pid1 == 0 ) {
kill(getpid(), SIGTSTP);
printf("Child continued\n");
} else {
alarm(1);
pause();
}
}

Using signals in c

I was writing a simple program in c where via fork i create i child process:
#include <stdio.h>
#include <stdlib.h>
#include<signal.h>
#include<sys/types.h>
int handler(){
}
int main(int argc, char **argv)
{
pid_t c=fork();
if(c>0){
sleep(1);
printf("f:pid is %d \n",getpid());
kill(c,SIGINT);
wait(NULL);
}
if(c==0){
pause();
signal(SIGINT,handler);
printf("child:pid is %d \n",getpid());
}
}
The problem is that the child prints nothing. I thought that pause just waits for a signal to unpause the process and i can't understand why the print never happens.Any ideas?
You need to set up the handler before you pause. Otherwise, pause() will be interrupted by the signal, then the default action of the signal will be taken, which is to terminate the process. It will never add the handler because the process is killed first.
if(c==0){
signal(SIGINT,handler);
pause();
printf("child:pid is %d \n",getpid());
}

How to avoid scanf after my signal handler function in C?

I have this code:
#include <stdio.h>
#include <signal.h>
void signal_handler(int signal) {
printf("Caught signal in CHILD.\n");
}
int main(void) {
int s;
signal(SIGTSTP, signal_handler);
while(1){
printf("%s#%s/# ",getlogin(),get_current_dir_name());
scanf("%d",&s);
}
return 0;
}
when i run the code it prints:
something: ^ZCaught signal in CHILD.
As far i understand that the scanf doesn't execute when i press the ctr-z. Although after the printf inside my function it goes straight to the scanf, waits for input and then starts the loop again.Is there any way to avoid scanf when i press ctr-z and start the while loop again? I tried something like that
void signal_handler(int signal) {
printf("Caught signal in CHILD.\n");
printf("%s#%s/# ",getlogin(),get_current_dir_name());
}
but it didn't work. After the second printf goes straight to the scanf, waits for input and then starts the loop again. Can i, somehow, start the loop again?
The signal handler is interrupting scanf during its read of STDIN. However, because of the way you set signal disposition, the read system call restarts immediately upon return of the signal handler. That's why you are "stuck" in the scanf rather than back at the top of your loop.
One important thing you can do is to use sigaction rather than signal. This will force you to specify the behavior of interrupted calls: restart them or not?
The next thing to do is to limit your signal handlers to functions that are async-signal-safe, lest you risk misery.
As an aside, another change to make is to give us all the required includes (<unistd.h>?) and defines (_GNU_SOURCE ?) to make your program work.
As commented the worst solution should be:
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
void signal_handler(int signal) {
printf("Caught signal in CHILD.\n");
exit(1);
}
int main(void) {
int s;
signal(SIGTSTP, signal_handler);
while(1){
printf("test\n");
scanf("%d",&s);
}
return 0;
}
Better solution
#include <stdio.h>
#include <signal.h>
static volatile int keepRunning = 1;
void signal_handler(int signal) {
printf("Caught signal in CHILD.\n");
keepRunning = 0;
}
int main(void) {
int s;
signal(SIGTSTP, signal_handler);
while(keepRunning){
printf("test\n");
scanf("%d",&s);
}
return 0;
}
EDIT after comments
#include <stdio.h>
#include <signal.h>
static volatile int skipPrintf= 1;
void signal_handler(int signal) {
printf("Caught signal in CHILD.\n");
skipPrintf= 1;
}
int main(void) {
int s;
signal(SIGTSTP, signal_handler);
while(1){
if (skipPrintf == 0)
{
printf("test\n");
}
else
{
skipPrintf = 0;
}
scanf("%d",&s);
}
return 0;
}

How to handle SIGHLD

I'm having some troubles using sigchld...
what I want to do is to create a child process with fork and make the child print and sleep a second for a couple of times... during these process I want to send signal to child (SIGSTOP and SIGCONTINUED) and I want the parent to print what the signal was... here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
void handler (int i) {
int x;
waitpid(-1,&x, 0);
printf("WIFSTOPPED=%d, WIFCONTINUED=%d\n", WIFSTOPPED(x),WIFCONTINUED(x) );
}
int main(){
int x;
int q=fork();
if(q==0){
int i=0;
printf("%d\n",getpid());
while (i<20){
printf("%d\n", i++);
sleep(1);
}
_exit(0);
}
else {
signal(SIGCHLD, handler);
waitpid(-1,&x, 0);
while(WIFEXITED(x)!=1){
waitpid(-1,&x, 0);
sleep(1);
}
exit(0);
}
}
but it doesn't work beacause when I send a SIGSTOP or SIGCONTINUED to the child, the child stop and continue but the parent doesn't print anything
any suggestion?
Your handler shall not call waitpid again and you main while loop is also not correct : again you call waitpid twice the first time. And last, your waitpid call much declare to be interested in status changes (WUNTRACED option).
A much correct code could be :
void handler (int i) { // a handler just handle the fact some signal occured
printf("in handler\n");
}
int main(){
int x;
int q=fork();
if(q==0){
int i=0;
printf("%d\n",getpid());
while (i<20){
printf("%d\n", i++);
sleep(1);
}
_exit(0);
}
else {
signal(SIGCHLD, handler); // catch child status changes
do {
waitpid(-1,&x, WUNTRACED|WCONTINUED); // wait until child terminates or changes its status
if (WIFSTOPPED(x)|WIFCONTINUED(x)) // test what really happens
printf("STOPPED=%d, CONTINUED=%d\n", WIFSTOPPED(x),WIFCONTINUED(x) );
} while(!WIFEXITED(x));
exit(0);
}
}

How do I use the SIGALRM correctly?

I'm made this code, and I have to use the alarm signal (SIGALRM) to make the program print the message “I am alive.” every 3 seconds.
But it doesn't work, it sends the message "I'm Alive" only when I press CTR-C, I'm guessing
I didn't put the SIGALRM function in the right place, can you help me?
#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
unsigned Count = 0; //Counts the number of times it receives the signal SIGINT.
void mypause(int sign); //prototype of the function my pause.
void mypause(int sign) {
signal(SIGALRM, mypause); //Set alarm clock for 3 seconds.
alarm(3);
printf("I'm Alive");
signal(SIGINT, mypause);
switch (sign) {
case SIGINT:
printf("\nPressed CTR-C\n");
printf("I'm running, waiting for a sign\n");
Count++;
break;
case SIGQUIT:
printf("\nPressed CTR-\\n");
printf("You pressed CTR-C %d times", Conta);
exit(0); //Exit program.
break;
}
}
int main() {
signal(SIGALRM, mypause);
signal(SIGINT, mypause);
signal(SIGQUIT, mypause);
printf("\nI'm running waiting for a signal\n");
while (1) {}
return (0);
}
Maybe add alarm(3) in your main() ?

Resources