2

I need to share a variable's value which is modified in one program to reflect in another program..So I did the following.

  1. Created a header file :

    /* file1.h */
    extern int a = 0;
    
  2. created a C file:

    /* file2.c */    
    #include"file1.h"
    #include<stdio.h>
    int main()
    {
        a = 15;
        printf("%d",a);
        return 0;
    }
    
  3. created another C file:

    /* file3.c */
    #include"file1.h"
    #include<stdio.h>
    int main()
    {
        printf("%d",a);
        return 0;
    }
    

I wanted the file3.c program to print the value 15 but it gave the output as 0. How do I get the value in file3.c program also?

moooeeeep
  • 31,622
  • 22
  • 98
  • 187
Kaushik
  • 429
  • 1
  • 7
  • 19
  • The problem is when you're running file3 you haven't actually executed the file2 code to set A. Are these meant to run independently? If so you'll need to save the value e.g. to disk or use some IPC mechanism to pass it between the two. – Rup Jun 03 '13 at 09:04
  • 4
    `extern` can be used to share variables between two C files which are part of the same program, but not between two different programs. – interjay Jun 03 '13 at 09:05
  • i first execute file2.c so the value of 'a' is set.but when i run file3.c after that the value is not modified. – Kaushik Jun 03 '13 at 09:10
  • It's only set in the memory of process 2, but then that's thrown away when the process ends. And in any case process 3 has no access to the memory of process 2 (by default, anyway). So you'll need to do more work if you want to persist the value across to process 3. – Rup Jun 03 '13 at 09:15
  • You are looking for inter-process communication, e.g., _shared memory_. Also have a look at this question: http://stackoverflow.com/q/10684499/1025391 Note that this is a highly OS specific task. You may need to specifiy the operating system you are developing for. – moooeeeep Jun 03 '13 at 09:45
  • I am developing it in a Unix environment. I need to run two programs one after the other and need a value to be which is set by the first program to be used in the second program. I can always use files, but for what I am developing file operations could cause a lot of unnecessary overhead. – Kaushik Jun 03 '13 at 10:12

2 Answers2

5

What your code does is wrong; you're trying to share a variable between two processes but what you've done is sharing between two source files so declaring a variable as an extern certainly doesn't help you; because that's what you do if in one process you want to expose a variable in one source file to other source files.

A natural way to share a variable between two(or more) processes(programs) is something in UNIX systems called shared-memory.

Please check this link for an introduction. http://www.cs.cf.ac.uk/Dave/C/node27.html

Edit: Of course, inter-process communication(how two processes talk with each other) isn't limited to shared memory, you can also share a variable by sockets or through pipes

Fingolfin
  • 5,363
  • 6
  • 45
  • 66
2

It is not possible to share variable just by including files. The two executable which you will be running will end as two different processes each having its own memory space. So no way you can communicate just by including headers. You will have to use one of the ipc mechanism provided by your OS on which you are working. Something like share memory in unix or very simple pipe.

praks411
  • 1,972
  • 16
  • 23