#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>

void catchBreak(int signo) {
	char buf[128];
	fprintf(stderr, "Signal no = %d\n", signo);
	fprintf(stderr, "Naugty-Naughty!\n");
}

void catchChild(int signo) {
	pid_t thePid;
	int statloc;
	thePid = wait(&statloc);
	fprintf(stderr, "child %ld ended\n", (long)thePid);
}

int main(void) {

char buf[256];
ssize_t nbytes;
pid_t theChild;

if ((theChild = fork()) == -1) {
	perror("Could not fork");
	exit(1);
}
else if (theChild == 0) { /* Child here */
	sleep(100);
	exit(0);
}

/* parent here */
signal(SIGCHLD, catchChild);
signal(SIGINT, catchBreak);
while (1) {
	printf("enter a number: ");
	fflush(stdout);
	nbytes = read(STDIN_FILENO, buf, 255);
	if (nbytes == 0) {
		printf("Bye-bye\n");
		break;
	}
	else if (nbytes == -1)
		perror("could not read");
	else {
		buf[nbytes] = '\0';
		printf("double of %d is %d\n", atoi(buf), 2 * atoi(buf));
	}
}
return 0;
}
