Thread: Question about forking with sockets listening

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

    Question about forking with sockets listening

    I am trying to create a simple server-client program (a primitive bulettin board)
    Up to now I have created the listening socket bind()ed it and I am accepting in a for(; loop. Right now when someone tries to connects to read the topics and replies I call fork() to create a separate process for handling that client. I know that fork basically creates a copy of the address space of the parent process, so what I am confused about is this:
    Inside the infinite for the very first command is accept() and after that fork. Inside the child process I can use the file descriptor from accept to trade messages with the client that connected but it's a copy of the file descriptor that was created in the parent process right.
    My code is like this:

    Code:
    ...
    for (;;)	{	
    		if ((rem_socket=accept(loc_socket,&client_addr,&client_len))<0){
    			printf("server accept failure&#37;d\n",errno);
    			perror("Server: ");
    		}
    		printf("\n%d\n",rem_socket);
    		pid = fork();
    		if (pid==0){
                int choice;
    			data = shmat(shm_id, NULL, 0);
    			if (data == (char *)-1) {
       				printf("Could not attach to shared memory!\n");
       				exit(1);
    			}
    			
    /*1*/       showMenu(&rem_socket);
    /*2*/       printf("%d\n",read (rem_socket,buf,BUFFER_SIZE));
                printf("\n%s",buf);
                choice = selection();
                close(rem_socket);  
                exit(0);
    		}	
    }
    ...
    wont there be problems? for instance when another clients tries to connect what happens? The parent process runs along with the child so won't it continue to accept (infinite loop) resulting in overwriting the rem_socket...
    I haven't understood very clearly what exactly happens when fork is used with sockets and shared memory for starters I want some clarifying with the socket part and then I can move to shared memory and semaphores,

    BTW Is it stupid and unnecessary to think about adding the sockets in the shared memory (although I have no idea right now if it's possible and how)
    Last edited by Phoenix_Rebirth; 11-08-2008 at 03:34 PM.

  2. #2
    Registered User
    Join Date
    May 2008
    Posts
    87
    If I understand your question correctly, you are worried that while a child process is servicing a client connection, the accept() call in the parent will somehow overwrite/destroy the socket that the child process is using to communicate with the remote client?

    I don't think you have anything to be worried about. When you fork(), the child gets a *copy* of all of the parent's descriptors. Further, the descriptor variables are just integers that index the OS's table of files/sockets. So, when you go back to the accept() call in the parent, you are just grabbing a new index value for use in the parent. This does not destroy the socket associated with the previous descriptor, nor does it modify the descriptor in the child process because the child has its own *copy* - its own integer that is equal to the value of the parent's at the time of fork().

    Hope that helps

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Sockets: IPv4 vs IPv6
    By ninboy in forum Networking/Device Communication
    Replies: 1
    Last Post: 10-15-2008, 01:02 AM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Hi all!, Another question about socket's & NetBios
    By Pandora in forum Windows Programming
    Replies: 9
    Last Post: 03-19-2003, 08:10 AM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM