Write/Read in Shared memory


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Write/Read in Shared memory
# 1  
Old 02-20-2010
Write/Read in Shared memory

I have created and attached a shared memory segment. However I do not know how am I supposed to write and read data from it. I want to save various different data on this segment such as process IDs and other. Do you know how am I supposed to write these and be able to read such data in the correct manner, i.e. how can i read a single full pid without having to read the others. Thanks in advance and any answers given will be highly appreciated. Smilie
# 2  
Old 02-20-2010
You read from and write to it the exact same way you read from and write to any other block of memory. As such, how you use it really depends on how you want to use it, which you haven't actually said.

You don't have to write ASCII text to it. That makes it a whole lot harder in fact since you have to scan through and hunt for things instead of doing random access. It may be simplest to use it as an array of native data types, which can be anything you want, including structures. You can build most other data structures out of arrays anyway.
Code:
int fd=shm_open("/asdf", O_RDWR|O_CREAT, 0777);
int *mem=mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
ftruncate(fd, 4096); // gotta make the file big enough to match the mapping!
// You now have a shared array of integers 4096/sizeof(int) in length, yay.
// Or a shared array of characters 4096 bytes in length.
// Or a shared array of structures 4096/sizeof(my_structure) in length.
// Or...  etc.


Last edited by Corona688; 02-20-2010 at 01:12 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Shared library with acces to shared memory.

Hello. I am new to this forum and I would like to ask for advice about low level POSIX programming. I have to implement a POSIX compliant C shared library. A file will have some variables and the shared library will have some functions which need those variables. There is one special... (5 Replies)
Discussion started by: iamjag
5 Replies

2. Programming

Linux read and write external memory.

Hi everyone. I asked once here and it went well, so I'm very happy with this community. I have a new question to ask. I would like to read&write specify memory of a process which is not the process I'm doing it from, nor a child nor a parent. So, I want to do a program similar to Cheat... (3 Replies)
Discussion started by: lilezek
3 Replies

3. IP Networking

read/write,write/write lock with smbclient fails

Hi, We have smb client running on two of the linux boxes and smb server on another linux system. During a backup operation which uses smb, read of a file was allowed while write to the same file was going on.Also simultaneous writes to the same file were allowed.Following are the settings in the... (1 Reply)
Discussion started by: swatidas11
1 Replies

4. Programming

Urgent!!!! - Write/read to/from shared memory

I created a shared memory and attached it. I also created the appropriate semaphores to sync. my read/write operations. However I do not know how am I supposed to do so. Anyone has got any ideas? I want to write int the form of 1234:23:444:... where each number between : means something to the... (0 Replies)
Discussion started by: tyron
0 Replies

5. Programming

Write into shared memory segments

I have created a shared memory segment (which size is 64 bytes) using shmget, shmat e.t.c and i want to divide it into 2 areas. One area for input data and one area for output? How can i do that? Furthermore, When i have to write my input data into the shared memory segment i want to write... (3 Replies)
Discussion started by: mae4
3 Replies

6. Programming

Shared memory for shared library

I am writing a shared library in Linux (but compatible with other UNIXes) and I want to allow multiple instances to share a piece of memory -- 1 byte is enough. What's the "best" way to do this? I want to optimize for speed and portability. Obviously, I'll have to worry about mutual exclusion. (0 Replies)
Discussion started by: otheus
0 Replies

7. Programming

shared memory read/write using threads

I am looking for C program source code. Could you please help me in finding the source code required mentioned below. program to create multiple threads (one master thread and rest worker threads) and using the threads write into and read from shared memory Restrictions: Only one thread... (2 Replies)
Discussion started by: kumars
2 Replies

8. Programming

Shared memory in shared library

I need to create a shared library to access an in memory DB. The DB is not huge, but big enough to make it cumbersome to carry around in every single process using the shared library. Luckily, it is pretty static information, so I don't need to worry much about synchronizing the data between... (12 Replies)
Discussion started by: DreamWarrior
12 Replies

9. Programming

memory sharing - not shared memory -

hi, this is the problem: i want to swap a linked list between 4 processes (unrelated), is there any way i can do that just by sending a pointer to a structure? //example typedef struct node { int x; char c; struct node *next; } node; or i should send the items ( x,c ) by... (9 Replies)
Discussion started by: elzalem
9 Replies

10. UNIX for Advanced & Expert Users

Shared memory shortage but lots of unused memory

I am running HP-UX B.11.11. I'm increasing a parameter for a database engine so that it uses more memory to buffer the disk drive (to speed up performance). I have over 5GB of memory not being used. But when I try to start the DB with the increased buffer parameter I get told. "Not... (1 Reply)
Discussion started by: cjcamaro
1 Replies
Login or Register to Ask a Question