I have a long running program in C under Linux:
longrun.c
#include <stdio.h>
int main()
{
int mode=0;
int c=0;
while(1)
{
printf("\nrun @ mode %d value : %d ",mode,c );
if (c>100)
c=0;
if(mode==0)
c++;
else
c=c+2;
sleep(3);
}
return 0;
}
It will display
run @ mode 0 value : 0
run @ mode 0 value : 1
run @ mode 0 value : 2
I need to write another program in C (some thing like changemode.c
) , so that it can communicate to the longrun.c
and set its value of mode to some other value, so that the running program will
display values in incremental order of 2.
I.e., if I am running the program after some x minutes , it will display in this pattern:
run @ mode 0 value : nnn
run @ mode 0 value : nnn+2
run @ mode 0 value : (nnn+2)+2
I can do it using file method the changemode.c will create a file saying mode =2 then the longrun.c will everytime open and check and proceed. Is there some other better way to solve this, like interprocess communication?
If possible can any one write a sample of the changemode.c?