• C

inter process communication

I have a program with a child and a parent process.

How can one process notify the other process that an event has occurred (by using signals) ?

please provide some code examples.
FOXBATAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
sunnycoderConnect With a Mentor Commented:
Here are some examples and details
http://www.cs.cf.ac.uk/Dave/C/node24.html#SECTION002400000000000000000

http://www.delorie.com/gnu/docs/glibc/libc_503.html

I would recommend using sigaction interface instead of signal interface
0
 
sunnycoderCommented:
Hi FOXBAT,

what platform ?

Sunnycoder
0
 
sunnycoderCommented:
use kill to send a signal

#include <sys/types.h>

#include <signal.h>

int kill(pid_t pid, int sig);

http://www.die.net/doc/linux/man/man2/kill.2.html

parent gets a child pid when child is spawned and child can get parent pid by calling getppid()
http://www.die.net/doc/linux/man/man2/getppid.2.html
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
FOXBATAuthor Commented:
can you please modify this code to send a signal from one process to the other

if(pid == 0) // child
{  
      printf("child %d is sleeping \n", getpid());
      sleep(1);
}


while(waitpid(pid, &status, WNOHANG) == 0) // parent
{
      printf("waitin....\n");
      sleep(1);

}
0
 
sunnycoderCommented:
who should send signal to whom and which signal ? .. added a line below

if(pid == 0) // child
{  
     printf("child %d is sleeping \n", getpid());
     sleep(1);
}


while(waitpid(pid, &status, WNOHANG) == 0) // parent
{
     printf("waitin....\n");
     sleep(1);
     kill (pid, SIGUSR1);  <-- parent will send SIGUSR1 to child. Since you used pid == 0 to check for a child, I assume that pid holds the pid for the child and appropriate error checking for fork has been done
}
0
 
FOXBATAuthor Commented:
ok so how does the child recive the signal?

can you please provide the example in the code above
0
 
sunnycoderCommented:
Check the second link I posted for a complete example
http://www.delorie.com/gnu/docs/glibc/libc_503.html
0
 
grg99Commented:
The signal() way of communication is a rather narrow and slow channel.

If you just want to pass a byte every second, signal is okay.

If you need much more bandwidth, I'd use a TCP connection.  

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.