Find the answer to your Linux question:
Results 1 to 6 of 6
Hello again, As part of my ongoing project I need to put together a messaging server of sorts. So far I have coded a server which accepts multiple connections, which ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2010
    Posts
    25

    How do use Globals when using fork?


    Hello again,

    As part of my ongoing project I need to put together a messaging server of sorts. So far I have coded a server which accepts multiple connections, which then runs a function that will perform a task, see below.

    On this sever I have a global structure which is populated using the data found in a read() buffer.

    However with each fork() that runs the below function each process is handed a blank structure. How could I go about allowing each fork() child process access the same structure?

    My code should hopefully show what I am trying to do?

    Code:
    void listen_for_client(int sock)
    {
        char buffer[256];
        int n;
        int len = 0;
        int r =0;
    
        char num[5] = {0};
        char name[15] = {0};
    
        char *ptr = NULL;
    
        extern sThisUnitsBay *bayBank;
    
        n=1;
        
        while(n>0){
            memset(buffer, 0, sizeof(buffer));
    
            n = read(sock,buffer,255);
    
            if(n<0)error("Error reading from socket");       
    
            ptr = strstr(buffer, ">>");
            ptr++;ptr++;
    
            strncpy(num, ptr, 1);
            
            ptr++;ptr++;
                           
            len = strcspn(ptr, "&");
            strncpy(name, ptr, len);
            name[len]=0;
            
                    
            if(strstr(buffer, "PASS"))
                r = 1;
            else r = 0;
    
           if(strstr(buffer, ">>1")){
               sprintf(bayBank->bay1.name, "%s", name);
                bayBank->bay1.r = r;
           }
           if(strstr(buffer, ">>2")){
               sprintf(bayBank->bay2.name, "%s", name);
                bayBank->bay2.r = r;
           }
           if(strstr(buffer, ">>3")){
               sprintf(bayBank->bay3.name, "%s", name);
                bayBank->bay3.r = r;
           }
           if(strstr(buffer, ">>4")){
               sprintf(bayBank->bay4.name, "%s", name);
                bayBank->bay4.r = r;
           }
                   
    
            system("clear");
            printf("BAY: 1\r\nNAME: %s\r\nRESULT: %d\r\n\n",bayBank->bay1.name,bayBank->bay1.r);
            printf("BAY: 2\r\nNAME: %s\r\nRESULT: %d\r\n\n",bayBank->bay2.name,bayBank->bay2.r);
            printf("BAY: 3\r\nNAME: %s\r\nRESULT: %d\r\n\n",bayBank->bay3.name,bayBank->bay3.r);
            printf("BAY: 4\r\nNAME: %s\r\nRESULT: %d\r\n\n",bayBank->bay4.name,bayBank->bay4.r);
    
        }
    }

  2. #2
    Just Joined!
    Join Date
    Oct 2009
    Posts
    85
    Quote Originally Posted by aManFromOuterSpace View Post
    Hello again,

    As part of my ongoing project I need to put together a messaging server of sorts. So far I have coded a server which accepts multiple connections, which then runs a function that will perform a task, see below.

    On this sever I have a global structure which is populated using the data found in a read() buffer.

    However with each fork() that runs the below function each process is handed a blank structure. How could I go about allowing each fork() child process access the same structure?

    My code should hopefully show what I am trying to do?

    Code:
    void listen_for_client(int sock)
    {
        char buffer[256];
        int n;
        int len = 0;
        int r =0;
    
        char num[5] = {0};
        char name[15] = {0};
    
        char *ptr = NULL;
    
        extern sThisUnitsBay *bayBank;
    
        n=1;
        
        while(n>0){
            memset(buffer, 0, sizeof(buffer));
    
            n = read(sock,buffer,255);
    
            if(n<0)error("Error reading from socket");       
    
            ptr = strstr(buffer, ">>");
            ptr++;ptr++;
    
            strncpy(num, ptr, 1);
            
            ptr++;ptr++;
                           
            len = strcspn(ptr, "&");
            strncpy(name, ptr, len);
            name[len]=0;
            
                    
            if(strstr(buffer, "PASS"))
                r = 1;
            else r = 0;
    
           if(strstr(buffer, ">>1")){
               sprintf(bayBank->bay1.name, "%s", name);
                bayBank->bay1.r = r;
           }
           if(strstr(buffer, ">>2")){
               sprintf(bayBank->bay2.name, "%s", name);
                bayBank->bay2.r = r;
           }
           if(strstr(buffer, ">>3")){
               sprintf(bayBank->bay3.name, "%s", name);
                bayBank->bay3.r = r;
           }
           if(strstr(buffer, ">>4")){
               sprintf(bayBank->bay4.name, "%s", name);
                bayBank->bay4.r = r;
           }
                   
    
            system("clear");
            printf("BAY: 1\r\nNAME: %s\r\nRESULT: %d\r\n\n",bayBank->bay1.name,bayBank->bay1.r);
            printf("BAY: 2\r\nNAME: %s\r\nRESULT: %d\r\n\n",bayBank->bay2.name,bayBank->bay2.r);
            printf("BAY: 3\r\nNAME: %s\r\nRESULT: %d\r\n\n",bayBank->bay3.name,bayBank->bay3.r);
            printf("BAY: 4\r\nNAME: %s\r\nRESULT: %d\r\n\n",bayBank->bay4.name,bayBank->bay4.r);
    
        }
    }
    Just for a clarification, where does the extern pointer refers to?
    One more thing if you do a fork, the parent and child are in different process context. so you should prefer to use a IPC mechanism like pipe, messagequeue or something like Shared memory(better for your case)

  3. #3
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    do you need to fork off a child process, or could what you need to do be accomplished with a thread running inside the process, thereby giving all process context to this thread?

  4. #4
    Just Joined!
    Join Date
    Oct 2008
    Posts
    7
    If you need access to the global band structure you should be using a thread for each client instead of forking to a different process. You COULD do it with different processes, just that it's more difficult. I suggest you look at the pthread library. Also, mind the fact that access to the shared resource must be synchronized.

  5. #5
    Just Joined!
    Join Date
    Jul 2010
    Posts
    25
    Quote Originally Posted by kux4 View Post
    If you need access to the global band structure you should be using a thread for each client instead of forking to a different process. You COULD do it with different processes, just that it's more difficult. I suggest you look at the pthread library. Also, mind the fact that access to the shared resource must be synchronized.
    A colleague suggested the same method. I suppose it makes sense to use threads, the code will eventually need a GUI (to please the commercial guys) so to use GTK I will end up using gThread anyways.

    I'm guessing fork has its uses but this is not it.

    Cheers

  6. #6
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    fork does have uses, but most of the time in client server application server should be spawning threads to do client requests rather than fork off new process

    fork should be reserved for spawning daemons and the like (and college OS course)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •