Thread: Exiting Endless Loop

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

    Exiting Endless Loop

    Ok , so I have a question. I wrote some programs for some assignments where I have a server and a client/clients and they interact. One was a bulletin board system (very basic and primitive of course) the other a mail server-client and the other a chat...

    I completed them but there is one thing bugging me; In all these the server goes into an endless loop where he accepts connections and creates new threads or processes that complete tasks. So my question is this: After the endless for loop I have some statements that detach shared memory from processes and delete it or destroys thread attributes and mutexes. But how do I get these commands to execute. I mean the only way to terminate the server is by ctrl-c. But when I press that doesnt the whole program closes at that point? I thought about doing it like normal programs - using a variable - and when catching the signal of ctrl-c after asking the user whether to exit the server or resume setting the variable so I can break from the for-loop i.e
    Code:
    for (;;)    {
    ...
        if (c) break;
    ...
    }
    but I want to end it instantly. With this way the program will most definitely accept another connection and THEN terminate because after a connection is accepted it will loop back and block at the accept statement waiting for an incoming connection.. So any ideas? [This is strictly curiosity from my part I dont need it right now for anything]

    My code in the above always looks something like this

    Code:
    int main()    {
    ...
    ...
    ...
    for (;;)     {
        sd = accept(...);
        ...
        if (!fork())    {
             child process here
        }
        parent here
    }//I am talking about the following statements
    sem_unlink(...);
    shmctl(...);
    }
    or


    Code:
    int main()    {
    ...
    ...
    ...
    for (;;)     {
        sd = accept(...);
        ...
        pthread_create(...);
    
    }//I am talking about the following statements
    pthread_attr_destroy(...);
    pthread_mutex_destroy(...);	
    
    }
    So m this is it.
    Last edited by Phoenix_Rebirth; 11-23-2008 at 04:13 PM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, so I presume the real problem here is that you don't know what state the server is in, or where in the endless loop your server happens to be?

    In that case, it's quite difficult to solve the problem.

    Using a signal handler to let your code leave through a function that cleans up is one possible approach.

    Another approach would be to have a your code accept a particular type of packet from a special list of hosts that can send a "kill message" to the socket itself. You obviously need to restrict the hosts that can do this to the ones the you trust and control, otherwise someone from the outside can down your server, which is a bad idea.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68
    Hmm, that's a good idea. Thanks matsp. Expanding on that, how about sending a "kill message" to itself. That could work... I think

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Phoenix_Rebirth View Post
    Hmm, that's a good idea. Thanks matsp. Expanding on that, how about sending a "kill message" to itself. That could work... I think
    Well, sending a kill message to itself only works if you have a way to tell it to do so - which you may. That would depend a lot on the actual server and it's purpose.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    HelpingYouHelpUsHelpUsAll
    Join Date
    Dec 2007
    Location
    In your nightmares
    Posts
    223
    (if possible) it is usually better to not use infinate loops, instead defining the terminating condition. This also saves using an if and logically should manage memory better (even though the program will probably free the memory anyway)
    long time no C; //seige
    You miss 100% of the people you don't C;
    Code:
    if (language != LANG_C && language != LANG_CPP)
        drown(language);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can a "switch" be inside a loop?
    By gmk0351 in forum C Programming
    Replies: 5
    Last Post: 03-28-2008, 05:47 PM
  2. Fscanf endless loop
    By krum in forum C Programming
    Replies: 3
    Last Post: 12-05-2007, 12:14 AM
  3. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 03:48 AM
  4. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM