Docsity

UNIX I/O: Basic Operations and File Representation in CS241 System Programming, Study notes of Computer Science

Partial preview of the text

Download UNIX I/O: Basic Operations and File Representation in CS241 System Programming and more Study notes Computer Science in PDF only on Docsity!

CS

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

Review from class z^ Read ssize_t read(int fildes, void *buf, size_t nbyte); 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;^ for (bufp = buf, bytestoread = size, totalbytes = 0; bytestoread > 0;^ bufp += bytesread, bytestoread -= bytesread) {^ bytesread = read(fd, bufp, bytestoread)

;

if ((bytesread == 0) && (totalbytes == 0))^ return 0; if (bytesread == 0) {^ errno = EINVAL;^ return -1; } if ((bytesread) == -1 && (

errno != EINTR

))

return -1; if (bytesread == -1) bytesread = 0; totalbytes += bytesread; } return totalbytes; }

Select #include <sys/select.h> int select(int nfds, fd_set *restrict readfds,

fd_set *restrict writefds, fd_set *restrict errorfds, struct timeval *restrict timeout);

z^ Provides a method of monitoring file descriptors from a singleprocess: for 3 conditions^ z^ readfds

: whether read will not block z^ writefds

: whether write will not block z^ errorfds

: exceptions z^ Returns the number of descriptors contained in the descriptorsets if successful. -1 with

errno^ set if unsuccessful

Select โ€“ 4 Macros z^ Setting the corresponding bit^ void FD_SET(int fd, fd_set *fdset); z^ Clearing the corresponding bit^ void FD_CLR(int fd, fd_set *fdset); z^ Clearing all bits^ void FD_ZERO(fd_set *fdset); z^ Testing whether the corresponding bit is set^ int FD_ISSET(int fd, fd_set *fdset);

Example z^ Program 4.15 int waitfdtimed(int fd, struct timeval end) {^ fd_set readset;^ int retval;^ struct timeval timeout;^ 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> int^ poll(struct

pollfd^

fds[],^ nfds_t

nfds,^ int

timeout);

z^ Provides a method of monitoring file descriptors z^ Organizes the information by file descriptor:

struct

pollfd

z^ Select

: by the type of event z^ Returns the number of descriptors that have events if successful.0 if timeout, or -1 with errno set if unsuccessful

Example z^ Monitoring an array of file descriptors (Program 4.17)^ if ((pollfd = (void *)calloc(numfds, sizeof(struct

pollfd))) == NULL)

return; for (i = 0; i < numfds; i++) { (pollfd + i)->fd = (fd + i); (pollfd + i)->events = POLLRDNORM; } / Continue monitoring until descriptors done */ while (numnow > 0) { numready = poll(pollfd, numfds, -1)

;

if ((numready == -1) && (errno == EINTR))^ continue;

/* poll interrupted by a signal, try again / else if (numready == -1)/ real poll error, can't continue */^ break;

Example (continued) z^ Monitoring an array of file descriptors (Program 4.17)^ for (i = 0; i < numfds && numready > 0; i++)

{

if ((pollfd + i)->revents) {^ if ( (pollfd + i)->revents & (POLLRDNORM | POLLIN)

) {

bytesread = r_read(fd[i], buf, BUFSIZE); numready--; if (bytesread > 0)^ docommand(buf, bytesread);

// some command to call on the data else^ bytesread = -1;

/* end of file */

} else if ((pollfd + i)->revents & (POLLERR | POLLHUP))^ bytesread = -1; else^

/* descriptor not involved in this round */ bytesread = 0; if (bytesread == -1) {

/* error occurred, remove descriptor */ r_close(fd[i]); (pollfd + i)->fd = -1; numnow--; } } } }

Buffering z^ How does the output appear when the followingprogram 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 alinear sequence of bytes^ z^ e.g. converting an object to a byte-string for networktransmission z^ Involved in saving an object onto a storage medium(file, memory buffer, etc.) z^ Antonym: Unserialization^ z^ Restoring the object from the serialized byte sequence

Floatlist.c z^ An illustration of given data structures

struct FloatList {struct Floats *floats;struct FloatList *next;struct FloatList *last;}; struct Floats {float *a;float *b;float *c;};

Floatlist.c z^ Step 1: Traverse the list and figure out thenumber of unique struct Floats stored.^2

3 1

5 4 2 3 1

5 4

0x000A 0x3A
0x3A540x