CS241 System Programming: Discussion Section 7 March 13 - March 16
CS241 System Programming: Discussion Section 7 March 13 - March 16
System Programming
Discussion Section 7
March 13 – March 16
Outline
z UNIX I/O
z Basic Operations
z open, close, read, write
z select, poll
z File Representation
z Serialization
Terminology
z Peripheral device
z Hardware accessed by a computer system
z Device Driver
z OS modules that enable other programs to interact with a
device through system calls
z Write
ssize_t write(int fildes, const void *buf, size_t nbyte);
z Open
int open(const char *path, int oflag, ...);
z Close
int close(int fildes);
Example
z Reading a specific number of bytes (Program 4.7)
ssize_t readblock(int fd, void *buf, size_t size) {
char *bufp;
size_t bytestoread;
ssize_t bytesread;
size_t totalbytes;
z errorfds : exceptions
FD_ZERO(&readset);
FD_SET(fd, &readset);
if (gettimeout(end, &timeout) == -1) return -1;
while (((retval = select(fd + 1, &readset, NULL, NULL, &timeout)) == -1)
&& (errno == EINTR)) {
if (gettimeout(end, &timeout) == -1) return -1;
FD_ZERO(&readset);
FD_SET(fd, &readset);
}
if (retval == 0) {
errno = ETIME;
return -1;
}
if (retval == -1) return -1;
return 0;
}
Poll
#include <poll.h>
z File Pointer
z points to a data structure called a FILE structure in the
user area of the process
z Buffering is used
Buffering
z How does the output appear when the following
program executes? (Exercise 4.26)
#include <stdio.h>
int main(void) {
int i;
fprintf(stdout, "a");
scanf("%d", &i);
fprintf(stderr, "a has been written\n");
fprintf(stdout, "b");
fprintf(stderr, "b has been written\n");
fprintf(stdout, "\n");
return 0;
}
Serialization
z Process of converting an in-memory object into a
linear sequence of bytes
z e.g. converting an object to a byte-string for network
transmission
z Antonym: Unserialization
z Restoring the object from the serialized byte sequence
Example
z Given two data structures:
z Floats: A structure containing three float pointers
z FloatList: A doubly linked list with the element at each
node being a struct Floats pointer
z Tasks to do
z Serialization: store the FloatList onto a char buffer
z Unserialization: restore the FloatList from the char buffer
struct Floats {
float *a;
float *b;
float *c;
};
2 3 1 5 4
Floatlist.c
z Step 1: Traverse the list and figure out the
number of unique struct Floats stored.
2 3 1 5 4
Floatlist.c
z Step 2: Figure out the number of unique
floats stored.
2 1 4 5 5 4 3 1 4
2 3 1 5 4
Floatlist.c
z Step 3: Store the parameters at the first
z # nodes, # unique struct Floats, # unique floats
2 1 4 5 5 4 3 1 4
2 3 1 5 4
4 3 5
Floatlist.c
z Step 4: Store the indices of intermediate
arrays
2 1 4 5 5 4 3 1 4
2 3 1 5 4
4 3 5 0 2 1 2 0 2 4 3 3 4 1 2 4
Floatlist.c
z Step 5: Copy over the floats themselves
2 1 4 5 5 4 3 1 4 5 5 4
2 3 1 5 4
4 3 5 0 2 1 2 0 2 4 3 3 4 1 2 4 2 3 1 5 4
Floatlist.c
z Unserialization
z Reverse process of what has been explained
z Returns s1
Summary
z UNIX I/O
z Basic Operations
z read, write, open, close
z select, poll
z File Representation
z Buffering
z Serialization
z Example in MP3 given files