Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _GNU_SOURCE
- #include <sys/mman.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <stdint.h>
- #include <pthread.h>
- #include <stdatomic.h>
- #include <fcntl.h>
- #include <signal.h>
- #include <unistd.h>
- #include <string.h>
- #define PIPE_SIZE (256)
- #define max(a,b) \
- ({ __typeof__ (a) _a = (a); \
- __typeof__ (b) _b = (b); \
- _a > _b ? _a : _b; })
- #define min(a,b) \
- ({ __typeof__ (a) _a = (a); \
- __typeof__ (b) _b = (b); \
- _a < _b ? _a : _b; })
- // Task: Implement userspace pipes
- // The functions underneath contain the basic scaffold for what the pipes should support.
- // Assume tedious parts of the logic can be abstracted away via a function (i.e. buffer logic)
- //
- // NOTE: Consider forks!
- //
- // FOLLOW UP:
- // Consider processes that don't close.
- // How would you still ensure things get cleaned up for the most part?
- typedef struct {
- // TODO: implement me!
- } user_pipe;
- enum {
- READ_END,
- WRITE_END
- };
- // TODO: allocate and initialize a pipe for IPC
- user_pipe *open_pipe();
- // TODO:
- // Should close a particular end of a pipe (READ or WRITE)
- // If all ends of the pipe are fully closed, the pipe should be deallocated.
- void close_pipe(user_pipe *p, int end);
- // TODO:
- // Read data from a pipe into a buffer
- // The read should be atomic *up* to the amount of data in the pipe.
- // NOTE: consider edge cases...
- ssize_t read_pipe(user_pipe *p, char *buf, size_t len);
- // TODO:
- // Write data from a pipe
- // The write should be atomic *up* until the pipe fills up.
- // NOTE: consider edge cases...
- ssize_t write_pipe(user_pipe *p, char *buf, size_t len);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement