Exforsys
+ Reply to Thread
Results 1 to 10 of 10

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 the executables ...

  1. #1
    itzsug is offline Junior Member Array
    Join Date
    May 2004
    Answers
    22

    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


  2. #2
    cddvd is offline Junior Member Array
    Join Date
    Jun 2004
    Answers
    6

    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


  3. #3
    itzsug is offline Junior Member Array
    Join Date
    May 2004
    Answers
    22

    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


  4. #4
    akpraveen Guest

    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.


  5. #5
    bnaveens is offline Junior Member Array
    Join Date
    Sep 2004
    Answers
    1

    Re:using a variable between 2 programs

    you may also try creating a shared memory area and putting that variable in there.


  6. #6
    deepakmenon181 is offline Junior Member Array
    Join Date
    Sep 2004
    Answers
    2

    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.


  7. #7
    kishleo is offline Junior Member Array
    Join Date
    Aug 2004
    Answers
    4

    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.


  8. #8
    vijaykumarreddy is offline Junior Member Array
    Join Date
    Mar 2006
    Answers
    2

    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


  9. #9
    priyaraji is offline Member Array
    Join Date
    Apr 2006
    Answers
    52
    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


  10. #10
    rahul246 is offline Junior Member Array
    Join Date
    Mar 2006
    Answers
    3
    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


    •    Sponsored Ads



Latest Article

Network Security Risk Assessment and Measurement

Read More...