Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

pipes and carriage control

ArturGuzik
Valued Contributor I
388 Views
Dear All,

I used a code posted here by Jugoslav as an example of using pipes on Windows, redirecting own I/O to pipe ends etc. Everything works fine. I was able to print to console (using unit 0) and to pipe (using *) within the same program. However, have another pretty complex software, which prints to console, calls other programs (execs), and reports progress by using Fortran carriage control (in order to overwrite previous lines). And also within this code I need to implement pipes for IPC with one of the exec. And here problems start. It seems that commands are not written to pipe correctly. What I'm doing wrong, or where can I learn on what to do to make this working (correctly). I have some (limited) experience with this on Linux but...

Is this issue with carriage control or I did something wrong in a different place??

As an additional info - other execs also use Fortran carriage control (to print it's own status in single line) except the "piped" exe (I guess).

this is portion of Jugoslav's example and below I simply try to write (*, *) command string.
---------------------------------------------------------------------
IF (CreateProcess(NULL, Path, NULL, NULL, TRUE, 0, NULL, NULL, SI, PI).EQ.FALSE) THEN
WRITE(*,*) "Cannot create child process. Error code = ", GetLastError()
STOP
END IF

!Redirect our own I/O to appropriate pipe ends
!NB: you have to SetStdHandle BEFORE you write(*) anything to it.
!In a gui app, these handles are normally not used anyway.
iret = SetStdHandle(STD_OUTPUT_HANDLE, hChildStdInWr)
iret = SetStdHandle(STD_INPUT_HANDLE, hChildStdoutRd)
---------------------------------
My statements follow here.

------------------------------------------------------------
A.

0 Kudos
5 Replies
Jugoslav_Dujic
Valued Contributor II
388 Views
Um, I admit I've lost you... for the start, you refer to this kind of carriage control? If you do, I must say that a) it's Greek to me and b) I don't know how it would interact with pipe mechanism, but I can imagine that it would interact badly.

Note that you can "select" what you will read/write to the pipe and what to the screen with some code changes (i.e. without using SetStdHandle for your own output), for example (untested):

INTEGER, PARAMETER:: PIPE=42
INTEGER, EXTERNAL:: UOPEN
OPEN(PIPE, FILE="MyPipe", USEROPEN=UOPEN)

WRITE(PIPE, "(A)") "Foobar" !This goes to the pipe
WRITE(*, "(A)") "Foobar" !This goes to the screen
...
!=======
INTEGER FUNCTION UOPEN( FILENAME, DESIRED_ACCESS, SHARE_MODE, &
 A_NULL, CREATE_DISP, FLAGS_ATTR, B_NULL, UNIT, FLEN )
!Declare all args as integer -- shouldn't matter as you don't use them.
!hChildStdInWr should be global, or CreatePipe here:
UOPEN = hChildStdInWr
END FUNCTION UOPEN
In the code, just change WRITE(*) to WRITE(PIPE) where appropriate, i.e. skipping the progress messages and like.
0 Kudos
ArturGuzik
Valued Contributor I
388 Views
Dear Jugoslav,

many thanks for your, as always invaluable, advice. I wanted to do exactly that, I mean, separate read/write to pipe and to console (in the presence of Fortran carriage control setting). Sorry for not being clear enough about that from the very beginning. I have no idea what's wrong with Fortran carriage control (I had in mind that what you mentioned, where + outputs the record (at the current position in the current line) and a carriage returns), and where it messes up with pipe.

I've just used your suggestion in a test code by simply replacing:

iret = SetStdHandle(STD_OUTPUT_HANDLE, hChildStdInWr)
iret = SetStdHandle(STD_INPUT_HANDLE, hChildStdoutRd)

with

OPEN(PIPE, FILE="MyPipe", USEROPEN=UOPEN)

It seems to work OK. Thanks a lot.

Please let me ask you two more things:

(1) how should I correctly clean up after using that (CloseHandle, close file?)

(2) I'm getting this warning (for all of 4 handles)

Warning: %LOC is being stripped from argument which has REFERENCE and IGNORE_LOC attributes. [HCHILDSTDINRD]

Should I do something about it or it's just the way it is.

Thanks once more for your help.

A.

0 Kudos
Jugoslav_Dujic
Valued Contributor II
388 Views
ArturGuzik:

(1) how should I correctly clean up after using that (CloseHandle, close file?)


I'm fairly sure that normal Fortran CLOSE(PIPE) calls (among other things) CloseHandle, so you probably don't have to do anything. I'm not sure if CloseHandle for the opposite end is required, but it probably won't do harm.

ArturGuzik:

(2) I'm getting this warning (for all of 4 handles)

Warning: %LOC is being stripped from argument which has REFERENCE and IGNORE_LOC attributes. [HCHILDSTDINRD]

Should I do something about it or it's just the way it is.


That sounds as if you have used LOC() or %LOC() around the argument where it wasn't necessary (i.e. the argument is already declared as REFERENCE in the function's INTERFACE block). The warning is harmless in itself.
0 Kudos
ArturGuzik
Valued Contributor I
388 Views
Jugoslav,

thank you again for educating me.

The only place where LOC is used (i was playing with your example code) is:

iret = CreatePipe(LOC(hChildStdinRd), LOC(hChildStdinWr), SA, 0)
iret = CreatePipe(LOC(hChildStdoutRd), LOC(hChildStdoutWr), SA, 0)

so understand that LOC is not required here.

thanks once more.

A.

PS. When trying to reply (and write text) I'm getting "irresponsive JavaScript" warning message in my FireFox, which also becomes ... irresponsive. Is that normal or my setting/FireFox is incorrect/wrong?
(while editing message, what I'm doing right now, is without any problem).

0 Kudos
Steven_L_Intel1
Employee
388 Views

Artur,

The forum problem with the Firefox browser is known. I'm told it's being worked on, but it has been a long time. I recommend using the IE Tab extension and tell it to load this forum using IE.

0 Kudos
Reply