Help in C


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help in C
# 1  
Old 03-26-2006
Data Help in C

Hi..
Iam facing a problem in the code Smilie 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 Smilie
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 09:15 PM.. Reason: Add code tags for readability
# 2  
Old 03-26-2006
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.
 
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question