Find the answer to your Linux question:
Results 1 to 4 of 4
hi to all... i have int variable in my code but i need two process one of them read it from the user and the other show it in increment ...
  1. #1
    Just Joined!
    Join Date
    Nov 2009
    Posts
    4

    Talking two processes use the same variable

    hi to all...

    i have int variable in my code but i need two process one of them read it from the user and the other show it in increment by one all the time regardless the user enter value or not(if no new value show the previous one) .... the problem that if i use fork or clone and treat with it as a global variable the other process dosnt show the new value.

    or : i use shared memoy(good solution) between two process but using two code , i cant put the all work in the same code.

    any one have a solution ....

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    You have to use shared memory to do what you say you want. All forking does is clone the global data segment.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Nov 2009
    Posts
    4
    ur answer not clear..........

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Each program that is started in Linux is started with a fork() and/or exec(). From the command line, the shell forks and then execs the process specified. All processes have their own memory. The only memory that is shared by default is program code, so two programs that are the same will share code, but not data. Likewise, every program that uses a common shared library, shares the code segment of the library, but not the static, global, or other data memory it uses. Each program gets its own data segment. In order for 2 or more programs to share data in linux either the programs have to pass the data back and forth, or they need to use what is called shared memory. There are system function calls to allocate and/or link a pointer to shared memory that can be accessed (read or written) by more than one program. That is what you seem to want to do.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...