473,231 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

Sharing a variable across multiple files

Hi all,

I have specific question about usage of extern. I want to share a
single variable say int a; across multiple files.

I would define it in file1.c as

int a=0;
int main()
{
.........
a= 20;
..........
}

In another file2.c I would use it as,

int main()
{
extern int a;
................
printf("value of a=%d\n", a);

..................
}

Is this usage right. If yes, how do I run these as two separate
processes so that value changed in file1 is always reflected in file2
whenever it is used in fil2.c. How do I link these two processes put
it in another way.

Regards,
Aruna
Apr 7 '08 #1
2 3460
ar**********@gmail.com wrote:
>
Hi all,

I have specific question about usage of extern. I want to share a
single variable say int a; across multiple files.
....
Is this usage right. If yes, how do I run these as two separate
processes so that value changed in file1 is always reflected in file2
whenever it is used in fil2.c. How do I link these two processes put
it in another way.
Extern doesn't do what you want across process boundaries. You'll
need help from your operating system.

I'd suggest asking in a newsgroup specific to your O/S.

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/
Apr 7 '08 #2
On 7 Apr 2008 at 19:11, ar**********@gmail.com wrote:
Hi all,

I have specific question about usage of extern. I want to share a
single variable say int a; across multiple files.

I would define it in file1.c as

int a=0;
int main()
{
.........
a= 20;
..........
}

In another file2.c I would use it as,

int main()
{
extern int a;
................
printf("value of a=%d\n", a);

..................
}

Is this usage right. If yes, how do I run these as two separate
processes so that value changed in file1 is always reflected in file2
whenever it is used in fil2.c. How do I link these two processes put
it in another way.
You need to investigate your system's shared memory features. On POSIX
you can used the shm* functions and mmap - here's an extremely simple
example:
/* server.c */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>

#define MESSAGE "Hello world\n"
#define PATH "/foobar"
#define SIZE 100

int main(void)
{
int shmfd;
char *msg;

if((shmfd = shm_open(PATH, O_CREAT | O_EXCL | O_RDWR, S_IRWXU | S_IRWXG)) < 0)
abort();
ftruncate(shmfd, SIZE);

msg = mmap(NULL, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shmfd, 0);
if(!msg)
abort();
strcpy(msg, MESSAGE);

return 0;
}


/* client.c */

#include <stdio.h>
#include <stdlib.h>

#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>

#define PATH "/foobar"
#define SIZE 100

int main(void)
{
int shmfd;
char *msg;

if( (shmfd = shm_open(PATH, O_RDWR, S_IRWXU | S_IRWXG)) < 0)
abort();

msg = mmap(NULL, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shmfd, 0);
if (!msg)
abort();

printf("%s", msg);
if (shm_unlink(PATH))
abort();

return 0;
}


$ make server client LDLIBS=-lrt
cc server.c -lrt -o server
cc client.c -lrt -o client
$ ./server
$ ./client
Hello world

Apr 7 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alex | last post by:
Hi all, I want to write a "File sharing system". Is posssible with socket and thread create multisuer connection at the same time ? or is wrong my idea?? thanks
1
by: Simon Neve | last post by:
Hello, This question is related to sharing .Net projects across solutions and is reposted from the SourceSafe group. We have several different solutions and want to share common assemblies...
5
by: cybertof | last post by:
Hi ! What is the common use of sharing a single .cs across multiple project files ? I think it's to share common classes between projects. I have actually a .cs file shared accross multiple...
4
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is...
3
by: Shikari Shambu | last post by:
Hi All, I have a situation where multiple applications are sharing some pages/ controls. So, I have a separate common project that has the common pages/ controls. My question is how do I...
2
by: David | last post by:
How do I share code across files in C#, without including a reference to a class library. Here's what I want to do. I have a device I want to talk to. It's a machine in a factory. It can talk...
3
by: Ben Holness | last post by:
Hi all, I have a php/mysql website where people can upload their own graphics for the buttons and background of pages on the website. This used to run on one server, but I have now been asked...
2
by: Jeff Dege | last post by:
I'm working with a group that's been doing C++ coding for quite a long time, now, and in that environment we've pretty much worked out development practices that serve us well. We've been doing...
45
by: =?Utf-8?B?QmV0aA==?= | last post by:
Hello. I'm trying to find another way to share an instance of an object with other classes. I started by passing the instance to the other class's constructor, like this: Friend Class...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.