![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
Hi..
Iam facing a problem in the code 0 1 2 I am child with pid: 22735. I am, now sending my signal! 3 4 5 I am child with pid: 22736. I am, now sending my signal! Son! Stop making noise! 6 7 8 9 but, iam not getting what I want this is my code..it is in c program Code:
#include <unistd.h> /* define fork(), etc. */
#include <sys/types.h> /* define pid_t, etc. */
#include <sys/wait.h> /* define wait(), etc. */
#include <signal.h> /* define signal(), etc. */
int main (void)
{
int child_pid;
int i;
int child_status;
/* The child process forking code... */
child_pid = fork();
switch (child_pid) {
case -1: /* fork() failed */
perror("fork");
exit(1);
case 0:
/* inside child process */
sleep(2);
printf("Iam child with pid: %d,Iam now sending my signal!\n", getpid());
signal(SIGINT, SIG_IGN);
case 1:
sleep(5);/* sleep a little, so we'll have */
printf("Iam child with pid: %d,Iam now sending my signal!\n", getpid());
signal(SIGINT, SIG_IGN);
printf("Son! Stop Making Noise!\n");
exit(0);
default: /* inside parent process */
break;
}
/* Afer waiting, the parent process goes on... */
/* for example, some output... */
for (i=0; i<10; i++) {
printf("%d\n", i);
sleep(1); /* sleep for a second, so we'll have time to see the mix */
}
return 0;
}
Last edited by Perderabo; 03-26-2006 at 06:15 PM. Reason: Add code tags for readability |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
You want those printf's to send output when they are called. But printf may be buffered. Call setvbuf early in the program to turn off buffering. The child should not call signal. That is not the way to send a signal. It must call kill() to send a signal. It will need the pid of the parent. It can use getppid() for that. Look again at the docs for signal(). Yes the parent needs to call it. But SIG_IGN says to ignore the signal. Do you want the parent to ignore the signal? And, by the way, fork() creates one new process. The child will get a return code of zero. But you have "case 1". The return for the parent is the pid of the child, this will not be 1 ever. This should be enough hints to get you closer.
|
||||
| Google The UNIX and Linux Forums |