CS360 Midterm Exam: March 2, 2004. Question 1

Behold the following system call:
int send_to_card(char *ptr, int nbytes);
This sends the bytes specified by ptr and nbytes to a special card on your computer. Nbytes must be less than 4096. It returns 0 on success, and -1 on failure.

Your job is to write:

int bsend_to_card(char *ptr, int nbytes);
bsend_to_card() gives you two features:
  1. It performs buffering, so that calls where nybtes is small will typically not be penalized with system call overhead.
  2. It allows nbytes to be greater than 4096 bytes.
bsend_to_card() will work as follows. It will manage a 4096-byte buffer in the following way: As an example, suppose the buffer holds 1000 bytes, and bsend_to_card(0x10000, 8192) is called. bsend_to_card() will do the following things: If send_to_card() ever returns -1, bsend_to_card() should instantly return -1. Otherwise, bsend_to_card() should return 0 when it is done.

You will need global variables for this. I am including an answer sheet for this problem that includes the proper declarations for global variables. You may add more if you want, but you do not have to.