DOC PREVIEW
U of I CS 241 - System Programming

This preview shows page 1-2-3-25-26-27 out of 27 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 27 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 27 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 27 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 27 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 27 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 27 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 27 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS241 System ProgrammingDiscussion Section 7March 13 – March 16Outlinez UNIX I/Oz Basic Operationsz open, close, read, writez select, pollz File Representationz SerializationTerminologyz Peripheral devicez Hardware accessed by a computer systemz Device Driverz OS modules that enable other programs to interact with a device through system callsz UNIX provides uniform access to most devicesz 5 functions: open, close, read, write, ioctlReview from classz Readssize_t read(int fildes, void *buf, size_t nbyte);zWritessize_t write(int fildes, const void *buf, size_t nbyte);zOpenint open(const char *path, int oflag, ...);zCloseint close(int fildes);Examplez 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;}Examplez A program to copy a file (Program 4.9)#define READ_FLAGS O_RDONLY#define WRITE_FLAGS (O_WRONLY | O_CREAT | O_EXCL)#define WRITE_PERMS (S_IRUSR | S_IWUSR)int main(int argc, char *argv[]) {if ((fromfd = open(argv[1], READ_FLAGS)) == -1) {perror("Failed to open input file");return 1; }if ((tofd = open(argv[2], WRITE_FLAGS, WRITE_PERMS)) == -1) {perror("Failed to create output file");return 1;}bytes = copyfile(fromfd, tofd);printf("%d bytes copied from %s to %s\n", bytes, argv[1], argv[2]); return 0; /* the return closes the files */ }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 single process: for 3 conditionsz readfds : whether read will not blockz writefds : whether write will not blockz errorfds : exceptionsz Returns the number of descriptors contained in the descriptor sets if successful. -1 with errno set if unsuccessfulSelect – 4 Macrosz Setting the corresponding bitvoid FD_SET(int fd, fd_set *fdset);zClearing the corresponding bitvoid FD_CLR(int fd, fd_set *fdset);zClearing all bitsvoid FD_ZERO(fd_set *fdset);zTesting whether the corresponding bit is setint FD_ISSET(int fd, fd_set *fdset);Examplez Monitoring File Descriptors (Program 4.14)while (numnow > 0) { /* continue monitoring until all are done */FD_ZERO(&readset); /* set up the file descriptor mask */for (i = 0; i < numfds; i++)if (fd[i] >= 0) FD_SET(fd[i], &readset);numready = select(maxfd, &readset, NULL, NULL, NULL); /* which ready? *//* Error checking skipped */for (i = 0; (i < numfds) && (numready > 0); i++) { /* read and process */if (fd[i] == -1) /* this descriptor is done */continue;if (FD_ISSET(fd[i], &readset)) { /* this descriptor is ready */bytesread = r_read(fd[i], buf, BUFSIZE);numready--;if (bytesread > 0)docommand(buf, bytesread);else { /* error occurred on this descriptor, close it */r_close(fd[i]);fd[i] = -1;numnow--;}}}}Examplez Program 4.15int 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 descriptorsz Organizes the information by file descriptor: struct pollfdz Select: by the type of eventz Returns the number of descriptors that have events if successful. 0 if timeout, or -1 with errno set if unsuccessfulPollz struct pollfdz File descriptor: int fd;z Requested Events: short events;z Returned Events: short revents;z Event Flags: Table 4.2Examplez 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 dataelsebytesread = -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--;}}}}File Representationz File Descriptorz Represents a file or device that is openz An int-type index into the file descriptor table of each processz Can refer to files, directories, blocks, sockets, pipesz File Pointerz points to a data structure called a FILE structure in the user area of the processz Buffering is usedBufferingz 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;}Serializationz Process of converting an in-memory object into a linear sequence of bytesz e.g. converting an object to a byte-string for network transmissionz Involved in saving an object onto a storage medium (file, memory buffer, etc.)z Antonym: Unserializationz Restoring the object from the serialized byte sequenceExamplez Given two data structures: z Floats: A structure containing three float pointersz FloatList: A doubly linked list with the element at


View Full Document

U of I CS 241 - System Programming

Documents in this Course
Process

Process

28 pages

Files

Files

37 pages

File I/O

File I/O

52 pages

C Basics

C Basics

69 pages

Memory

Memory

23 pages

Threads

Threads

14 pages

Lecture

Lecture

55 pages

C Basics

C Basics

24 pages

Signals

Signals

27 pages

Memory

Memory

45 pages

Threads

Threads

47 pages

Threads

Threads

28 pages

LECTURE

LECTURE

45 pages

Threads

Threads

30 pages

Threads

Threads

55 pages

Files

Files

37 pages

SIGNALS

SIGNALS

22 pages

Files

Files

37 pages

Threads

Threads

14 pages

Threads

Threads

13 pages

Load more
Download System Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view System Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view System Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?