Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2009-10

Linux IPC (SHM) : Getting Strange Value for shm_nattch (# of attachements)

4 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Tom__G

Hi,

I try to get number of processes attached to a specific shared memory segment.
So I read the shmctl record to retrieve this into shared memory :

type TShmid_ds = record
shm_perm: TIPC_Perm; Permissions
shm_segsz: cint; Segment size
shm_atime: time_t; Last access time
shm_dtime: time_t; Last detach time
shm_ctime: time_t; Create time
shm_cpid: ipc_pid_t; Creator PID
shm_lpid: ipc_pid_t; Last operation PID
shm_nattch: Word; Number of attachments <--<< !
shm_npages: Word; Number of pages
shm_pages: Pointer; ?
attaches: pointer; ?
end;

I just created a simple function that I added to example found here : http://www.freepascal.org/docs-html/rtl/ipc/shmctl.html http://www.freepascal.org/docs-html/rtl/ipc/shmctl.html

...
Function Get_Nb_Proc(ID : longint) : word;
Var
  data : TSHMid_ds;
  shmctl_result : integer;
begin
  shmctl_result := shmctl (shmid,IPC_STAT,@data);
  writeln ('shmctl_result :', inttostr(shmctl_result));
  If shmctl_result=-1 then
  begin
    writeln ('Error : shmctl :',fpgeterrno);
    halt(1);
  end
  else
    Get_Nb_Proc:=data.shm_nattch;
end;
...

I tested and attached 1, 2 or 3 max processes to the segment. But I always get strange high value while 'ipcs -m' shows me correct number of attachements :

ipcs -m
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status 0x53030001 24379392 tom 666 100 2

./shmtool1
Shared memory -1 exists. Opening as client
shmctl_result :0
Number of Processes Attached to Shared Memory Segment 24379392 : 40920 <--<< !
Press to Close

Does anyone know what happens ?
Thanks
Tom

Henry Vermaak
to Tom__G: I try to get number of processes attached to a specific shared memory segment. So I read…

2009/10/13 Tom__G :

Show quoted text 20 lines
Hi, I try to get number of processes attached to a specific shared memory segment. So I read the shmctl record to retrieve this into shared memory : type TShmid_ds = record  shm_perm: TIPC_Perm; Permissions  shm_segsz: cint; Segment size  shm_atime: time_t; Last access time  shm_dtime: time_t; Last detach time  shm_ctime: time_t; Create time  shm_cpid: ipc_pid_t; Creator PID  shm_lpid: ipc_pid_t; Last operation PID  shm_nattch: Word; Number of attachments  <--<< !  shm_npages: Word; Number of pages  shm_pages: Pointer; ?  attaches: pointer; ? end;

This definition differs from the one I've got in
/usr/include/bits/shm.h, where shm_nattch is defined as "unsigned long int", so you may want to look into that. I haven't tried this myself, so sorry if I'm barking up the wrong tree...

Henry

Tom__G
to Henry Vermaak
Henry Vermaak wrote 34 lines
2009/10/13 Tom__G : > > Hi, > > I try to get number of processes attached to a specific shared memory > segment. > So I read the shmctl record to retrieve this into shared memory : > > type TShmid_ds = record >  shm_perm: TIPC_Perm; Permissions >  shm_segsz: cint; Segment size >  shm_atime: time_t; Last access time >  shm_dtime: time_t; Last detach time >  shm_ctime: time_t; Create time >  shm_cpid: ipc_pid_t; Creator PID >  shm_lpid: ipc_pid_t; Last operation PID >  shm_nattch: Word; Number of attachments  <--<< ! >  shm_npages: Word; Number of pages >  shm_pages: Pointer; ? >  attaches: pointer; ? > end; This definition differs from the one I've got in /usr/include/bits/shm.h, where shm_nattch is defined as "unsigned long int", so you may want to look into that. I haven't tried this myself, so sorry if I'm barking up the wrong tree... Henry _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Thanks for suggestion. You may actually talk about this mismatch :

C : unsigned long int (4 bytes) : 0 to 4 294 967 295 FP : word : integers from 0 to 65535

Into my /usr/share/fpcsrc/2.2.4/rtl/unix/ipc.pp I found :

{ ---------------------------------------------------------------------- Sys V Shared memory stuff
----------------------------------------------------------------------}

Type
  PShmid_DS = ^TShmid_ds;
{$ifdef linux}
{$ifdef cpux86_64}
  TShmid_ds = record
    shm_perm  : TIPC_Perm;
    shm_segsz : size_t;
    shm_atime : time_t;
    shm_dtime : time_t;
    shm_ctime : time_t;
    shm_cpid  : pid_t;
    shm_lpid  : pid_t;
    shm_nattch : culong;        <--<<
    __unused4 : culong;
    __unused5 : culong;
  end;
{$else cpux86_64}
  TShmid_ds = record
    shm_perm  : TIPC_Perm;
    shm_segsz : cint;
    shm_atime : time_t;
    shm_dtime : time_t;
    shm_ctime : time_t;
    shm_cpid  : ipc_pid_t;
    shm_lpid  : ipc_pid_t;
    shm_nattch : word;        <--<<
    shm_npages : word;
    shm_pages  : Pointer;
    attaches   : pointer;
  end;
{$endif cpux86_64}
{$else} // FreeBSD checked
  TShmid_ds = record
    shm_perm  : TIPC_Perm;
    shm_segsz : cint;
    shm_lpid  : pid_t;
    shm_cpid  : pid_t;
    shm_nattch : cshort;        <--<<
    shm_atime : time_t;
    shm_dtime : time_t;
    shm_ctime : time_t;
    shm_internal : pointer;
  end;
{$endif}

My hardware here is x86 (not x86_64).