Please help my SIGUSR1 is not being received and I do not know what is wrong. I am doing this for practice in Linux. I exit the process in using ctrl+z and then type in kill -SIGUSR1 but the signal is not being received.
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <signal.h>
void sig_handler(int signo){
switch(signo)
case SIGUSR1:
printf("Hello\n");
exit(0);
}
int main()
{
pid_t pid;
printf("PID: %i\n", getpid());
while(1)
{
if(signal(SIGUSR1, sig_handler) == SIG_ERR)
{
printf("Error\n");
}
sleep(2);
}
return 0;
}
Try these steps
step 1 : run in background
bash# ./a.out &
[1] 4338
# PID: 4338
step 2: issue SIGUSR1 from bash to the pid
bash# kill -10 4338
10 is for SIGUSR1
Pressing the ctrl+z key causes the system to send a TSTP signal (SIGTSTP) to the running process and causes the process to suspend its execution.
Hence when you send kill -SIGUSR1 on suspended process, SIGUSR1 signal will be blocked.
For example:
Run the process
$ ./a.out
PID*****: 10869
Suspend the process
^Z //Pressed ctrl+z
[1]+ Stopped ./a.out
$ ps
PID TTY TIME CMD
7529 pts/0 00:00:00 bash
10869 pts/0 00:00:00 a.out
10870 pts/0 00:00:00 ps
Send SIGUSR
$ kill -SIGUSR1 10869
$
/*SIGUSR is blocked until process resumes*/
Resume the process.
$ fg
./a.out
Hello***
SIGUSR is sent to process
$ ps
PID TTY TIME CMD
7529 pts/0 00:00:00 bash
10871 pts/0 00:00:00 ps
Solution:
Use & to run your process in background and send SIGUSR1 signal as below.
$ ./a.out &
[1] 11400
$ PID*****: 11400
$kill -SIGUSR1 11400
$Hello***
$
[1]+ Done ./a.out
Related
I'm guessing I need to call some function in my signal handler and I have no idea what it is
My program wants to sleep until it receives some kind of signal. So naturally I used pause(). Now it ran just fine when I sent my app SIGUSR1 however I get a print out of User defined signal 1 which is not what I want because I plan to send many signals and it will make the console unreadable. I tried handling the signal by writing signal(SIGUSR1, mySignalHandler); but now pause() no longer resumes after I receive the signal. Is there some kind of function I need to call from mySignalHandler?
The default behavior when a program receive a USR1 signal is to terminate (see https://www.man7.org/linux/man-pages/man7/signal.7.html, standard signal).
An empty signal handle will just do what you want: no message will be displayed.
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
static int s_got = 0;
void handler(int s)
{
++s_got;
}
int main(void)
{
signal(SIGUSR1, handler);
while(1) {
pause();
printf("resumed %d\n", s_got);
}
return 0;
}
:~/so/pause$ gcc -Wall main.c
:~/so/pause$ ./a.out &
[1] 2286
:~/so/pause$ pkill -USR1 a.out
resumed 1
:~/so/pause$ pkill -USR1 a.out
resumed 2
:~/so/pause$ pkill -USR1 a.out
resumed 3
:~/so/pause$ pkill -USR1 a.out
resumed 4
:~/so/pause$ pkill -USR1 a.out
resumed 5
:~/so/pause$
#include <stdio.h>
#include <sys/wait.h>
#include <pthread.h>
void* runfunc(void* arguments){
printf("in run thread\n");
return NULL;
}
int main(void)
{
pthread_t pthread_id;
pthread_create(&pthread_id, NULL, runfunc, NULL);
printf("after pthread creation\n");
return 0;
}
$ gcc -pthread intro.c
$ ./a.out
after pthread creation
in run thread
in run thread
$ ./a.out
after pthread creation
$
There are 4 types of outputs for this program on linux.
"after pthread creation" -> this output can be easily understood as before thread execution process completes and exits and deletes all threads.
"after pthread creation
in " -> this output can also be understood as while process was exiting thread executed a little bit to print in
"in running thread
after pthread creation" -> this output can also be easily understood
"after pthread creation
in running thread
in running thread" -> now i don't get why am i getting "in running thread" 2 times instead of once.
Linux implementation of threads is considered.
My Goal: I have a simple c program that should overwrite the default SIGTSTP handler with my own, and send a SIGTSTP only to the child process.
My Issue: The kill call within the SIGTSTP handler stops the parent process, and exits my program (not just the child). What am I doing wrong?
Edit: This problem seems to only happen when I compile and run my code using the following make command: gcc -o program *.c -lreadline && ./program. It seems (the make process?) is terminated because my output contains the following line upon ctrl-z:
gcc -o sandbox *.c -lreadline && ./sandbox
Is there a way to both get my program to have the desired functionality and use make?
My Code:
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <stdlib.h>
#include <readline/readline.h>
int child;
void handler();
static void SIGTSTP_Handler()
{
if (child != 0) {
kill(child, SIGTSTP);
}
}
int main(void)
{
signal(SIGTSTP, SIGTSTP_Handler);
child = fork();
if (child == 0) {
setpgid(0, getpid());
printf("CHILD's PID ::: [ %d ]\n", getpid());
printf("CHILD's GROUP ::: %d\n", getpgrp());
execlp("sleep", "sleep", "30", NULL);
}
else {
setpgid(child, child);
int status;
printf("CHILD's PID (From Parent Perspective) ::: [ %d ]\n", child);
printf("PARENT's PID ::: %d\n", getpid());
printf("PARENT's GROUP ::: %d\n", getpgrp());
waitpid(child, &status, WUNTRACED | WCONTINUED);
}
while (1);
}
The issue was caused because the make command that I started my program using was terminated by ctrl-z. To fix the problem you can either:
OLD Problematic Make Command:
gcc -o program *.c && ./program
Potential Solutions:
(1) Remove the && ./program line from the make command
(2) Compile and run your program without using make
I am unsure if there is anyway to still use start the program using make if you are hoping to keep your main program running in the case of a SIGTSTP signal
Why does a process that has gone into seccomp mode always get killed on exit?
$ cat simple.c
#include <stdio.h>
#include <stdlib.h>
#include <linux/prctl.h>
int main( int argc, char **argv )
{
printf("Starting\n");
prctl(PR_SET_SECCOMP, 1);
printf("Running\n");
exit(0);
}
$ cc -o simple simple.c
$ ./simple || echo "Returned $?"
Starting
Running
Killed
Returned 137
From the man page, under PR_SET_SECCOMP, the only allowed system calls are read, write, exit, and sigreturn.
When you call exit(0) in the standard library (in recent Linux), you call the exit_group system call, not exit. This is not allowed, so you get a SIGKILL.
(You can see this if you strace the process...)
I am on ubuntu linux 10.04
I have the following
#simplec.c
#include "stdio.h"
int main()
{
printf("Hello World\n");
system("ps -H");
return 12;
}
AND
#callsimplec.c
#include "stdio.h"
int main()
{
int ret =0;
ret = system("./simplec");
printf("In callsimplec ret is %d\n", ret);
}
When I do
gcc callsimplec.c -o callsimplec
gcc simplec.c -o simplec
./callsimplec
I get:
Hello World
PID TTY TIME CMD
27238 pts/2 00:00:00 bash
28066 pts/2 00:00:00 callsimplec
28067 pts/2 00:00:00 simplec
28068 pts/2 00:00:00 ps
In callsimplec ret is 3072
So I figured out that 3072 is printed because 256 times 12 is 3072. Whatever return value I use in simplec.c I get that value multiplied by 256 as the output in print. Why is that? I am just trying to make sense of it.
The value returned by system should be used with the macros: WEXITED, WIFEXITSTATUS etc.
The value returned by system (and by the wait family) is, acording to case:
Normal termination:
15 ............. 8 ............. 0
exit status 0x00
Killed by signal:
15 ............. 8 7............ 0
unused killer signal
Stopped by signal
15 ............. 8 ............. 0
stop signal 0x7f
Continued by signal
15 ............................. 0
0xFFF
So in your case the process exited normally and system returned 12 shifted 8 times.