Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

inter process communication

Posted on 2004-09-14
8
Medium Priority
?
480 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:FOXBAT
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 12052868
Hi FOXBAT,

what platform ?

Sunnycoder
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 12052877
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
 
LVL 45

Accepted Solution

by:
sunnycoder earned 600 total points
ID: 12052898
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:FOXBAT
ID: 12052901
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 12052916
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
 

Author Comment

by:FOXBAT
ID: 12052965
ok so how does the child recive the signal?

can you please provide the example in the code above
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 12052990
Check the second link I posted for a complete example
http://www.delorie.com/gnu/docs/glibc/libc_503.html
0
 
LVL 22

Expert Comment

by:grg99
ID: 12055466
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question