C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-07-2008, 10:47 AM   #1
Registered User
 
Phoenix_Rebirth's Avatar
 
Join Date: Dec 2005
Location: Cyprus
Posts: 60
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
Phoenix_Rebirth is offline   Reply With Quote
Old 11-07-2008, 10:58 AM   #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...
Ironic is offline   Reply With Quote
Old 11-07-2008, 11:00 AM   #3
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Type man shmget and look at return values. I don't see how 0 could be returned.
tabstop is offline   Reply With Quote
Old 11-07-2008, 11:32 AM   #4
Registered User
 
Phoenix_Rebirth's Avatar
 
Join Date: Dec 2005
Location: Cyprus
Posts: 60
Hmm, Yes you are right, it returns something like 55115. Don't know how I couldnt see it , thank you very much
Phoenix_Rebirth is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 01:53 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22