Exforsys

Online Training

using a variable between 2 programs

This is a discussion on using a variable between 2 programs within the C and C++ forums, part of the Programming Talk category; hi, i have 2 C programs prog1.c and prog2.c. i have compiled them separately using gcc. i have ...


Go Back   Exforsys > Programming Talk > C and C++

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-08-2004, 04:37 AM
Member
 
Join Date: May 2004
Posts: 42
itzsug
using a variable between 2 programs

hi,

i have 2 C programs prog1.c and prog2.c. i have compiled them separately using gcc. i have the executables prog1 and prog2. i want to set a value for a variable in prog1.c and should be able to access the value of the same variable in prog2.c.

prog1 and prog2 would be continously running.

how can achieve this?

thanks

from
jagadish
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 09-08-2004, 05:25 AM
Junior Member
 
Join Date: Jun 2004
Posts: 7
cddvd
Re:using a variable between 2 programs

well, i gues you have to store your variable/value some where between your two programs so that you can access from both the programs, why don dont you try storing your variable value in a file separately and you can open this file in any program you want and access the value
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-08-2004, 05:42 AM
Member
 
Join Date: May 2004
Posts: 42
itzsug
Re:using a variable between 2 programs

Hi cddvd,

thanks a lot for the information. The thing the value of the variable is changed many times. So do u think using a file would be feasible? can u suggest any other possibility?

thanks

-Jagadish
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 09-08-2004, 03:13 PM
akpraveen
Guest
 
Posts: n/a
Re:using a variable between 2 programs

Hello Jagadish!

I think it is quite obvious that a \"buffer\" should be used to stored the value of the variable whose value changes frequently. This \"buffer\", or a temporary memory location, can be
1. A file
2. Database
3. Memory Location
4. Attribute Indicators/Flags

Since you are coding in C, the language allows better control over memory allocations and usage unlike Java! The technique I am proposing, is only theoretical. I haven\'t implemented it. It is your responsibility to try!

Assign Memory Location for variable DATA (using malloc() or some other library function). Assign Memory Location for variable FLAG.

Assuming there are two programs in the memory, PROG1 and PROG2, and if DATA changes in PROG1, then point PROG2 variables to the same memory location as DATA and FLAG. A flag can be just a boolean! If DATA from PROG1 changes, then set the FLAG to true and update DATA. From PROG2, run a thread every 20 seconds or whatever time period you want, to keep checking the FLAG. If the FLAG state changes, then retrieve the data from DATA variable.

I hope you understand what I am trying to say! This is just pseudo-code.

Good luck.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 09-15-2004, 04:46 PM
Junior Member
 
Join Date: Sep 2004
Posts: 1
bnaveens
Re:using a variable between 2 programs

you may also try creating a shared memory area and putting that variable in there.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 09-16-2004, 10:19 AM
Junior Member
 
Join Date: Sep 2004
Posts: 5
deepakmenon181
Re:using a variable between 2 programs

if ur using linux for running ur program then use Named pipe or fifo.
there is a system call to create a Named pipe(fifo).
so ur prog1 should create the fifo and write the value to it.
then the prog2 can read the value from fifo.
there are other techniques --queue,shared memory,rpc etc .
but using a fifo is more simpler.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 09-16-2004, 01:24 PM
Junior Member
 
Join Date: Aug 2004
Posts: 12
kishleo
Re:using a variable between 2 programs

yeah you can use memory locations but i dont advice you to use the memory location coz you have the chance of loosing data once if something goes wrong with the system. if you use the database or files you can have your value there all the time.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 03-16-2006, 10:41 AM
Junior Member
 
Join Date: Mar 2006
Posts: 2
vijaykumarreddy is on a distinguished road
use extern keyword to store

u can use extern keyword to store the value which u want to use in more than one program try for help on extern by typing extern in the c editor and pressing ctrl+F1

i dont know the exact process
but i am sure it will work
try it
all the best
use the variable with preceding extern keyword in the declaration part
if it works plz post me
thank u
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 04-28-2006, 08:10 PM
Member
 
Join Date: Apr 2006
Posts: 65
priyaraji is on a distinguished road
Hi,
Since the value of the variable in prog1.c is going to be always edited what you could do is make the value of the variable in prog1.c to be written into a file and make prog2.c to read the value of the variable from this file.You can use this using file handling functions in C

Sripriya
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 03-10-2007, 04:28 PM
Junior Member
 
Join Date: Mar 2006
Posts: 5
rahul246 is on a distinguished road
hi kishleo,
can you give me your contact no.. there is something i need to talk about the post in the ustechsolutions forum. I need to remove my email ID from this post, can you help me in that ?
You are the moderator of that group, so this can be done only by you..
anurag
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

Thread Thread Starter Forum Replies Last Post
Atari 8-Bit Computers: Frequently Asked Questions Michael Current Tech FAQ 0 10-25-2005 01:34 AM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit Tech FAQ 0 09-01-2004 07:00 AM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit Tech FAQ 0 07-03-2004 08:27 PM


All times are GMT -4. The time now is 05:02 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2004 - 2007 Exforsys Inc. All rights reserved.