User Tools

Site Tools


gams:how_can_a_gams_process_communicate_with_another_process

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

gams:how_can_a_gams_process_communicate_with_another_process [2007/02/02 16:47] (current)
Line 1: Line 1:
 +====== How can a GAMS process communicate with another process? ======
  
 +Sometimes, you want to exchange data between a running ​ GAMS process and some other process. ​ The following example ​
 +shows how to do that.  Here a GAMS program and a C program continuously exchange data (a scalar). ​ The communication is done through ASCII and GDX files and the synchronization ​
 +through some semaphores:
 +
 +<​code>​
 +$Title Trivial example of two asynchronous processes ​
 +(one is GAMS) that exchange data through ASCII files
 +
 +$onechoV > wait.cmd
 +@echo off
 +printf "--- Waiting for %1%done"​
 +:loop
 +if exist %1%done goto finish
 +printf "​."​
 +sleep 5
 +goto loop
 +:finish
 +printf "​\n"​
 +rm -f %1%done
 +$offecho
 +
 +$onechoV > read.gms
 +Scalar Ccnt /
 +$include Ccnt.txt
 +/
 +$offecho
 +
 +* C Program that reads data from GAMS and provides date for a running GAMS job
 +$onechoV > gc.c
 +#​include ​
 +
 +void main() {
 +  FILE *fp;
 +  int  Gcnt=0;
 +
 +  printf("​C - Program started\n"​);​
 +   
 +  while (Gcnt<​10) {
 +    system("​wait.cmd G"​); ​          /* Wait for GAMS to write Gcnt.txt */
 +
 +    printf("​--- Reading Gcnt.txt\n"​);​ /* Read Gcnt.txt */
 +    fp = fopen("​Gcnt.txt","​r"​);​
 +    fscanf(fp,"​%d",&​Gcnt);​
 +    fclose(fp);
 +
 +    Gcnt = 2*Gcnt;
 +    printf("​--- Writing Ccnt.txt (%d)\n",​ Gcnt); /* Write Ccnt.txt */
 +    fp = fopen("​Ccnt.txt","​w"​);​
 +    fprintf(fp,"​%d",​Gcnt);​
 +    fclose(fp);
 +    system("​touch Cdone"​);​ /* Tell the GAMS program we are done writing */
 +  }
 +  printf("​C - Program terminated\n"​);​
 +}
 +$offecho
 +* Next line call your favorite C compiler
 +* $call cl gc.c
 +
 +* Remove old semaphores ​
 +$call rm -f Cdone Gdone
 +
 +* Start up the C Program (you can start this also from a different Window)
 +$call start cmd /K gc.exe
 +
 +* The GAMS Process
 +$eolcom //
 +scalar cnt /3/;
 +file fx / Gcnt.txt /;
 +
 +while (cnt < 10,
 +  cnt=cnt - 1;
 +  putclose fx cnt:​0:​0 ​                      // write out the current scalar
 +  execute 'touch Gdone'; ​                   // Tell the C program GAMS writing is done
 +  execute '​wait.cmd C'; ​                    // wait for the C program to write out the updated scalar
 +  execute 'gams read.gms gdx read.gdx lo=2' // From the ASCII file with the scalar create a GDX file
 +  execute_load '​read',​ cnt=Ccnt; ​           // Load the updated scalar from the GDX file
 +  display cnt;               
 +); 
 +
 +* make sure the C program also gets to write out the current scalar
 +putclose fx cnt:​0:​0 ​                        
 +execute 'touch Gdone';​
 +
 +</​code>​
IMPRESSUM / LEGAL NOTICEPRIVACY POLICY gams/how_can_a_gams_process_communicate_with_another_process.txt ยท Last modified: 2007/02/02 16:47 (external edit)