question on stop and wait arq protocol

Posted on 2004-10-27
Last Modified: 2008-03-10

I"m a novice, and working on a stop and wait protocol simulation. I'm wondering how can approach to show Error checking to be demonstrated by use of signals ie ^C – Damaged Frame ^Z – Lost ACK ^\ - Lost NAK in the program. I'm kinda confuse about signals and handling.  Please help, thanks.
Question by:aik_21
    LVL 23

    Accepted Solution

    Signal handler functions receive an integer defining one-of the possible
    signals defined in signal.h.   Each of the keystrokes you mention
    sends a different signal to the process*.  So your signal handler gets a
    value passed to it that corresponds to the keystroke sent:

    ^C  sends the SIGQUIT, ^Z sends EOF on STDIN, etc.   When you register
    your signal handler, you specify a mask representing the set of signals you
    will accept.  Your handler should then check which of the signals from that
    set it actually received.

    man signal

    LVL 2

    Expert Comment

    Assuming that we're talking about signals as in "SIGQUIT" etc, rather than things like "end of file" conditions...

    Signals are a bit like interrupts - they can happen at any time.
    As Brett said, "man signal" will tell you a fair amount, and following the "see also" stuff will tell you more.

    Note: Some signals can't be caught and processed by your program, e.g. SIGKILL (the classic "kill -9") kills a process stone dead so fast the process doesn't know what hit it - it just ceases to exist.  SIGSTOP is much the same, only the process gets suspended isntead of dieing.

    You've not specified what operating system you're playing with here - more detail would kinda help.

    On Solaris "man sigaction" will tell you much about how to intercept these signals - the basic idea is that you write yourself a function, and register that to be called instead of the original signal handler by calling sigaction(...) with various parameters (one of which is the number of the signal you want to over-ride).  Just note that this function can then be called at _any_ point in your program's execution - e.g. halfway through a printf call or whatever.
    On Linux it's a bit simpler (friendlier!), e.g.

    void someone_just_pressed_ctrl_c(int willBeTwo)
      fprintf(stderr,"It's no use, pressing Ctrl-C won't quit anymore\n");

    int main(int argc, char **argv)
      sighandler_t oldHandler = signal(SIGINT,someone_just_pressed_ctrl_c);
      return 0;
    Note: It's not necessary to restore the old handler if you're just about to exit anyway, but I thought I'd include it for completeness.

    I'm not sure, but there may be things you have to do in your signal handler to ensure the system knows you've finished with it, e.g. tell it to "clear" the signal.  Not sure about that - see the man-pages for the system you're using.

    Expert Comment

    On occasion, you may want your program to wait for a period of time before continuing, giving the affect of a pause. This can be achieved using a sleep function.

    To make a standard sleep function that is portable, you can use the following functions, as supplied by Prelude and Sebastiani. These simply put your program into a permanent loop until the necessary amount of time has passed. The problem with this method is that it is very CPU intensive. For a less CPU intensive, but also less portable method, keep reading.

    #include <time.h>

    void sleep_seconds ( long seconds ) {
      clock_t limit, now = clock();
      limit = now + seconds * CLOCKS_PER_SEC;
      while ( limit > now )
        now = clock();

    void sleep_ticks ( long ticks ) {
      clock_t limit, now = clock();
      limit = now + ticks;
      while ( limit > now )
        now = clock();

    void wait(long milliseconds)
      long timeout = clock() + milliseconds;
      while( clock() < timeout ) continue;

    Some compilers come with a non-standard set of functions to do the same thing, but these normally put your program to sleep without the need for a permanent loop. The advantage with these is that they don't hog the CPU.

    Check your compiler's documentation for some of these functions:

    Sleep() (maybe in windows.h)
    sleep() (maybe in unistd.h)
    delay() (maybe in dos.h)

    Be warned, some functions work in milliseconds, and some in seconds, so handle with care  
    LVL 2

    Expert Comment

    I must say, I've never seen a program (other than on machine with no operating system) use a "busy loop" (i.e. CPU intensive loop)*.
    I know such practices aren't uncommon in simple embedded devices and on MSDOS programs where there is no operating system or other programs running, but I'd have expected those to be in the minority these days.

    I would seriously recommend the use of sleep() or whatever equivalent your target platform has.  It's not unknown for system administrators to get "quite excited" about people who persist in running such programs, and such people to then find everything they do has somehow been reduced to "minimum priority", hence takes ages to do anything, assuming their files don't accidentally get lost in a highly localised disc-crash incident instead.
    If you're running on a standalone PC then it's not so big an issue, but if you're on a shared server, it's best to be nice.

    * Actually that's not quite true - I found that Windows 3.1, which was claimed to be an operating system, would use a busy-loop for some internal operations, resulting in the machine locking up for many minutes, sometimes indefinately, before the system returned to normal.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
    Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
    The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

    846 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now