0

i have tried sharing a global variable between different source files in c using extern. it seems like each program creates locally different copies of the variable and as a result, when a program changes its value which also changes is not visible to the second program..how can i repair this? The program is given below:

tools.h

#ifndef  __TOOLS__
#define  __TOOLS__
#include <errno.h>
#include <stdlib.h>

extern int i;

void init();

#endif 

tools.c

#include "tools.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>

int i;

void init(){

i=0;
}  

prog1.c

#include "tools.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc,char *argv[]){

i=1;
printf("%d\n", i);

return 0;
}

prog2.c

#include "tools.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc,char *argv[]){

sleep(1);
printf("%d\n", i);

return 0;
}

prog1 printed 1

prog2 printed 0 (the target was to print 1 - see the change to the value prog1 did)

2
  • 2
    Your program has undefined behaviour, since it defines main twice. Commented May 21, 2014 at 21:52
  • 2
    If you created two executables, one using prog1.c and the other using prog2.c, then both programs are behaving normal. If you used both of them, you should get a linker error. Commented May 21, 2014 at 21:55

4 Answers 4

5

The C language describes the behaviour of one program. You seem to have multiple different programs. Different programs don't interact with one another other than explicitly through the I/O system (FILE*) or the system interface, in a platform-dependent manner (e.g. shared memory on System-V).

Sign up to request clarification or add additional context in comments.

Comments

1

The scope of a global variable is limited to a single application/executable.

Comments

0

I don't see anywhere in prog1 or prog2 where you call tools' init method.. So the fact that it's printing 0 in prog2, is probably just whatever the compiler initialized it to.

Also, if you're compiling these as two separate programs, it will never do what you want. For that, you need to look into "shared memory" or "IPC" systems, to allow two simultaneously running programs to share data at runtime.

1 Comment

i is global, so it has to be 0 by default.
0

This isn't how computers work.

Since you show two different main functions, I'm going to assume that prog1 and prog2 are two separately compiled executables.

In all modern operating systems, programs are prohibited from accessing memory outside of their own designated space. This is dicated by the operating system, and enforced by any number of features on the CPU.

Typically, each process runs in its own virtual address space. That means I can't write a program that directly modifies the memory of my notepad.exe. This is a critical feature that allows systems to be as stable as they are today.

If you want to share information between to separate running programs, or even two instances of the same program, (these are all separate processes), you'll need to implement some kind of Inter-Process Communication (IPC).

3 Comments

This is very misleading. Even if all programs ran directly in physical memory, they still wouldn't be sharing the variable i in question. (The linker and loader would just have to work differently to support such a platform.)
@KerrekSB I don't know about "misleading", because I'm emphasizing the fact that one way or another (paged virtual memory, segmentation, etc.), programs are not allowed to stomp on each other, as dictated by the OS and enforced by the CPU.
But that's completely incidental. You don't need (to know about) virtual memory, paging, segmentation or RMS's favourite tie colour to explain the OPs situation. The C language suffices.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.