tags:

views:

196

answers:

2

Hi, I have created two files:

tunables.h

#ifndef TUNABLES_H
#define TUNABLES_H

void tunables_load_conservative();
void tunables_load_aggressive();

extern int timer_x;
#endif /*TUNABLES_H */

and tunables.c

#include "tunables.h"

int timer_x;

void tunables_load_conservative(){
     timer_x = 3;
}
void tunables_load_aggressive(){
     timer_x = 1;
}

All the other files of my project includes "tunables.h". When I load the project both A.c and B.c calls *tunables_load_conservative* but if, after a while, I call in file A.c *tunables_load_aggressive()* in file B.c the *timer_x* remains 3. Why?

This is my Makefile:

INCLUDE=`pwd`/include
GCCFLAGS= -ansi -O3 -pedantic -Wall -Wunused -I${INCLUDE} -DDEBUG 
GCCOTHERFLAGS= -ggdb -pg

all: A B

A: A.o tunables.o
    gcc -o A ${GCCFLAGS} ${GCCOTHERFLAGS} tunables.o

B: B.o tunables.o
    gcc -o LBfixed ${GCCFLAGS} ${GCCOTHERFLAGS} tunables.o

A.o: A.c
    gcc -c ${GCCFLAGS} ${GCCOTHERFLAGS} A.c

B.o: B.c
    gcc -c ${GCCFLAGS} ${GCCOTHERFLAGS} B.c

tunables.o: tunables.c
    gcc -c ${GCCFLAGS} ${GCCOTHERFLAGS} tunables.c

clean:
    rm -rf *.o A B
+4  A: 

It looks like you've got two separate processes, A and B. The extern declaration does not set up shared memory across processes, but instead allows different compilation units within the same process to access the same variable.

To share a variable across processes, you will need to use system-dependent IPC methods.

Greg Hewgill
Omg your right! -_-I can't notice it because I've created a shell script which run both processes in automatic. :P
Federico
+1  A: 

tunables.o is included in your project twice. Each copy has it's own copy of your timer_x variable.

If you want them to share the variable you must generate a single executable.

If you need two executables, you need to share the value some other way - perhaps using a temporary file and reading/writing from it.

Devin Bayer