



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




















z^ File Representation z Serialization
;
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; }
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
; 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.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
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;
{
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--; } } } }
<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; }
3 1
5 4 2 3 1
5 4