Output of Function Set By signal() Delayed

From: Leo Wong (gnow_oel_at_hotmail.com)
Date: 08/26/04


Date: 25 Aug 2004 20:45:20 -0700

QUESTION
--------

I ran the program below on the latest Debian Linux. When I pressed
Ctrl-C, "interrupt" printed, but the new prompt '%' did not appear
until I entered RETURN. Then I changed the line

        printf("interrupt\n%% ");

in function sig_int to

        fprintf(stderr, "interrupt\n%% ");

and reran the program, every thing was OK; the prompt '%' printed
immediately.

Thanks in advance for explaining me why this happend.

COPY of THE CODES
-----------------

/* p.18, program 1.8,
 * Advanced Programming in the UNIX Environment
 */

#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include "ourhdr.h"

static void sig_int(int);

int
main(void)
{
  char buf[MAXLINE];
  pid_t pid;
  int status;

  if (signal(SIGINT, sig_int) == SIG_ERR)
    err_sys("signal error");

  printf("%% ");
  while(fgets(buf, MAXLINE, stdin) != NULL) {
    buf[strlen(buf) - 1] = 0;

    if ( (pid = fork()) < 0)
      err_sys("fork error");

    else if (pid == 0) {
      execlp(buf, buf, (char *) 0);
      err_ret("couldn't execute: %s", buf);
      exit(127);
    }

    if ( (pid = waitpid(pid, &status, 0)) < 0)
      err_sys("waitpid error");
    printf("%% ");
  }
  exit(0);
}

void
sig_int(int signo)
{
  printf("interrupt\n%% "); /**/
}



Relevant Pages

  • [PATCH -rt] irq nobody cared workaround for i386
    ... of the mask and unmask interrupt routines. ... from level to edge for mask and edge to level for unmask. ... PCI-E from thinking it's in legacy mode and assert an old fashion INT# ... static void clear_IO_APIC_pin ...
    (Linux-Kernel)
  • the PS/2 mouse problem
    ... > high interrupt latency for non-fast interrupt handlers (especially ones ... static int doopen ... +static void _psmintr; ... s = spltty(); ...
    (freebsd-current)
  • [PATCH 4/9] irq-remove: driver non-trivial
    ... unsigned int iobase; ... static void ip2_poll; ... Bottom half interrupt handlers ... struct sx_port *port; ...
    (Linux-Kernel)
  • Re: [PATCH -rt] irq nobody cared workaround for i386
    ... of the mask and unmask interrupt routines. ... from level to edge for mask and edge to level for unmask. ... PCI-E from thinking it's in legacy mode and assert an old fashion INT# ... static void clear_IO_APIC_pin ...
    (Linux-Kernel)
  • [PATCH] powerpc: Fix loss of interrupts with MPIC
    ... With the new interrupt rework, an interrupt "host" mapcallback can be ... static void mpic_mask_irq(unsigned int irq) ...
    (Linux-Kernel)