Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
google site



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Display Modes
  #1  
Old 02-08-2010
Registered User
 

Join Date: Feb 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
How to send file descriptor by shared memory

I find from Stivenson (Advanced Programming Unix Environment) "How to send file descriptor by system queue"
I make next code
PHP Code:
        int size;
        
int p_count;
    
int shmid;
    
char *ptr;
    
char *shmptr;
    
shmid_ds ds;
    
    
key_t key ftok("db.pid"1  );
    if (
key 0){
        
printf(  "ftok error \n" );
        exit(
1);
    }
 
    if ( (
shmid shmget(key4096IPC_CREAT )) < 0){
        
printf(  "shmget error \n" );
        exit(
1);
    }

    
shmptr = (char*) shmat(shmid,0,0);
    if ( (
size_tshmptr  == -1  )
    {
        
printf(  "shmat error: %s\n",  strerrorerrno) );
    }
    
    if( 
shmctl(shmidIPC_STAT, &ds) < 0){
            
printf(  "shmctl stat error: %s \n"strerrorerrno) );
    }

    
ds.shm_perm.mode 0666;        

    if( 
shmctl(shmidIPC_SET, &ds) < 0){
            
printf(  "shmctl SET error: %s \n" ,strerrorerrno));
    }

     
int fd open"dump.txt"O_WRONLY O_CREAT0666);
    *(
int *) shmptr fd;
  return 
EXIT_SUCCESS
the file descriptor (int fd=3) was transmitted with number 3, and I can have access to file.

But if I open new file (descriptor number 3) on the next programm (who is receive file descriptor ) I dont have access to descriptor of file "dump.txt"
the next programm
PHP Code:
  int size;
    
int p_count;
    
int shmid;
    
char *ptr, *shmptr;
    
    
key_t key ftok("db.pid"1  );
    if (
key 0){
        
printf(  "ftok error \n" );
        exit(
1);
    }
                
    if ( (
shmid shmget(key0)) < 0){
        
printf(  "shmget error: %s \n",strerrorerrno) );
        exit(
1);
    }
    
shmptr = (char *) shmat(shmid,0,0);
    if ( (
size_tshmptr  == -1  )
    {
        
printf(  "shmat error: %s \n",strerrorerrno) );
    }
        
//     int fd2 = open( "dump2.txt", O_WRONLY | O_CREAT, 0666);

    
int fd = *(int *) shmptr  ;

    
char buff "***\n";

    
int res write(fd buff 4);
        
printf(  "writed %d fd=%d\n",res,fd );
        if (
res<0)
        
printf(  "write error: %s \n",strerrorerrno) );

    
close(fd);

    
// delete shm segment
    
if( shmctl(shmidIPC_RMID0) < 0){
        
printf(  "shmctl error, %s \n",strerrorerrno) );    
    } 
If I uncomment string int fd2 = open( "dump2.txt", O_WRONLY | O_CREAT, 0666);
I write text to file dump2.txt.

How is correctly to send/receive file descriptor by shmemory?

can I to send socket descriptor by shared memory?

Last edited by akalend; 02-08-2010 at 07:30 PM..
Sponsored Links
  #2  
Old 02-11-2010
Registered User
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 3,095
Thanks: 6
Thanked 51 Times in 51 Posts
Quote:
Originally Posted by akalend View Post
the file descriptor (int fd=3) was transmitted with number 3, and I can have access to file.
All you've done is copy the integer 3 into some memory. This doesn't magically reopen FD 3 anywhere. how does the other method arrange to have the FD duplicated?
  #3  
Old 02-23-2010
Registered User
 

Join Date: Feb 2010
Location: /usr/src/bin
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
Two processes cannot shared a single file descriptor ,
Sending a file descriptor does not mean that you can open and access the file, its just
a number for the other process.

Otherwise send a file name via shared memory and make the other process to open and get a file descriptor for itself then access the file.
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Shared memory for shared library otheus Programming 0 09-03-2008 03:55 AM
Shared memory in shared library DreamWarrior Programming 12 05-30-2007 04:33 PM
memory sharing - not shared memory - elzalem Programming 9 05-02-2007 07:45 AM
all about shared memory vijaya2006 Linux 0 02-28-2006 03:14 AM
Shared memory shortage but lots of unused memory cjcamaro UNIX for Advanced & Expert Users 1 10-13-2004 05:10 PM



All times are GMT -4. The time now is 07:58 AM.