The problem with the code is when u give n as input to the child, the parent is still alive so in order to kill the parent use return from the read in parent to see if the child has exited or not and then if the child is already exit, then exit the parent too. this can be done by editing your code with
if( (numread = read(rdfd, buf, MAX_BUF_SIZE)) == 0)
return 0;
actually once you give n as input in the child, the child exits and then old value of ans which is y is compared in parent process and so it doesn't exit....
check n reply... thanks...