Question
Need help with c programming code updated below a. If no integer has ever been written to the active slot in the device since the
Need help with c programming code updated below
a. If no integer has ever been written to the active slot in the device since the device was activated, the read() call will block until an integer is written into the slot. b. If the slot was cleared (HIOCCLEARSLOT) and was not subsequently written to, make the read( ) call block until an integer is written into the slot. c. The process blocked in the read( ) call should wake up when some process writes to the slot, and should then return the data in the slot. You should also handle the case where multiple processes are blocking on a read from the same slot (all should wake up and get the data).
d. Hints: i. If a function in the driver returns ENOREPLY, then there will be no message in response to the message sent to the driver. This will cause the process calling this function to block. ii. To resume a process, you need to send a reply message to the original message. Fortunately, there is a function in the chardriver library called chardriver_reply_task that can do just that.
source:
#include
static int homework_open(devminor_t minor, int access, endpoint_t user_endpt); static int homework_close(devminor_t minor); static ssize_t homework_read(devminor_t minor, u64_t position, endpoint_t endpt, cp_grant_id_t grant, size_t size, int flags, cdev_id_t, id); static ssize_t homework_write(devminor_t minor, u64_t position, endpoint_t endpt, cp_grant_id_t grant, size_t size, int flags, cdev_id_t, id); static ssize_t homework_ioctl(devminor_t UNUSED(minor), unsigned long request, endpoint_t endpt, cp_grant_id_t grant, int UNUSED(flags), endpoint_t user_endpt, cdev_id_t, UNSED(id));
/*SEF functions and variables. */ static void sef_local_startup(void); static int self_cb_init(int type, self_init_info_t *info); static int self_cb_lu_state_save(int); static int lu_state_restore(void);
/* Entry points to the driver. */ static struct chardriver homework_tab = { .cdr_open = homework_open, .cdr_close = homework_close, .cdr_read = homework_read, .cdr_write = homework_write, .cdr_ioctl = homework_ioctl };
/* state variable to count the number of times the device has been opened. * Note that this is not the regular type of open counter: it never decreases. */ static int open_counter; static int currentSlot = -1; static int slotData[5] = {-1, -1, -1, -1, -1};
static int checkSlot(int slotNum) { int result = 0; if ((slotNum < 0) || (slotNum > 4)) { result = -1; } if (currentSlot == -1) { result = -1; } return result; }
static int homework_open(devminor_t UNUSED(minor), int UNUSED(access), endpoint_t UNUSED(user_endpt)) { printf("homework_open(). Called %d times(s). ", ++open_counter); return OK; }
static int homework_close(devminor_t UNUSED(minor)) { printf("homework_close() "); return OK; }
static ssize_t homework_write(devminor_t UNUSED(minor), u64_t position, endpoint_t endpt, cp_grant_id_t grant, size_t size, int UNUSED(flags), cdev_id_t UNUSED(id)) { printf("homework_write() "); if (checkSlot(currentSlot) == -1) { printf("Invalid slot number or no valid slot. Current slot: %d ", currentSlot); return -1; } if((sys_safecopyfrom(endpt, grant, 0, (vir_bytes) &slotData[currentSlot], size)) !=OK) { return -1; } printf("Wrote %d to slot %d ", slotData[currentSlot], currentSlot); return size; }
static ssize_t homework_read(devminor_t UNUSED(minor), u64_t position, endpoint_t endpt, cp_grant_id_t grant, size_t size, int UNUSED(flags), cdev_id_t UNUSED(id)) { int ret; printf("homework_read() "); if(checkSlot(currentSlot) == -1) { return -1; } if ((size > sizeof(slotData[currentSlot]))) { printf("Invalid read size: %d ", (int) size); return -1; } /* Copy the requested part to the caller. */ if ((ret = sys_safecopyto(endpt, grant, 0, (vir_bytes) &slotData[currentSlot], size)) != OK) { return ret; } return size; }
static ssize_t homework_ioctl(devminor_t UNUSED(minor), unsigned long request, endpoint_t endpt, cp_grand_id_t grant, int UNUSED(flags), endpoint_t user_endpoint, cdev_id_t UNUSED(id)) { int r; r = OK; switch(request) { int requestSlot; case HIOCSLOT: r = sys_safecopyfrom(endpt, grant, 0, (vir_bytes) & requestSlot, sizeof(requestSlot))); if ( r != OK) { printf("HIOSLOT request failed. Return %d ", r); return -1; } if ((requestSlot < 0) || (requestSlot > 4)) { printf("Only between 0 and 4 should be accepted"); return -1; } currentSlot = requestSlot; printf("Slot now is %d ", currentSlot); break; case HIOCCLEARSLOT: currentSlot = -1; printf("Slot is cleared "); break; case HIOCGETSLOT: printf("In HIOCGETSLOT "); r = sys_safecopyto(endpt, grant, 0, (vir_bytes) ¤tSlot, sizeof(currentSlot)); break; default: printf("Invalid command: %ld ", request); return -1; } return r; }
static int sef_cb_lu_state_save(int UNUSED(state)){ /* Save the state. */ ds_publish_u32("open_counter", open_counter, DSF_OVERWRITE); return OK; }
static int lu_state_restore(){ /*Restore the state. */ u32_t value; ds_retrieve_u32("open_counter", & value); ds_delete_u32("open_counter"); return OK; }
static void sef_local_startup() { /* * Register init callbacks. Use the same function for all event types */ sef_setcb_init_fresh(sef_cb_init); sef_setcb_init_lu(sef_cb_init); sef_setcb_init_restart(sef_cb_init);
/* * Register live update callbacks. */ /* - Agree to update immediately when LU is requested in a valid state. */ sef_setcb_lu_prepare(sef_cb_lu_prepare_always_ready); /* - Support live update starting from any standard state. */ sef_setcb_lu_state_isvalid(sef_setcb_lu_state_isvalid_standard); /* - Register a custom routine to save the state. */ sef_setcb_lu_state_save(sef_cb_lu_state_save);
/* - Let SEF perform startup. */ sef_startup(); }
static int sef_cb_init(int type, sef_init_info_t *UNUSED(info)) { /* Initialize the driver. */ int do_announce_driver = TRUE; open_count = 0; switch(type){ case SEF_INIT_FRESH: printf("%s", HELLO_MASSAGE); break; case SEF_INIT_LU: /* Restore the state. */ lu_state_restore(); do_announce_river = FALSE; printf("%sHey, I'm a new version! ", HELLO_MESSAGE); break; case SEF_INIT_RESTART: printf("%sHey, I've ust been restarted! ", HELLO_MESSAGE); break;
/* Announce we are up when nessary. */ if (do_announce_driver){ chardriver_announce(); } /* initialization completed successfully. */ return OK; }
int main(void) { /* * Perform initialization */ sef_local_startup();
/* * Run the main loop. */ chardriver_task(&homework_tab); return OK; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started