earlati2 has asked for the wisdom of the Perl Monks concerning the following question:

I need to access a pubblic shared memory , given it's pathname as using ftok C function.

key_t ftok(const char *pathname, int proj_id);

It seems that in perl ftok is non available so for now I build my own implementation of ftok which I call from perl as show here:

$file_shmem = "/tmp/IPC/shared_memory1"; $size_shmem = 18640; open( F, "./ftok $file_shmem 1 |" ); $s1 = <F>; close F; chomp $s1; $ipckey = "0x$s1"; print "IPC-KEY of ($file_shmem) is $ipckey \n"; $idshm = shmget( $ipckey, $size_shmem, 0666 ) || die "\n Creation o +f shared memory failed $! \n"; print "IPC-SHMEM: id $idshm \n"; shmread( $idshm, $data_shmem, 0, $size_shmem ) || warn "\n\n Error +reading shared-memory: $! \n";

All this will work, but is not polite and it seems strange to me that perl have not an implementatio for ftok.

Can you provide me some suggestion on that topic ?

regards, Enzo

Code tags added by GrandFather

Replies are listed 'Best First'.
Re: use ftok to access shared memory
by GrandFather (Saint) on Feb 15, 2006 at 09:54 UTC

    You may be interested in this thread which uses ftok from the module IPC::SysV.

    As your code stands it can not possibly work - function calls don't interpolate. open( F, "./ftok $file_shmem 1 |" ); will attempt to open a file called "./ftok /tmp/IPC/shared_memory1 1 |", which is probably not what you expect.


    DWIM is Perl's answer to Gödel
      The OP is using the pipe variant of open(). I presume that ./ftok is a C program that reads (via key_t ftok(const char *pathname, int proj_id);) the shared memory for pathname /tmp/ICP/shared_memory and proj_id 1, sending stuff on STDOUT.

      Flavio
      perl -ple'$_=reverse' <<<ti.xittelop@oivalf

      Don't fool yourself.
        It is as you guess.

        enzo@P0101222 shared_memory$ ./ftok /prd/b_BCM/dat/IPC/shared_memory1 1

        101a3ce

        I read the value 101a3ce from the stdout

        But I'm looking for a better solution