Link to home
Start Free TrialLog in
Avatar of sahithi poreddi
sahithi poreddi

asked on

Modify stop and wait program by implementing SIGALRM function

Hi Experts,

I need your help as I'm new to programming.  Could you please help me out with a solution for the below program?

The program simulates different errors that the protocol checks for.

           Modify the program so that it implements "time out".

          Theory:

          Sender: sends a frame and calls a SIGALRM function and sets the timer

           for say 2 seconds. If the sender gets a response within two seconds

           from the receiver (either "ACK" or "NAK"), turn off the alarm and

           transmit the next frame. If no response is received, time out function

           kicks in and retransmits the frame.

           Receiver: When it sees an error in transmission, do not send back

           anything to the sender and this causes the time out and retransmit.


sender_c.txtreceiver_c.txt
Avatar of Duncan Roe
Duncan Roe
Flag of Australia image

Is this homework by any chance? I ask because one of your header files is called "assign1.h".
We are not allowed to do your homework, but can of course offer you general advice. Having said that, it really would be helpful if you  could post the 3 headers your code references - can you do that please?
Is the object of the assignment to (1) give you practice with signal (SIGALRM in particular) or do you (2) just need to wake up after 2 seconds?
If (2), consider calling select(2) with a timeout.
If (1)
Read up on alarm(2) (i.e. on you computer, in a command window enter man 2 alarm). When the alarm goes off SIGALARM will be raised. From signal(7), the default action of SIGALARM is to terminate the program. So you have to catch it in a signal handler (function that you write). Providing you do that, read(rTOs[0], reply, sizeof("ACK")) will return error code EINTR (interrupted system call).
You need to check the return code from read() in fact you should always check the return codes from any system call. That could be the most important lesson you learn from this assignment.
Next you need to look up how to write a signal handler. Start by looking at signal(2) but note portability issues especially the para starting
POSIX.1 solved the portability mess
I guess you won't get marked down for using this old interface on your first encounter with signal handling. Post back if you need help with that.
Here's something you could do for a top grade: have the signal handler set a global boolean to show it was invoked. Clear this boolean before calling read(). The reason is: you can get EINTR on any signal, but you only want to take action on SIGALRM. (You can get other signals any time, e.g. SIGWINCH when you resize the terminal window).
Avatar of sahithi poreddi
sahithi poreddi

ASKER

Hi Roe,

This is my assignment and I'm very new to programming concepts.

Please do the needful by explaining me the flow so that i can add SIGALARM function to implement TIMEOUT in stop & wait protocol.

FYI headers attached as well.

Thanks
Sahithiassign1h.txtsender_h.txtreceiver_h.txt
We are not allowed to do your homework

<aside>
Well actually, it is permitted now.  The previous policy was that experts were not to do homework for questioners.  Policy was changed, oh, about a year ago I think it was.

Still, it may be worth mentioning that many long-time experts (yours truly included) adhere to the previous policy.  Otherwise nobody involved learns anything -- the expert already knows how and the questioner doesn't learn how.
</aside>
Every C program needs to have a main function where execution starts. You don't have one - I suspect that void receiver() should be int main(int argc, char ** argv) which will fix that problem.
Also you have not defined the makeChecksum() function (or did not post it). Please post the code.
What is the flow: SIGALRM, SIGINT (already in your code) and SIGWINCH (that I mentioned earlier) are all what is known as asynchronous signals. The operating system can deliver them to a process at any time. You can declare a handler for almost any asynchronous signal: this handler will be invoked when the signal is delivered. Absent a handler, there is a default action for each signal as I mentioned earlier. The sigset mechanism is obsolete and deprecated but must be what your Prof told you to use, so stick with it.
You already have a sigset call in your code: just add another one for SIGALRM. The called function can do nothing, or set a global as I described earlier.
Please post back if any of this is unclear.
Also post back if you don't grok the existing sigset call.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.