{"id":8983,"date":"2011-12-02T07:00:00","date_gmt":"2011-12-02T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2011\/12\/02\/dont-let-more-than-one-process-try-to-read-from-stdin-at-the-same-time\/"},"modified":"2011-12-02T07:00:00","modified_gmt":"2011-12-02T07:00:00","slug":"dont-let-more-than-one-process-try-to-read-from-stdin-at-the-same-time","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20111202-00\/?p=8983","title":{"rendered":"Don&#039;t let more than one process try to read from stdin at the same time"},"content":{"rendered":"<p>A customer reported a problem with a program that ran a series of other programs in parallel.<\/p>\n<blockquote class=\"q\"><p> We have a main program (call it main.exe) that runs a bunch of child processes with stdout and stderr redirected. (We are not redirecting stdin.) We&#8217;ve found that some of the child processes get stuck inside the C runtime startup code on a call to <code>Get&shy;File&shy;Type<\/code> on the stdin handle. What could be the reason for this? Is there something we can do that doesn&#8217;t require us to modify the child processes? (They are third party code we do not have control over.) <\/p><\/blockquote>\n<p> This is one of those <i>once you&#8217;ve debugged this problem you never forget it<\/i> type of problems.\n Notice that each of the child processes inherits the same stdin from main.exe, since you aren&#8217;t redirecting stdin. Since the stdin handle was not opened as overlapped, all I\/O to the handle is serialized.\n The C runtime calls <code>Get&shy;File&shy;Type<\/code> at startup to determine whether or not to use buffering. When each child process starts up, it calls <code>Get&shy;File&shy;Type<\/code>, enters its <code>main<\/code>, and goes about its business. Everything is great <i>until one of them tries to read from stdin<\/i>. At that point, everything falls apart.\n The next child process to start calls <code>Get&shy;File&shy;Type<\/code>, but instead of returning with a result, it waits for the previous I\/O request (the read) to complete because the handle is marked synchronous, and synchronous handles permit only one operation at a time. The user, of course, doesn&#8217;t realize that the first program is waiting for input (the prompt got redirected), so the user sits and waits for the program while the program sits and waits for the user.\n To solve this problem, you first need to decide what you want to happen to stdin. Right now, you gave stdin to a dozen child processes, and each line of input the user types will be randomly assigned to one of those programs. In this case, the customer&#8217;s answer is &#8220;I don&#8217;t care about stdin; these programs aren&#8217;t supposed to be reading from stdin anyway&#8221;, in which case you can redirect stdin of the child processes to <code>NUL<\/code>.<\/p>\n<p> <b>Bonus chatter<\/b>: This is also why, when you hit Ctrl+C to exit a console program which launched child processes with <code>CREATE_NEW_PROCESS_GROUP<\/code>, the command prompt that comes back sometimes behaves kind of strangely. Since the child processes were launched in a separate process group, the Ctrl+C killed the main program but left the child processes running. If any of those child processes read from stdin, you get the &#8220;Randomly assign input&#8221; effect described above because you have two programs racing to read from stdin: the orphaned child process and <code>cmd.exe<\/code>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>A customer reported a problem with a program that ran a series of other programs in parallel. We have a main program (call it main.exe) that runs a bunch of child processes with stdout and stderr redirected. (We are not redirecting stdin.) We&#8217;ve found that some of the child processes get stuck inside the C [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-8983","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A customer reported a problem with a program that ran a series of other programs in parallel. We have a main program (call it main.exe) that runs a bunch of child processes with stdout and stderr redirected. (We are not redirecting stdin.) We&#8217;ve found that some of the child processes get stuck inside the C [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/8983","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=8983"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/8983\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=8983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=8983"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=8983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}