I would like to:
Parent process:
- Write data to a pipe
- Send a signal to child process
Child process:
- Read data after signal is caught.
Here is my tryout:
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
int fd[2];
//Handler - Exiting process, OR reading from pipe
void sigHandler(int signumber){
if(signumber == SIGUSR1) {
printf("SIGUSR1 catched.\n");
//So lets read from pipe
int read_data = -1;
close(fd[1]);
read(fd[0], &read_data, sizeof(read_data));
//This printf never gets called
printf("Received data: %s", read_data);
}
else {
printf("SIGQUIT catched.\n");
exit(3);
}
}
//This is handles parent process to NOT exit on sending a signal
//maybe not the best workaround but i could only do this in this way.
void sigDummy(int signumber){
printf("SigDummy catched\n");
}
int main(){
fflush(stdout);
pipe(fd);
char option=-1;
pid_t child_a;
pid_t child_b;
child_a = fork();
//A Child Proc which is not used atm, it will be
if (child_a == 0) {
signal(SIGQUIT ,sigHandler);
signal(SIGUSR1 ,sigHandler);
while(1) {
//idle
sleep(1);
printf("child_a iddle work\n");
}
}
//Child B Proc for reading form the PIPE after got SIGUSR1 signal
else {
child_b = fork();
if (child_b == 0) {
signal(SIGQUIT ,sigHandler);
signal(SIGUSR1 ,sigHandler);
while(1) {
//idle
sleep(1);
printf("child_b iddle work\n");
}
}
//Parent Proc for writing to a pipe and sending signals to child B to read the pipe
else {
signal(SIGUSR1 ,sigDummy);
//MENU WHILE
while(option!=0){
scanf("%d", &option);
printf("input was: %d\n", option);
kill(child_b,SIGUSR1);
close(fd[0]);
write(fd[1], &option, sizeof(option));
}
}//End of Menu while
//Exiting child prcoesses then exiting parent prcoess
if(option==0){
int status_a, status_b;
waitpid(child_b, &status_b, WNOHANG|WUNTRACED);
waitpid(child_a, &status_a, WNOHANG|WUNTRACED);
kill(child_b,SIGQUIT);
kill(child_a,SIGQUIT);
}
}
return 1;
}
Output:
child_b iddle work
child_a iddle work
child_b iddle work
child_a iddle work
child_b iddle work
child_a iddle work
child_b iddle work
child_a iddle work
4
input was: 4
SigDummy catched
SIGUSR1 catched.
SIGUSR1 catched. //I wait here several seconds, child_a and b processes are not printing anything anymore....
3
input was: 3
SigDummy catched
1
input was: 1
SigDummy catched
And if i remove the pipe writing the pipe reading parts i have nice working code:
Output without pipe write and read:
child_a iddle work
child_b iddle work
3
input was: 3
SigDummy catched
SIGUSR1 catched.
child_a iddle work
SIGUSR1 catched.
child_b iddle work
child_a iddle work
child_b iddle work
2
input was: 2
SigDummy catched
SIGUSR1 catched.
child_b iddle work
SIGUSR1 catched.
child_a iddle work
child_b iddle work
child_a iddle work
1
input was: 1
SigDummy catched
SIGUSR1 catched.
child_b iddle work
SIGUSR1 catched.
child_a iddle work
child_b iddle work
child_a iddle work
4
input was: 4 //Inputs are always catched with my signal handler func, and child processes does not stop printing.
SigDummy catched
SIGUSR1 catched.
child_b iddle work
SIGUSR1 catched.
child_a iddle work
child_b iddle work
child_a iddle work
child_b iddle work
child_a iddle work
0
input was: 0
SigDummy catched
SIGUSR1 catched.
SIGQUIT catched.
SIGUSR1 catched.
SIGQUIT catched.
Exiting
The method i found out is working well without the write-read pipe part. But after adding it it is simply randomly wrong.
So why my code behaviours other way when i try to do the pipe sending thing? I just started to learn c, and the code may be really messed up, be nice please.