0

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?

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Kajal
  • 223
  • 4
  • 15
  • Have you googled or looked on wikipedia for [inter-process communication](http://en.wikipedia.org/wiki/Inter-process_communication)? There are a lot of methods to accomplish this. As far as socket communication, I've used the example programs from http://www.linuxhowtos.org/C_C++/socket.htm several times. Or, to stick with your suggestion of a file, you could make use of [inotify](http://www.ibm.com/developerworks/linux/library/l-ubuntu-inotify/index.html) to get a little fancy. Keep in mind blocking/non-blocking capabilities of some of these operations. – Macattack Jun 20 '13 at 19:58
  • You really don't need to write changemod.c at all. Just have the current pgm open (nonblocking) a FIFO. Periodically read the FIFO (or use `select` to tell you when to read). On the other side you can write to the FIFO from the command line. – Duck Jun 20 '13 at 21:04

3 Answers3

0

One of the most basic ideas in Unix programming is process forking, and the establishment of a pipe between the 2 processes. longrun could start by creating a pipe, calling fork, and using the parent process as the changemode 'monitor' process, and the child process as you use longrun now. You will need to periodically read / write on either end.

A google search will return many examples. Here's another.

Brett Hale
  • 21,653
  • 2
  • 61
  • 90
0

As mentioned in other answers, you need some kind of inter-process communication. You can find more info on the topic in the "Beej guide to Unix IPC" (it's a "classic"), available at:

http://beej.us/guide/bgipc/

Fernando

Fernando
  • 97
  • 4
0

The solution has two parts:

  1. A communication channel between the two processes. Unix Domain Sockets are a good tool for it, and they behave similarly to TCP/IP sockets.

  2. Replacing sleep with select. select will listen on the socket, handling communication with the other program. You can also specify a 3 second timeout, so when it returns 0 (meaning no activity on the socket), you know it's time to print some output.

As an alternative to #2, you could use two threads - one sleeping and producing output, the other handling the socket. Note that any data shared by the threads should be synchronized (in your very simple case, where there's just one integer, you probably need nothing, but you sure do when it gets more complicated).

ugoren
  • 16,023
  • 3
  • 35
  • 65