Killing a subshell

I am calling a script from with another script and reading its output one line at a time (using <childscript> | while read line) in the parent script. If the output exceeds a predefined number of lines I want to kill the child shell from within the parent shell.

I decided to print the process ID using $$ in the child shell so that parent shell can read it and send kill signal to it. That I am able to, however it says process doesn't exist.

Can anybody suggest me a better way to do things. Note I need to do cleanup in child script (so I am using trap) and also this has to be done on sh and not any other shell, not even bash.

Thanks for helping.

Just break out of the loop. The child script will receive a SIGPIPE and exit, unless you explicitly catch that condition.

The thing is the lines I am reading and displaying are XML tags. The child script is supposed to display one final tag at the end based on the output processed so far. So it needs to handle the exit gracefully. And the parent script is to be printed on stdout.