0

Consider the following program where a process father creates a process child , the father should print on the screen 'A' and 'B' , the child writes 'C' and 'D'. The problem is that I want the program to ONLY result with 'ACBD' This is the code :

void main () {
int pid;
signal(SIGUSR1,SIG_IGN);
if (pid=fork()) {
    printf("A");
    kill(pid,SIGUSR1);
    pause();
    printf("B");
}
else {
    pause();
    printf("C");
    kill(getppid(),SIGUSR1);
    printf("D");
}
}

But its the output is NOTHING ! Any help would be appreciated !

Ahmad Hijazi
  • 635
  • 2
  • 9
  • 27
  • What behavior do you expect? Parent process doesn't display "A" because output is buffered then it sends SIGUSR1 to child and pauses. Child process just pauses not having a chance to display anything... – jaroslawj Apr 06 '15 at 09:37
  • I expect that parent would first print "A" , then it sends a signal to the child, that is waiting it by "pause();" ,then child would print "C" and send signal to parent which is also waiting it and prints "B", then child prints "D"! – Ahmad Hijazi Apr 06 '15 at 09:40

1 Answers1

0

There is a chance that parent is trying to sent signal while 'child' is not alive yet. Add sleep(1) before calling kill in parent code. However it's not nice solution... Furthermore you should add signal handler function (even an empty one). You should also be aware that the output is buffered - it means it is not pushed to terminal immediately, you can force pushing it by adding "\n" or calling fflush(stdout).

#include <stdio.h> 
#include <signal.h>
void main () {
int pid;
void signal_handler(int test) {}
signal(SIGUSR1, signal_handler);
if (pid=fork()) {
    printf("A");
    fflush(stdout);
    sleep(1);
    kill(pid,SIGUSR1);
    pause();
    printf("B");
    fflush(stdout);
}
else {
    pause();
    printf("C");
    fflush(stdout);
    kill(getppid(),SIGUSR1);
    printf("D");
    fflush(stdout); 
  }
}
jaroslawj
  • 462
  • 2
  • 12
  • Ah. One more thing, add signal handler. Even empty one `void signal_handler(int test) { } signal(SIGUSR1, signal_handler); ` currently you ignore usr signals using `signal(SIGUSR1, SIG_IGN);` – jaroslawj Apr 06 '15 at 09:49
  • the code becomes like this : void signal_handler(int test) { } void main () { int pid; signal(SIGUSR1, signal_handler); if (pid=fork()) { printf("A"); sleep(1); kill(pid,SIGUSR1); pause(); printf("B"); } else { pause(); printf("C"); kill(getppid(),SIGUSR1); printf("D"); } } and the output is "CDAB" , whereas I want it "ACBD" ! @jaroslawj – Ahmad Hijazi Apr 06 '15 at 09:56
  • 1
    Just like I said before, output is buffered. Add "\n" after each letter -> "A\n", "B\n" and so on, but if you don't want new lines just add fflush(stdout) after each printf . – jaroslawj Apr 06 '15 at 09:59
  • Thank u @jaroslawj ! It works now :D But please can u explain me why now it works?! I mean what do u mean by the output is buffered ? and if you want post ur answer to mark it correct :) – Ahmad Hijazi Apr 06 '15 at 10:01
  • Output is buffered - it means not immediately pushed to the terminal because of performance reasons. When you mix it with multiprocessing / multithreading results may be weird (unexpected order). You may turn it (buffering) off using snippets from this thread http://stackoverflow.com/questions/7876660/how-to-turn-off-buffering-of-stdout-in-c – jaroslawj Apr 06 '15 at 10:19