0% found this document useful (0 votes)
92 views

CS241 System Programming: Discussion Section 7 March 13 - March 16

This document discusses serialization and deserialization of data structures in C. It provides an example of serializing a doubly linked list of floating point data (FloatList) to a character buffer. The steps are: 1) Count the number of nodes and unique structs in the list. 2) Count the unique floats. 3) Store metadata like counts. 4) Store indices of floats in intermediate arrays. 5) Copy over the floats. This allows reconstructing the original data structure from the serialized character buffer.

Uploaded by

Gaurav
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views

CS241 System Programming: Discussion Section 7 March 13 - March 16

This document discusses serialization and deserialization of data structures in C. It provides an example of serializing a doubly linked list of floating point data (FloatList) to a character buffer. The steps are: 1) Count the number of nodes and unique structs in the list. 2) Count the unique floats. 3) Store metadata like counts. 4) Store indices of floats in intermediate arrays. 5) Copy over the floats. This allows reconstructing the original data structure from the serialized character buffer.

Uploaded by

Gaurav
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

CS241

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 UNIX provides uniform access to most devices


z 5 functions: open, close, read, write, ioctl
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;
}
Example
z 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 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 descriptor


sets 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 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--;
}
}
}
}
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
Poll
z struct pollfd
z File descriptor: int fd;
z Requested Events: short events;
z Returned Events: short revents;

z Event Flags: Table 4.2


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--;
}
}
}
}
File Representation
z File Descriptor
z Represents a file or device that is open
z An int-type index into the file descriptor table of each
process
z Can refer to files, directories, blocks, sockets, pipes

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 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
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

z Answer: Look at floatlist.{c,h} (given in MP3) and


analyze them.
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;
};
2 3 1 5 4
Floatlist.c
z Step 1: Traverse the list and figure out the
number of unique struct Floats stored.

0x000A 0x3A54 0x1262 0x3A54

2 3 1 5 4
Floatlist.c
z Step 2: Figure out the number of unique
floats stored.

0x000A 0x3A54 0x1262 0x3A54

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

0x000A 0x3A54 0x1262 0x3A54

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

0x000A 0x3A54 0x1262 0x3A54

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

0x000A 0x3A54 0x1262 0x3A54

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 Make sure you understand the function


memcpy
#include <string.h>

void *memcpy(void *s1, const void *s2, size_t n);

z Copies n bytes from memory area s2 to s1


z Similar to strcpy, but not affected by NULL
z Should not be used if the memory areas overlap

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy