Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <sys/errno.h>
  5. #include <assert.h>
  6. #include "uthread.h"
  7. #include "disk.h"
  8.  
  9. unsigned int sum = 0;
  10. volatile int is_read_pending; // flag that indicates whether a read is pending
  11. // set before requesting disk_read
  12. // cleared by interrupt service routine
  13.  
  14. /**
  15. * Called by disk subsystem each time that a read completes.
  16. * You may assume that reads complete in request order.
  17. */
  18. void interrupt_service_routine() {
  19. is_read_pending = 0; // clear flag to indicate that read is no longer pending
  20. }
  21.  
  22. int main (int argc, char** argv) {
  23.  
  24. // Read Command Line Arguments
  25. static char* usage = "usage: sRead num_blocks";
  26. int num_blocks;
  27. char *endptr;
  28. if (argc == 2)
  29. num_blocks = strtol (argv [1], &endptr, 10);
  30. if (argc != 2 || *endptr != 0) {
  31. printf ("argument error - %s \n", usage);
  32. return EXIT_FAILURE;
  33. }
  34.  
  35. // Initialize
  36. uthread_init (1);
  37. disk_start (interrupt_service_routine);
  38.  
  39. // Sum Blocks
  40. for (int blockno = 0; blockno < num_blocks; blockno++) {
  41. int result;
  42. is_read_pending = 1; // set flag saying that read will be pending
  43. disk_schedule_read (&result, blockno); // request disk to read specified blockno
  44. while (is_read_pending); // loop until is_read_pending == 0
  45. sum += result;
  46. }
  47. printf ("%d\n", sum);
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement