child-process signal handling in c - c

I need your help in an exercise i have about signal handling between processes. I think that it's a trivial question but i can't find the answer anywhere. I want to print something from the parent in a file, send a signal from the parent to the child, the child has to print in a file and then send a signal from the child to the parent.
The code i am using is this:
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>
#define WRITEF 123451 //Random number
FILE *infile;
void writef() {
fprintf(infile, "Child Starting (%d)\n", getpid());
printf("Child Starting (%d)\n", getpid());
}
int main() {
pid_t pid;
infile = fopen("pid_log.txt","w");
pid = fork();
signal(WRITEF, writef);
if ( pid == 0 ) {
pause();
printf("enter child\n");
}
else {
fprintf(infile, "Parent (%d)\n", getpid());
printf("Parent (%d)\n", getpid());
kill(pid, WRITEF);
pause();
wait((int*)1);
}
fclose(infile);
return 1;
}

PROBLEM SOLVED!!!
The key is that you have to register the signal (use the singal function) before every pause(). Also you cannot use a "user-made" signal and in my case i used SIGCONT.
Here is the final (almost) version of my program:
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>
FILE *infile;
void noth() {
}
void writec() {
infile = fopen("pid_log.txt","a+");
fprintf(infile, "Child (%d)\n", getpid());
printf("Child (%d)\n", getpid());
fclose(infile);
}
void writep() {
infile = fopen("pid_log.txt","a+");
fprintf(infile, "Parent (%d)\n", getpid());
printf("Parent (%d)\n", getpid());
fclose(infile);
}
main() {
pid_t pid = fork();
if ( pid == 0 ) { //child process
signal(SIGCONT,noth); //registering signal before pause()
pause();
infile = fopen("pid_log.txt","a+");
printf("Child Starting (%d)\n",getpid());
fprintf(infile,"Child Starting (%d)\n",getpid());
fclose(infile);
while (1) {
sleep(2);
kill(getppid(), SIGCONT); //sending singal to parent
signal(SIGCONT, writec);
pause();
}
}
else { //parent process
infile = fopen("pid_log.txt","a+");
printf("Parent Starting (%d)\n",getpid());
fprintf(infile,"Parent Starting (%d)\n",getpid());
fclose(infile);
kill(pid, SIGCONT);
signal(SIGCONT, writep);
pause();
while (1) {
sleep(2);
kill(pid, SIGCONT);
signal(SIGCONT, writep);
pause();
}
}
return 1;
}

Related

How do find the granchild pid in C?

I am trying to code a toy process manager and was wondering how do I find the grandchild pids when one of my children pids dies. I.e. how to deal with the "Readiness protocol".
systemd has the "Type=forking" option, which as far as I understand, waits for the forked pid to die and then assumes that one of the forked pid's children is the actual "daemon" to monitor.
My code so far is as following, but I am missing the XXX
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/prctl.h>
int
main (int argc, const char *argv[])
{
printf("sup stared\n");
int pid;
prctl(PR_SET_CHILD_SUBREAPER);
pid = fork();
if (pid == 0) {
setpgid(0, 0);
printf("service started, pid: %d, pgid: %d\n",
getpid(), getpgid(0));
int dpid;
dpid = fork();
if (dpid == 0) {
printf("daemon started, pid: %d, pgid: %d\n",
getpid(), getpgid(0));
while (1);
}
printf("service exiting\n");
return 0;
} else {
printf("service pid: %d\n", pid);
int rc;
rc = waitpid(pid, NULL, 0);
printf("service exited? %d\n", rc);
int dpid;
dpid = XXcX();
printf("daemon, %d\n", dpid);
}
return 0;
}

how can I send a signal between a child and a parent processes in linux

I have two cods the first one is for the parent which sends a signal (SIGUSER1) to the child and when the child receive it he should print that he received it.
Parent code
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/types.h>
void sighand(int);
int main()
{
int cpid, ppid;
ppid = getpid();
printf("My process ID is %d\n", ppid);
FILE *fp1;
fp1 = fopen("cpid.txt", "w+");
cpid = fork();
if ( cpid == 0 ) {
printf("I am the child => PID = %d\n", getpid());
}
else
printf("I am the parent => PID = %d, child ID = %d\n", getpid(), cpid);
fprintf(fp1, "%d\n", cpid);
// kill(cpid, SIGUSR1);//id, signal, send
sigset(SIGUSR2, sighand);
return 0;
}
void sighand(int the_sig){
if (the_sig == SIGUSR2){
printf("sigusr2 received");
exit(1);
}
}
Child code
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/types.h>
void sighand1(int);
int main()
{
FILE *fp1;
int pid;
fp1 = fopen("cpid.txt", "r");
fscanf(fp1, "%d,", &pid);
sigset(SIGUSR1,sighand1);
while(1) {
printf("Waiting..");
sigpause(SIGUSR1);
}
return 0;
}
void sighand1(int the_sig)
{
if (the_sig == SIGUSR1){
printf("sigusr1 received");
exit(1);
}
}
When I start the code it prints that the process (child) was created then when I send a signal it wont do any thing the child stuck in a loop or the wait and the parent wont do anything can any one tell me where did i go wrong in my code or logic.
Your code has several problems:
You try to pass some pid through a file, but you can use the getppid() function (get parent id)
You have some child code, but it is not called
no signal is launched
So your code can be corrected this way:
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
void parent_handler(int the_sig)
{
if (the_sig == SIGUSR2){
printf("sigusr2 received in parent\n");
}
}
void child_handler(int the_sig)
{
if (the_sig == SIGUSR1){
printf("sigusr1 received in child\n");
kill(getppid(), SIGUSR2);
exit(1);
}
}
int child_function()
{
/* prepare to receive signal */
sigset(SIGUSR1,child_handler);
while(1) {
printf("Waiting..");
fflush(stdout);
/* wait for signal */
sigpause(SIGUSR1);
}
return 0;
}
int main()
{
int cpid, ppid;
ppid = getpid();
printf("My process ID is %d\n", ppid);
cpid = fork();
if ( cpid == 0 ) {
printf("I am the child => PID = %d\n", getpid());
child_function();
return 0;
}
else
printf("I am the parent => PID = %d, child ID = %d\n", getpid(), cpid);
/* child will never reach this point */
sleep(1);
/* prepare parent to received signal */
sigset(SIGUSR2, parent_handler);
/* send signal to child */
kill(cpid, SIGUSR1);
sleep(1);
return 0;
}

signal handling on background process and file output

I have been considering the following code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
FILE* file;
void signal_handler(int _signal) {
switch(_signal){
case SIGTERM:
fprintf(file, "Ouch, the Daemon Child was killed!\n");
fflush(file);
abort();
default:
fprintf(file, "So what?!\n");
fflush(file);
}
}
int main() {
pid_t pid;
int status;
pid = fork();
if(pid != 0) {
// parent
waitpid(pid, &status, WNOHANG); // daemonize the child
} else {
// child
signal(SIGTERM, signal_handler);
file = fopen("daemon.txt", "w");
while(1) {
sleep(1);
fprintf(file, "Daemon child is alive.\n");
fflush(file);
}
}
return 0;
}
and I expected I could find at the end of daemon.txt the string Ouch, the Daemon Child was killed!, after sudo kill -KILL . However, this is not the case. Where is my fault?
You appear to be catching SIGTERM, and then sending SIGKILL, which you do not have a handler for. You will probably see your expected output if you use kill -TERM $pid rather than kill -KILL.

Why doesn't the last line of the child process print?

#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
int main( ) {
pid_t pid;
int status = -1;
if ((pid = fork()) != 0) {
printf("Father process wait child PID=%d\n", pid);
wait(&status);
printf("Child finish with status: %d\n",WEXITSTATUS(status));
exit(0);
}
else {
printf("Child process running...\n");
execl("/bin/ls","ls", "-la", NULL);
printf("Child ending...\n");
}
}
When compiling this code the last line of the else doesn't print and I don't know why.
http://linux.die.net/man/3/execl
The exec() family of functions replaces the current process image with a new process image.
....
Return Value
The exec() functions only return if an error has occurred. The return
value is -1, and errno is set to indicate the error.

How run a program in C for x minutes?

I am trying to run a program in C for x minutes. I need to make the child process go to sleep for that amount of time. Any help would be appreciated. Basically I am trying to understand how fork() and sleep() work. Here is my code snippet
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int i = fork();
printf("fork return value = %d\n", i);
printf("this is the time before sleep");
system("date +%a%b%d-%H:%M:%S");
printf("\n");
if (i==0){
sleep(120);
}
system("ps");
printf("this is the time after sleep");
system("date +%a%b%d-%H:%M:%S");
printf("\n");
}
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(void)
{
pid_t pid;
int rv=1;
switch(pid = fork()) {
case -1:
perror("fork"); /* something went wrong */
exit(1); /* parent exits */
case 0:
printf(" CHILD: This is the child process!\n");
printf(" CHILD: My PID is %d\n", getpid());
printf(" CHILD: My parent's PID is %d\n", getppid());
printf(" CHILD: I'm going to wait for 30 seconds \n");
sleep(30);
printf(" CHILD: I'm outta here!\n");
exit(rv);
default:
printf("PARENT: This is the parent process!\n");
printf("PARENT: My PID is %d\n", getpid());
printf("PARENT: My child's PID is %d\n", pid);
printf("PARENT: I'm now waiting for my child to exit()...\n");
wait(&rv);
printf("PARENT: I'm outta here!\n");
}
return 0;
}
Say thanks to Brian "Beej Jorgensen" Hall

Resources