1

I need to terminate my child when parent exits abnormally. I found an answer in this below link for my requirement:

How can I cause a child process to exit when the parent does?

Answer from tomcri:

You can simply start a thread reading from System.in in the child process. If you are not writing to stdin of your child, nobody else will do and the thread will block "forever". But you will get an EOF (or an exception), if the parent process is killed or dies otherwise. So you can shutdown the child as well. (Child process started with java.lang.ProcessBuilder)

Actually, I tried to comment below this Answer but couldn't due to lack of enough reputation.

So my question is, how do I recognize the character in the stdin is EOF? In my program, I am reading data from stdin for some other purpose too. So I need to know when EOF/(chars comes when parent dies) somehow. stdin is filled with character = ÿ (hex = 0xFFFF). I read some pages like 'The highest possible valid code point is Ux10FFFF in Unicode'. So how do I check to exit?

My program is Windows C++ and supports Unicode characters. Is there any better way to understand this scenario?

SSDev
  • 51
  • 5
  • 3
    The parent can create a job object, set `JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE` flag, and add a child to that job. See [Job Objects](https://learn.microsoft.com/en-us/windows/win32/procthread/job-objects) – Igor Tandetnik Dec 18 '21 at 00:58
  • Parent is not a C++ application. – SSDev Dec 18 '21 at 01:02
  • 1
    (1) What is `size`? How did you obtain it? The answer you are basing your solution on assumes java, but your question is tagged C++. (2) Job objects don't care what language the program is written in. As long as you can call Win32 APIs, you can create the job object. Details on the job object approach are available in [this answer](https://stackoverflow.com/questions/6259055/createprocess-such-that-child-process-is-killed-when-parent-is-killed/6263700#6263700). – Raymond Chen Dec 18 '21 at 05:13
  • (1) I just reading the size (number of characters in stdin). Reading stdin in Java or C++ doesn't matter right? (2) My application is a browser extension exe. That's why I mentioned parent is not a C++ application. – SSDev Dec 19 '21 at 04:41

0 Answers0