The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


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 !!

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 03-26-2006
Registered User
 

Join Date: Mar 2006
Posts: 1
Unhappy Help in C

Hi..
Iam facing a problem in the code the output should look like that:
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;
}
Can somebody help me?

Last edited by Perderabo; 03-26-2006 at 06:15 PM. Reason: Add code tags for readability
Reply With Quote
Forum Sponsor
  #2  
Old 03-26-2006
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,613
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.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:31 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0