Thread: Cannot create shared memory

  1. #1
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68

    Cannot create shared memory

    Hi, I am trying to create a client0server application and i need to create some shared memory for some procedures that all children have access (clients). My problem is that the system doesn't let me to create it.
    Code:
    #include <stdio.h>
    #include <errno.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <sys/ipc.h>
    #include <sys/shm.h>
    #include "functions_Server.h"
    
    int main() {
    
    	int client_len, loc_socket, rem_socket;
    	int shm_id;
        char* data;
    	struct sockaddr addr;
    	struct sockaddr client_addr;
    
      
    	loc_socket=socket(AF_UNIX,SOCK_STREAM,0);	
    	unlink(SOCKFILE);						
    	bzero(&addr,sizeof(addr));			
    
    	addr.sa_family=AF_UNIX;
    	strcpy(addr.sa_data,SOCKFILE);	
    
    	if (bind(loc_socket,&addr,sizeof(addr))<0)	{	
    		printf("Server bind failure %d\n",errno);	
    		perror("Server:");
    		exit(1);
    	}
    
    	if (listen(loc_socket,LISTEN_Q)<0)	{			
    		printf("Server listen failure %d\n",errno);	
    		perror("Server:");							
    		exit(1);									
    	}
    
    	//HERE IS MY PROBLEM
        shm_id= shmget(SHM_KEY, SHM_SIZE, 0600 | IPC_CREAT);
        if (shm_id!= 0) {
           printf("Could not create shared memory!\n");
           exit(1);
        }
         //HERE IS MY PROBLEM
        printf("/n%d/n",shm_id);
    
    	for (;;)	{	
    		if ((rem_socket=accept(loc_socket,&client_addr,&client_len))<0){
    			printf("server accept failure%d\n",errno);
    			perror("Server: ");
    		}
    		printf("\n%d\n",rem_socket);
    	/*rest of code here */
    		close(rem_socket);
    	}
    
    }
    The program compiles just fine but when it gets to the part "HERE IS THE PROBLEM" it cant create the memory and it prints that corresponding message. My question is why cant it create it, what's wrong. I tried on UNIX and on UBUNTU but is the same

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    35
    shmget returns -1 on error. and memory identifier on success.

    you need to test if return is -1 and not if different from 0

    you are getting memory identifier in shm_id, and thinking it means error...

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Type man shmget and look at return values. I don't see how 0 could be returned.

  4. #4
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68
    Hmm, Yes you are right, it returns something like 55115. Don't know how I couldnt see it , thank you very much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with linked list and shared memory
    By Sirfabius in forum C Programming
    Replies: 10
    Last Post: 11-10-2008, 04:45 PM
  2. Shared Memory semaphores?
    By Ironic in forum C Programming
    Replies: 0
    Last Post: 10-31-2008, 07:13 PM
  3. Thread Prog in C language (seg fault)
    By kumars in forum C Programming
    Replies: 22
    Last Post: 10-09-2008, 01:17 PM
  4. Shared memory implementation using thread
    By kumars in forum C Programming
    Replies: 5
    Last Post: 06-18-2008, 04:24 AM
  5. shared memory not getting freed
    By Elkvis in forum Linux Programming
    Replies: 19
    Last Post: 02-29-2008, 04:48 PM