Advertisement
ananthm3

Unassuming

Jan 8th, 2025
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. #define _GNU_SOURCE
  2.  
  3. #include <sys/mman.h>
  4.  
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <stdint.h>
  8. #include <pthread.h>
  9. #include <stdatomic.h>
  10. #include <fcntl.h>
  11. #include <signal.h>
  12. #include <unistd.h>
  13. #include <string.h>
  14.  
  15.  
  16. #define PIPE_SIZE (256)
  17.  
  18. #define max(a,b) \
  19. ({ __typeof__ (a) _a = (a); \
  20. __typeof__ (b) _b = (b); \
  21. _a > _b ? _a : _b; })
  22. #define min(a,b) \
  23. ({ __typeof__ (a) _a = (a); \
  24. __typeof__ (b) _b = (b); \
  25. _a < _b ? _a : _b; })
  26.  
  27.  
  28. // Task: Implement userspace pipes
  29. // The functions underneath contain the basic scaffold for what the pipes should support.
  30. // Assume tedious parts of the logic can be abstracted away via a function (i.e. buffer logic)
  31. //
  32. // NOTE: Consider forks!
  33. //
  34. // FOLLOW UP:
  35. // Consider processes that don't close.
  36. // How would you still ensure things get cleaned up for the most part?
  37.  
  38. typedef struct {
  39. // TODO: implement me!
  40. } user_pipe;
  41.  
  42. enum {
  43. READ_END,
  44. WRITE_END
  45. };
  46.  
  47. // TODO: allocate and initialize a pipe for IPC
  48. user_pipe *open_pipe();
  49.  
  50. // TODO:
  51. // Should close a particular end of a pipe (READ or WRITE)
  52. // If all ends of the pipe are fully closed, the pipe should be deallocated.
  53. void close_pipe(user_pipe *p, int end);
  54.  
  55. // TODO:
  56. // Read data from a pipe into a buffer
  57. // The read should be atomic *up* to the amount of data in the pipe.
  58. // NOTE: consider edge cases...
  59. ssize_t read_pipe(user_pipe *p, char *buf, size_t len);
  60.  
  61. // TODO:
  62. // Write data from a pipe
  63. // The write should be atomic *up* until the pipe fills up.
  64. // NOTE: consider edge cases...
  65. ssize_t write_pipe(user_pipe *p, char *buf, size_t len);
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement