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

Calling other programs from Visual Fortran

gsfantos
Beginner
3,454 Views
Hi there,
I was wondering if you could have two fortran programs, where the second one is being called durring the run of the first one. Can they interchange information at real time? What commands should I use to start another program in the source file?
Thanks in advance
0 Kudos
28 Replies
Jugoslav_Dujic
Valued Contributor II
921 Views
How are you opening it? Both CreateMutex and OpenMutex should work. I have no particular advice except to perform a sanity check -- i.e. if the case of mutex name is right, if it's properly char(0) terminated etc. What does GetLastError say?
0 Kudos
gsfantos
Beginner
921 Views

To be honest I donot know what was the problem. However now I solved it.

I am struggling to understand how the mutex works. I am working on the event-programs you sent me before. I don't understand when the mutex becomes signalled and when the thread owns it or not. The WaitSing...function is not locking always and gives me 0. I understand that this has to do with the fact that the thread owns the mutex. But initially when I am creating the mutex in the main program: CreateMutex(NULL,FALSE,"m1"), and I start the sub-prog: LRes(1) = SYSTEMQQ("start .DebugMutexTest2.exe") the sub-program opens the mutex: OpenMutex(MUTEX_ALL_ACCESS,FALSE,"m1") properly. The main now, even though it has after the systemqq a WaitForSingleObject(hMutex, INFINITE) it doesn't lock there until the sub-program to finish.

Probably there is something I am doing wrong.

0 Kudos
Jugoslav_Dujic
Valued Contributor II
921 Views
Yep, I also had a hard time understanding mutexes. The bottom line is that you can only ReleaseMutex. After that, whoever had WaitFor*Object(mutex) then owns the mutex (until it releases it explicitly). So, a mutex can be "owned" if at least one thread has been awakened from its waiting, but it can also be "abandoned" if no one has been awaken (i.e. if it's explicitly released while no one else was waiting).

I noticed you wrote CreateMutex(NULL,FALSE,"m1"). I'm not sure if that's the actual code, but that ought to be "m1"C or "m1"//char(0). You can get all sorts of inconsistent behaviour if you forget to null-terminate the strings.
0 Kudos
gsfantos
Beginner
921 Views
I could say that with the help you gave me with the events program, I have completely understand and being very happy to work with events. The problem that I am experience is that for my problem every sub-program must first finish each job, so every time each one is finished I specify
SetEvent(hEvent(RRVE))
and to keep it waitng all the others sub-programs to finish and the main-program to get the signal to process and to re-give the job to the subprograms I specify
WaitForMultipleObjects(MRVE, LOC(hEvent),TRUE,INFINITE) for the sub-programs and for the main.
When every sub-program finishes the above statement unlocks and in the main every event is reseted while the subprograms are waiting a default sleepqq().
Now the problem with the above is that even though is working perfectly , and the above is repeated several times, at one moment I have an overflow of some sub-programs. That's why I tried the mutex option just to check with another philosophy how it behaves.
But now I think I will have to find another way to prevent any unexpected flow. The goal I think it would be to constrauct in a way that each time the sub-programs start, to be sure that the main-program has done its work before, and vice-versa
0 Kudos
gsfantos
Beginner
921 Views

Have you ever experience the fact having for example 10 threads, and up to a certain moment everything to work well until a moment where even though the main program sets the events correctly (returns 1), the specific sub-program hasn't manage to restart and reset each event. At the end of the day the main program goes to waitformultiple objects statement and passes it without all the sub-programs to have re-started and reseted their events. I understand that this is a problem of synchronization due to the multiple threads in a single processor. Is there a way the main program to know that definetely all the events have been reseted before to go to waitmultip?

Any ideas

0 Kudos
Jugoslav_Dujic
Valued Contributor II
921 Views
WaitForSingleObject with zero timeout will tell you the state of the event -- WAIT_TIMEOUT if not signalled, WAIT_OBJECT_0 if signalled.
0 Kudos
Jugoslav_Dujic
Valued Contributor II
921 Views
I presume, so would WaitForMultipleObjects with zero timeout.
0 Kudos
gsfantos
Beginner
921 Views
Thanks a lot, I am working on it, even though it seems to be the right way for controlling overflows
0 Kudos
Reply