Communication between two process using signals in C - c

I must create a program in C who will be communicate between two process by SIGUSR1. This program must will reply back when send and receive signal. I have the code below, but he just sends a signal, why does the receiving function not work?
IMG
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
void send_SIGUSR1();
void main()
{
int pid;
if ((pid = fork()) < 0) {
perror("Fork");
exit(1);
}
if (pid == 0) {
signal(SIGUSR1, send_SIGUSR1());
}
else
{
printf("\nPARENT: sending SIGUSR1\n\n");
kill(pid, SIGUSR1);
}
}
void send_SIGUSR1()
{
signal(SIGUSR1, send_SIGUSR1);
printf("CHILD: I have received a SIGUSR1\n");
}

Related

Child not Responding

Hello I'm having issues with sending signals from the father process to the child process. The Child doesn't respond to the signed sent by the father process:
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
pid_t pid;
pid = fork();
if(pid > 0) {
printf("Hello, my son!\n");
sleep(5); /* Sleep for 5 seconds... */
kill(pid, SIGUSR1);
printf("Goodbye, son!\n");
}
else {
printf("Son is running\n");
pause(); /* Wait for some signal... */
printf("I received the signal!");
}
}
The son doesn't received the signal it only says it's running here is the output:
output
The default behavior when SIGUSR1 is received is termination, so the child never prints the message. To handle the signal, you can use sigaction:
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
void handle(int sig, siginfo_t *i, void *v) { return; }
int
main(void)
{
pid_t pid = fork();
if( pid > 0 ){
printf("Hello, my child!\n");
sleep(1); /* Sleep for 1 second. */
kill(pid, SIGUSR1);
printf("Goodbye, child!\n");
} else {
struct sigaction act;
memset(&act, 0, sizeof act);
act.sa_sigaction = handle;
if( sigaction( SIGUSR1, &act, NULL ) ){
perror("sigaction");
exit(1);
}
printf("Child is running\n");
pause(); /* Wait for some signal... */
printf("I received the signal!\n");
}
}

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();
}
}

How to send a signal from the child process to parent process through kill command

I am trying to create a child process through fork() system call, then trying to send a signal to parent and print out something on the screen.
Here is my code:-
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
void func1(int signum) {
if(signum == SIGUSR2) {
printf("Received sig from child\n");
}
}
int main() {
signal(SIGUSR2, func1);
int c = fork();
if(c > 0) {
printf("parent\n");
}
else if(c == -1) {
printf("No child");
}
else {
kill(getppid(), SIGUSR2);
printf("child\n");
}
}
When I execute my program all I get is:-
child
Segmentation fault (core dumped)
I am a novice to C language system calls, and don't get why this is happening, and how to get the desired output, which would be printing of all the three printf statements. Any help for the same would be appreciated.
Your code has a number of minor issues and certainly has undefined behaviour i.e., you can't call printf or other async-signal unsafe functions from a signal handler.
This is the code with fixes (see comments in code). This should work as expected (with no particular order of print statements) and see if still get a segfault with this code.
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
void func1(int signum)
{
/* write is asyc-signal-safe */
write(1, "Received sig from child\n", sizeof "Received sig from child\n" - 1);
}
int main()
{
signal(SIGUSR2, func1);
/* fork returns a pid_t */
pid_t c = fork();
if(c > 0) {
printf("parent\n");
/* Wait for the child to exit; otherwise, you may not receive the signal */
if (wait(NULL) == -1) {
printf("wait(2) failed\n");
exit(1);
}
} else if (c == -1) {
printf("fork(2) error\n");
exit(1);
} else {
if (kill(getppid(), SIGUSR2) == -1) {
/* In case kill fails to send signal... */
printf("kill(2) failed\n");
exit(1);
}
printf("child\n");
}
}

Signals to child process

I have to write a C program which creates a child and waits for a SIGHUP signal. After receiving that, it sends a signal to it's child. The child executes the default action of the received signal.
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
void sigint();
void sighup();
pid_t pid, pid_parent;
int main(int argc, char const *argv[])
{
if ((pid = fork()) < 0) {
perror("fail1!");
exit(1);
}
else if (pid == 0) {
signal(SIGINT, sigint);
while(1) pause();
exit(1);
}
signal(SIGHUP, sighup);
printf("PARENT's pid: %d\n", pid_parent = getpid());
while(1) pause();
return 0;
}
void sighup()
{
signal(SIGHUP,sighup);
kill(pid,SIGINT);
printf("PARENT(%d): I received a SIGHUP. I'll send something to my child(%d).\n",getpid(),pid);
printf("OK");
}
void sigint()
{
signal(SIGINT,sigint);
printf("CHILD: I received a SIGINT. I'll execute it.");
signal(SIGINT,SIG_DFL);
}
Seems like it doesn't work after kill(pid, SIGQUIT). And I really don't have ideas why.

C UNIX: how to assign a new signal handler

I have a signal handler where I set ctrl+z/SIGTSTP to just be detected by the program. But when I want to change the signal handler of ctrl+z/SIGTSTP to its default behavior in the child process, the ctrl+z doesn't change. Is there a proper to change signal handlers?
#include <stdio.h>
#include <signal.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
void handler(int sig_num)
{
printf("detected %d\n",sig_num);
}
int main()
{
int x;
signal(SIGTSTP,handler);
pid_t pid = fork();
if(pid == 0)
{
signal(SIGTSTP,SIG_DFL);
printf("in child process \n");
while(1);
}
else if(pid > 0)
{
printf("running parent\n");
printf("waiting for my child to run\n");
wait(&x);
exit(0);
}
return 0;
}

Resources