C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 25th, 2003, 05:42 PM
soccerclue soccerclue is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 53 soccerclue User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
How to share memory in C?

How do we share variable between two processes in C? Thanks in advance.

Reply With Quote
  #2  
Old May 25th, 2003, 11:35 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,867 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 2 h 46 m 38 sec
Reputation Power: 480
In what OS?

In Linux/UNIX, there are all kinds of possible ways to do this. Consider the entire subject of InterProcess Communication (IPC), which includes pipes, shared memory, and UNIX domain sockets.

In DOS/Windows, I'm not sure what there is. I think that there might be a form of shared memory in Win32 in the form of memory-mapped files (I think, so don't quote me on it).

In either case, you could always use a disk file. In Linux/UNIX, you have file-locking that prevents the one process from reading the file at the same time the other process is writing to it. I don't know whether Win32 has it.

Reply With Quote
  #3  
Old May 26th, 2003, 09:36 AM
soccerclue soccerclue is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 53 soccerclue User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Hi, I'm working on the client/server program in C in Solaris system. One of the the programs is calling fork so that it enables the multithreaded servers. The problem is when I call fork, the variables that are needed to be updated between processes cannot get the latest value. Don't worry about synchronization point/mutual exclusion part first. The problem right now is I don't know how to make global variable between two processes. If I am not wrong, sharing variables between two variables between two files just need to initialize variable by adding "extern." Does anyone has a solution of how to share variable between two processes? Can we save variable in socket? But, writing and reading from socket in the same program? How to achieve that? Is there any function in C or library that could enable sharing variable between processes? I hope it's clear for you guys now. I really need help right now. Thanks.

Reply With Quote
  #4  
Old May 26th, 2003, 12:56 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,867 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 2 h 46 m 38 sec
Reputation Power: 480
I believe that POSIX IPC (interprocess comm) is available under Solaris. It is a subject that can very easily fill a book.

Pipes:
Basically the same thing that you do with the | on the command line. Just before you fork, call the pipe() function with an array of two file descriptors (fd's). It will return in that array the read and write fd's for the pipe. Then after the fork the child has the same fd's, so the parent and child can pass data to each other via the pipe.

Named pipes:
If the two processes are unrelated, they can use a named pipe, which is created with the mkfifo() function. That pipe exists within the file system. We didn't discuss named pipes much, so I don't know the pros and cons.

Shared memory:
You can also use shared memory with the functions shmat, shmctl, shmdt, and shmget. In this case a block of memory is created with shmget and made available to a process by attaching to it with shmat.

UNIX Domain sockets:
If you are familiar with sockets programming, then create the socket within the UNIX domain instead of the internet domain; i.e., AF_UNIX instead of AF_INET.

These should give you the keywords you need to find your solution. Like I said, it's a large subject area that requires a book or a large portion of a book.

PS:
You can directly apply low-level I/O to pipes and sockets; ie, the functions open, read, write, close. We didn't have time to play with shared memory, but I'm sure there are plenty of examples out there.

Last edited by dwise1_aol : May 26th, 2003 at 12:59 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > How to share memory in C?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT