Thread: forking and sharing variables

  1. #1
    Registered User Boomba's Avatar
    Join Date
    Jun 2003
    Posts
    89

    Exclamation forking and sharing variables

    Hi,

    I wanna do something really simple.....but can't seem to find my way around it....I have three client programs connecting to one server client. I'm using fork() to create a new thread for each client that connects. The problem is i noticed that when one client changes a global float vairiable (or any vafriable for that matter)...it does not update for all the other clients. I tried t make the float variable static but its as if each of the clients have complelty seperated values.

    I want to share that variable among the three processes..am i doing somthing wrong in my server? heres the code...

    I have a global vaiable:
    static float highest_bid = 50.00;

    this is the code hat coems right after a client successfuly connects:
    Code:
     pid = fork();
                
                if (pid < 0)
                    error("ERROR on fork");
                    
                if (pid == 0)  {
                    close(sockfd);
                    sprintf(str, "Welcome the current highest bid price for the new DVD player is %0.2f", highest_bid);
                    n = write(newsockfd,str,strlen(str));
                    CheckBid(newsockfd, inet_ntoa(cli_addr.sin_addr));
                    exit(0);
                }
                else close(newsockfd);
    Here is the check bid function if you need to see it:
    Code:
    void CheckBid (int sock, char* cur_ip)
    {
       int n; 
       float bid;
       char buffer[256];
       char end_msg[256];
       
       while(1)
       {    
            bzero(buffer,256);
            n = read(sock,buffer,255);
            if (n < 0) error("ERROR reading from socket");
            time(&after);
            
            if((after - before) > SETTIME)
            {
                sprintf(end_msg, "Sorry, Bidding is over and the winner is %s for $%0.2f", cur_ip, highest_bid);
                n = write(sock,end_msg,strlen(end_msg));
            }
            else
            {
                bid = atof(buffer);
                if( bid > highest_bid)
                {
                    highest_bid = bid;
                    sprintf(end_msg,"Congrats: you have the highest bid at $%0.2f", bid);
                    n = write(sock,end_msg,strlen(end_msg));
                }
                else
                {
                    sprintf(end_msg,"Sorry, the current highest bid is now $%0.2f", highest_bid);
                    n = write(sock,end_msg,strlen(end_msg));
                }
            }              
       }
    }
    thanx in advance
    Boomba,

  2. #2
    Registered User
    Join Date
    Dec 2003
    Posts
    12
    Some of the more experienced guys here may give you a better answer, but I'll give it a shot in the meantime.

    Whenever you do a fork() function the OS will create a new process. In doing this it will copy the global varibales over, but those globals are now on a seperate heap to the parent (and seperate from each other as well). You could get around this by using threads, although then that creates other problems. If you are familiar with threads this probably wouldnt be hard to implement.

    Otherwise you could use pipes to communicate the values between the processes quite possibly.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    95

    shared memory

    If are on unix. shared memory will be the answer.
    let the parent get a shared memory segment, and pass its address to all the childs.
    All the process will be able to access the shared memory, and change it or read it.
    One word, If u want to be sure that only one process changes the memory at one time use semphores.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sharing memory
    By YALINI in forum C Programming
    Replies: 1
    Last Post: 08-28-2001, 12:47 PM