public inbox for cygwin-talk@cygwin.com
 help / color / mirror / Atom feed
* - stdin read problem
       [not found] <216a31560804250726r55620c94tc0ec27cbb640dd57@mail.gmail.com>
@ 2008-04-25 14:39 ` Alexey Zakharov
  2008-04-25 14:56   ` Václav Haisman
  0 siblings, 1 reply; 6+ messages in thread
From: Alexey Zakharov @ 2008-04-25 14:39 UTC (permalink / raw)
  To: cygwin, cygwin-talk, cygwin-developers; +Cc: alexey.zakharov

[-- Attachment #1: Type: text/plain, Size: 1229 bytes --]

Hi,

I've attached simple cpp program which demonstrates the problem.
The program works fine from cmd.exe, but fails when started from cygwin.

The program does the following:
- The main thread creates a helper thread which reads stdin and prints
the data read.
- stdin is read via ReadFile() Win32 API function.
- At the end of the main function, the main thread calls CloseHandle()
func for the stdin. It causes the ReadFile() function to return, and
the helper thread to finish. From cmd the program works as described.
- Under Cygwin  the program works differently. The helper thread is
not finished, because it doesn't exit from the ReadFile() func. The
main thread waits for the helper thread's completion during 1 sec,
then it calls TerminateThread() Win32 API func.

When I start the program from cmd, I see the following output:
>read_stdin_example.exe
Stdin_Reader_Thread has been completed

When I start the program from Cygwin, I see:
$ ./read_stdin_example.exe
Stdin_Reader_Thread has NOT been completed. So, it will be terminated.

I believe that this information would be enough to investigate and fix
the problem.
If you need more information or have questions, feel free to contact me.

Have a nice day!
~Alexey

[-- Attachment #2: main.cpp --]
[-- Type: text/plain, Size: 2659 bytes --]


#include <stdio.h>
#include <windows.h>

#define MY_BUF_SIZE 1024

DWORD WINAPI read_stdin(LPVOID lpParameter)
{
    BOOL bResult = FALSE;
    char szBuf[MY_BUF_SIZE] = "";
    int nIndex = 0;
    DWORD nNumOfBytesRead = 0;

    HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
    if (hStdin == NULL || hStdin == INVALID_HANDLE_VALUE)
    {
        printf("GetStdHandle failed for STD_INPUT_HANDLE\n");
        exit(2);
    }

    nIndex = 0;
    for (;;)
    {
        nNumOfBytesRead = 0;
        szBuf[nIndex] = '\0';
        bResult = ReadFile(hStdin, &szBuf[nIndex], 1, &nNumOfBytesRead, NULL);

        if (bResult)
        {
            if (nNumOfBytesRead == 0)
            {
                // End of the file was reached

                // Print the data read if they exist
                if (nIndex > 0)
                {
                    printf("Input data = \"%s\"\n", szBuf);
                }

                break;
            }

            if ( (szBuf[nIndex] == '\n') || (nIndex == MY_BUF_SIZE - 1) )
            {
                // End of the input has been detected
                // Print the input data
                printf("Input data = \"%s\"\n", szBuf);

                // Reset the index back to the beginning of the input buffer
                nIndex = 0;
            }
            else
            {
                // Increment the index and continue the reading
                ++nIndex;
            }
        }
        else
        {
            DWORD dwErrorCode = GetLastError();

            printf("ReadFile failed ===== LastErrorCode = %d\n", dwErrorCode);
            exit(3);
        }

    } // infinite for cycle

    return 0;
} // read_stdin

int main(int argc, char *argv[])
{
    BOOL bResult = FALSE;

    DWORD dwThreadID = 0;
    HANDLE hStdinReaderThread = CreateThread(
        NULL, 0, (LPTHREAD_START_ROUTINE)read_stdin, NULL, 0, &dwThreadID);
    if (!hStdinReaderThread)
    {
        printf("CreateThread failed\n");
        exit(1);
    }

    // Simulate work in the main thread
    Sleep(1000);

    // Close the stdin => the stdin reader thread should exit in that case
    CloseHandle(GetStdHandle(STD_INPUT_HANDLE));
    if (WaitForSingleObject(hStdinReaderThread, 1000) != WAIT_OBJECT_0)
    {
        printf("Stdin_Reader_Thread has NOT been completed. So, it will be terminated.\n");
        TerminateThread(hStdinReaderThread, 1);
    }
    else
    {
        printf("Stdin_Reader_Thread has been completed\n");
    }
    CloseHandle(hStdinReaderThread);

    return 0;
} // main

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: - stdin read problem
  2008-04-25 14:39 ` - stdin read problem Alexey Zakharov
@ 2008-04-25 14:56   ` Václav Haisman
  2008-04-25 15:10     ` Corinna Vinschen
  0 siblings, 1 reply; 6+ messages in thread
From: Václav Haisman @ 2008-04-25 14:56 UTC (permalink / raw)
  To: The Vulgar and Unprofessional Cygwin-Talk List; +Cc: alexey.zakharov

[-- Attachment #1: Type: text/plain, Size: 1434 bytes --]

Alexey Zakharov wrote:
> Hi,
> 
> I've attached simple cpp program which demonstrates the problem.
> The program works fine from cmd.exe, but fails when started from cygwin.
> 
> The program does the following:
> - The main thread creates a helper thread which reads stdin and prints
> the data read.
> - stdin is read via ReadFile() Win32 API function.
> - At the end of the main function, the main thread calls CloseHandle()
> func for the stdin. It causes the ReadFile() function to return, and
> the helper thread to finish. From cmd the program works as described.
> - Under Cygwin  the program works differently. The helper thread is
> not finished, because it doesn't exit from the ReadFile() func. The
> main thread waits for the helper thread's completion during 1 sec,
> then it calls TerminateThread() Win32 API func.
> 
> When I start the program from cmd, I see the following output:
>> read_stdin_example.exe
> Stdin_Reader_Thread has been completed
> 
> When I start the program from Cygwin, I see:
> $ ./read_stdin_example.exe
> Stdin_Reader_Thread has NOT been completed. So, it will be terminated.
> 
> I believe that this information would be enough to investigate and fix
> the problem.
> If you need more information or have questions, feel free to contact me.
> 
> Have a nice day!
> ~Alexey
> 
Cross posting like this won't give you the kind of attention you seek.

--
VH


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 250 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: - stdin read problem
  2008-04-25 14:56   ` Václav Haisman
@ 2008-04-25 15:10     ` Corinna Vinschen
  2008-04-25 15:12       ` Dave Korn
  0 siblings, 1 reply; 6+ messages in thread
From: Corinna Vinschen @ 2008-04-25 15:10 UTC (permalink / raw)
  To: cygwin-talk

On Apr 25 16:55, V?clav Haisman wrote:
> Alexey Zakharov wrote:
>> [SNIP]
>> ~Alexey
> Cross posting like this won't give you the kind of attention you seek.

Quite the contrary.


Corinna

^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: - stdin read problem
  2008-04-25 15:10     ` Corinna Vinschen
@ 2008-04-25 15:12       ` Dave Korn
  2008-04-25 15:31         ` Corinna Vinschen
  0 siblings, 1 reply; 6+ messages in thread
From: Dave Korn @ 2008-04-25 15:12 UTC (permalink / raw)
  To: 'quotefix is acting funny on me'

Corinna wrote on 25 April 2008 16:10:

> On Apr 25 16:55, V?clav Haisman wrote:
>> Alexey Zakharov wrote:
>>> [SNIP]
>>> ~Alexey
>> Cross posting like this won't give you the kind of attention you seek.
> 
> Quite the contrary.
> 
> 
> Corinna


.... rather depends what kind of "attention" he's seeking, doesn't it?


    cheers,
      DaveK
-- 
Can't think of a witty .sigline today....

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: - stdin read problem
  2008-04-25 15:12       ` Dave Korn
@ 2008-04-25 15:31         ` Corinna Vinschen
  2008-04-25 15:44           ` Dave Korn
  0 siblings, 1 reply; 6+ messages in thread
From: Corinna Vinschen @ 2008-04-25 15:31 UTC (permalink / raw)
  To: cygwin-talk

On Apr 25 16:12, Dave Korn wrote:
> Corinna wrote on 25 April 2008 16:10:
> > Quite the contrary.
> 
> .... rather depends what kind of "attention" he's seeking, doesn't it?

You mean, if he's trying to get hit with a stinking wet fish, he did it
all right?


Corinna

^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: - stdin read problem
  2008-04-25 15:31         ` Corinna Vinschen
@ 2008-04-25 15:44           ` Dave Korn
  0 siblings, 0 replies; 6+ messages in thread
From: Dave Korn @ 2008-04-25 15:44 UTC (permalink / raw)
  To: 'mixing metaphors is like a fish trying to bark up the wrong
	bicycle'

cygwin-talk-owner@cygwin.com wrote on 25 April 2008 16:31:

> On Apr 25 16:12, Dave Korn wrote:
>> Corinna wrote on 25 April 2008 16:10:
>>> Quite the contrary.
>> 
>> .... rather depends what kind of "attention" he's seeking, doesn't it?
> 
> You mean, if he's trying to get hit with a stinking wet fish, he did it
> all right?
> 
> 
> Corinna

  No, I mean if he's trying to get a hippo dropped on him from a stinking
great height, he's doing it all right.

  ... That's not to say we couldn't persuade the hippo to carry a fish and
slap him with it on the way, though.



  Oh well.  It beats a slap round the kipper with a wet face.  At least it's
not as bad as a poke in the stick with a sharp eye.  Etc, etc, etc....

    cheers,
      DaveK
-- 
Can't think of a witty .sigline today....

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2008-04-25 15:44 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <216a31560804250726r55620c94tc0ec27cbb640dd57@mail.gmail.com>
2008-04-25 14:39 ` - stdin read problem Alexey Zakharov
2008-04-25 14:56   ` Václav Haisman
2008-04-25 15:10     ` Corinna Vinschen
2008-04-25 15:12       ` Dave Korn
2008-04-25 15:31         ` Corinna Vinschen
2008-04-25 15:44           ` Dave Korn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).