开发者

Using C, how can I access the same block of memory as another C program?

So, I'm on hour one of learning C. Maybe I'll jump over to C++ tonight.

I'd like to create two command line programs using C that can "talk" to each other. The first thought that came to mind is to transfer information between the two by writing to and polling files. Ho开发者_运维技巧wever, I figure C provides all the methods I need to directly allocate memory and access it, and that would be much faster and more sensible technique.

Can someone provide an example or advice?


What you are asking for is called shared memory. Unfortunately, shared memory is not part of the C language per se, but a feature of the operating system. Since you didn't specify what operating system you are using, I point you to a Unix SysV shared memory tutorial.


I believe you're referring to a Shared Memory Segment. You could also use pipes or sockets to communicate between the two processes. Depending on the application, each has its advantage.

Here's a great guide on interprocess communication, which covers the very subject in addition to pipes, sockets.

http://beej.us/guide/bgipc/output/html/multipage/index.html


The technology you need to look into is "Shared Memory". It is OS-specific, and not in the C Standard Library.

Since you didn't specify an OS, you'll have to do some research on your own. If you're on Windows, check out this page.


If you are in a unix environment you can use shmget() and shmat() to create and attach a shared memory segment. Both process assess the segment via a common integer key.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜