UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » Processes
  • » Can I use fcntl to block portion of sysv shared memory?

#1 2006-12-25 03:02 PM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: Can I use fcntl to block portion of sysv shared memory?

I know it's possible to use it with posix shared memory,byt the book wirtes that Linux hasn't posix shared memory.May be there are different ways to block a part of sysv shared memory?

Offline

#2 2006-12-25 05:49 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Can I use fcntl to block portion of sysv shared memory?

Newer Linux supports POSIX shared memory.

I'm not sure if there's a way to change only a part of shared memory, permissions are per segment, not per page.

(Maybe you can modify one page from the segment with mprotect, but that might not work or might not prevent changing it back.)

Offline

#3 2006-12-26 09:30 AM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: Can I use fcntl to block portion of sysv shared memory?

In my sys/mman.h
/* Guarantee all whole pages mapped by the range [ADDR,ADDR+LEN) to
   be memory resident.  */
extern int mlock (__const void *__addr, size_t __len) __THROW;
But I think I'll not use it because I found in the same file
/* Open shared memory segment.  */
extern int shm_open (__const char *__name, int __oflag, mode_t __mode);

/* Remove shared memory segment.  */
extern int shm_unlink (__const char *__name);
Beside I didn't find any precautions about max size of POSIX shared memory(which is set by truncate call).And sysv sh.mem has ones(segment (I know I can use a couple of  them but anyway) has maximum size of 4mb).So I can use 10,20 mb,up to some greater system limit?

Offline

#4 2006-12-26 09:44 AM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: Can I use fcntl to block portion of sysv shared memory?

I tried to use posix sh.m. but get errors undefined reference to `shm_open'
,undefined reference to `shm_unlink' while linking
Maybe i Must include some library (line -phtread for threads)?Or my Linux (RH4.0)  hasn't implemented this posix sh.m.?

I just need to point librt  (/lib/librt.so.1)

Offline

#5 2006-12-26 01:56 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Can I use fcntl to block portion of sysv shared memory?

Linking to the rt library should be enough to get posix shared memory working (I've no idea how old RedHat 4.0 is).

The limits of sysv shared memory are set with /proc/sys/kernel/shm*. Shmax is set to 32Mb by default here, so that's in general plenty.

Not sure if you asked a question or not, but mlock is to pin memory in ram so you can be sure that it won't be swapped to disk.

Offline

#6 2006-12-26 03:06 PM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: Can I use fcntl to block portion of sysv shared memory?

No, I don't ask, but it's useful info anyway(so you post it ) ).

Offline

#7 2006-12-27 11:06 AM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: Can I use fcntl to block portion of sysv shared memory?

is the fcntl the fastest way to block portion of file(in my case memory)?I've read it's quite slow, because it's system call.If you know a better way to do the task post it,please

Offline

#8 2006-12-27 02:19 PM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: Can I use fcntl to block portion of sysv shared memory?

The main question for my problem is how to syncronize reading and writing (so, as many processes can read simultaneously,but only one write for each block(for example : processes 1-3 reads from 1 element while 4 is blocked,because it is trying to wite to the same number 1 element,5-6 reading from the 2 element are bolcked,because process number 7 is writing to the same,process 8 is writing to 3 element,while processes 9-10 (which are trying to write the same element) and processes 11-14(which are trying to write to the same element ) are blocked)).
As for me I think,that I can do it with fcntl call and atomic counter of related processes for each element.But I'm afraid it can be too slow beacuse of a lot of system calls fcntl

Offline

#9 2006-12-27 06:34 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Can I use fcntl to block portion of sysv shared memory?

I think a simple sysv semaphore with one sem per block would be the easiest solution. That should be reasonable fast too.

If you want to avoid systemcalls then it's gets kludgey with atomic ops in shared memory (using futex on Linux would help to avoid spinning).

But I wouldn't worry about the systemcalls being too slow...

Offline

#10 2006-12-28 08:17 AM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: Can I use fcntl to block portion of sysv shared memory?

If I use a semaphore per each block, I will have 2-4 thousands of them.Is't ok?

Offline

#11 2006-12-28 03:52 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Can I use fcntl to block portion of sysv shared memory?

The default limit of the number of semaphores per set is 250. So you'd either need more sets, or increase the limit (/proc/sys/kernel/sem). Other than that it isn't a problem, except that it's a bit overkill. Better to use 250 sems and use one for multiple blocks, e.g. hash the block number to get the semaphore number.

Offline

#12 2006-12-29 08:26 AM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: Can I use fcntl to block portion of sysv shared memory?

So If I have 4000 of records join them into 100 blocks with a semaphore (It seems I need two for each block).My records are in an array,so I possibly do not need a hash,just can put an int before each record struct, or even use defined range of seaphores' keys (f.e from 2000 to 2200).Thank you for info about sem. limit.

Offline

#13 2006-12-29 02:57 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Can I use fcntl to block portion of sysv shared memory?

Maybe you don't need a hash, but it's advantage is that the relation between block number and semaphore number is more or less random, which causes in general much less contention on the semaphores, compared to when your do fixed grouping.   But if the blocks used are randomly spread instead of close together (e.g. when sequential operations are done), then not hashing is fine.

Another advantage of hashing is that you don't have to partition the number of blocks beforehand, which is convenient if the number of blocks is dynamic.

Are you sure you need two semaphores per block? As you can't atomically lock both it might open a door for race conditions or deadlocks.

Offline

#14 2006-12-31 06:05 PM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: Can I use fcntl to block portion of sysv shared memory?

Yeah. I think I need 2 sempahores because I want multiple procerses read the same block simultaneously.

Offline

#15 2006-12-31 06:24 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Can I use fcntl to block portion of sysv shared memory?

Sounds dangerous, sure it's worth the trouble? How are you going to avoid writer starvation for instance?

Offline

#16 2007-01-03 08:23 AM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: Can I use fcntl to block portion of sysv shared memory?

Of course I'm not sure!Most I of that I'm working at is new for me.I read some articles,manuals and books and try to apply this knowledge in practice."witer starvation for instance " - you mean that writer would wait too long to write to the instance?Yes, I think it would wait for a long time,but it's not critical for my task.Here how I did it:

semW=0,semR=0;
RW
semW.Post
----------------------------------------------------------------------------------------------
semW.WaitsemW.Wait
semR.PostsemR.WaitNull
semW.Postwrite
readsemW.Post
semR.Wait

I'd like to listen to any comments, notices and corrections.
And advices :)

Offline

#17 2007-01-03 08:04 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Can I use fcntl to block portion of sysv shared memory?

"for instance" means "as an example". So saying "Give me a number, for instance 5." means that 5 is one of the possible numbers that can be given. I should have put a comma before the "for" though...

In general, splitting up the locking by read and write access is done so that there can be multiple readers while there can be only one writer. There are two variants, the one where readers can starve writers, and where one writer can starve the readers. The first happens if readers can increase the semaphore the writer is waiting on, the second happens when all readers are blocken when a writer wants to write, which is what you probably want. If the number of writes isn't much greater than the number of reads (or vice versa) then using more than one semaphore makes no sense.

Locking stuff becomes quite complex rather quick, so it's best to keep it as simple as possible, and to only increase the complexity when it turns out it's needed. So start with simple mutexes, and if that is too slow try out read/write locking, if it makes sense. But if you have fine grained locks mutexes should be plenty fast already.

If there are many threads or processes accessing the same block of data then that's the problem that needs to be avoided. You can do all you want with the locking around it, it will stay a sore spot whatever you do.

Offline

#18 2007-01-04 08:37 AM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: Can I use fcntl to block portion of sysv shared memory?

I've already written the class and tested it, so I hope there wouldn't be a problem.Thank you anyway.
How you ever deal with OCCI (Oracle C++ Call Interface)?
Besides, Happy New Year.
To everybody )

Offline

#19 2007-01-04 02:53 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Can I use fcntl to block portion of sysv shared memory?

The "procedure" you posted isn't clear enough for me to judge if the locking is correct or not. For instance, I don't see anything protecting the read against a write happening at the same time. As I don't think you'd forget something basic like that, it must be me not understanding the snippet.

I've never used OCCI, thankfully.

Offline

  • Index
  • » Processes
  • » Can I use fcntl to block portion of sysv shared memory?

Board footer

Powered by FluxBB