cs 241 section (12/3/10)

27
CS 241 Section (12/3/10)

Upload: carys

Post on 15-Jan-2016

47 views

Category:

Documents


0 download

DESCRIPTION

CS 241 Section (12/3/10). Announcements. No class on Monday TA review session: Dec. 8, 2010 – in class HW2 due date: Dec. 8, 2010 - 11:00 am No late submissions MP8 due date: Dec. 8, 2010 – 11:59 pm Finals: Dec. 16, 2010 – 8:00 am. In section today. MP8 Overview UNIX File Systems - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS 241 Section (12/3/10)

CS 241 Section(12/3/10)

Page 2: CS 241 Section (12/3/10)

Announcements

• No class on Monday• TA review session: Dec. 8, 2010 – in class

• HW2 due date: Dec. 8, 2010 - 11:00 am• No late submissions

• MP8 due date: Dec. 8, 2010 – 11:59 pm• Finals: Dec. 16, 2010 – 8:00 am

Page 3: CS 241 Section (12/3/10)

In section today

• MP8 Overview• UNIX File Systems

• Directories• Links

Page 4: CS 241 Section (12/3/10)

MP8 Overview

Page 5: CS 241 Section (12/3/10)

What is the MP about?

• Implement a multi-threaded web server

• Web client: browser / wget / telnet

• Server should respond to client requests with local pages (from ./pages/ folder)

Page 6: CS 241 Section (12/3/10)

Running the server

• Run the web server

• The server listens on the port specified• Choose a port between 1024 and 32000

./myserver 10000

Port #

Page 7: CS 241 Section (12/3/10)

Testing the server

• Run the web client• wget

• Browser• Go to address:

• Server name (linux3.ews.illinois.edu) is obtained from the hostname command

wget –d http://linux3.ews.illinois.edu:10000/index.html

http://linux3.ews.illinois.edu:10000/index.html

Page 8: CS 241 Section (12/3/10)

MP Task I - Listen

• Your server should listen to the port# (specified in argv[1]) for incoming connections

• Helper function: newServerSocket(), acceptSocket()

• Your server must handle multiple connections at the same time

• Every incoming connection spawns a new thread to handle the request

Page 9: CS 241 Section (12/3/10)

Task II – Receive HTTP Request

• HTTP request sent by wget

• Function for extracting file name from request• char* getFileNameFromHTTPRequest(void

*vptrRequest, size_t length);

• All files served from web server root directory • ./pages/ directory

GET /index.html HTTP/1.0User-Agent: Wget/1.11.4 Red Hat modifiedAccept: */*Host: linux3.ews.illinois.edu:10000Connection: Keep-Alive

Page 10: CS 241 Section (12/3/10)

Task III – HTTP 1.1 Response

• If the file exists the response starts with:

• Function for building the response• HTTPResponse getResponseString(char *sContentType, void *vptrContent, size_t iContentLength);

HTTP/1.1 200 OKServer: CS/241MIME-version: 1.0Content-type: text/htmlContent-Length: 1697

Page 11: CS 241 Section (12/3/10)

Task III – HTTP 1.1 Response

• If the file does NOT exist the response starts with:

• Function for building the response• HTTPResponse getFileNotFoundResponseString()

HTTP/1.1 404 Not FoundServer: CS/241Connection: closeContent-Length: 300

Page 12: CS 241 Section (12/3/10)

Task III – HTTP 1.1 Response

• If the request is NOT GET the response starts with:

• Function for building the response• HTTPResponse getNotImplementedResponseString()

HTTP/1.1 501 Not ImplementedServer: CS/241Connection: closeContent-Length: 300

Page 13: CS 241 Section (12/3/10)

Task III – HTTP 1.1 Response

• If the browser request for /• The web server should list all the files in

the root directory (./pages/ directory)• ONLY the files

• Function for building an HTML page with list of files

• char *getHTMLOfDirectoryList(char **fileNames);

• Build Response with getResponseString()

Page 14: CS 241 Section (12/3/10)

Last Task – Sending the Response

• We have a HTTP response ready in a HTTPResponse struct:

• Use send() to send the vptrResponse back• free vptrResponse after send()

typedef struct __HTTPResponse { void *vptrResponse; size_t length;} HTTPResponse;

Page 15: CS 241 Section (12/3/10)

Reading a Directory

Page 16: CS 241 Section (12/3/10)

Directory reading functions#include <dirent.h>

Open the directoryDIR *opendir(const char *dirname);

Close the directoryint closedir(DIR *dirp);

Read the directorystruct dirent *readdir(DIR *dirp);

Page 17: CS 241 Section (12/3/10)

What’s in a directory entry?struct dirent

Member Fieldschar d_name[256]

Null-terminated file nameino_t d_ino

inode numberunsigned char d_reclen

Length of this recordunsigned char d_type

Type of file (DT_REG, DT_DIR, DT_FIFO, DT_SOCK, DT_CHR, DT_BLK, DT_UNKNOWN)

Page 18: CS 241 Section (12/3/10)

Example• Use opendir and readdir to print all the filenames in

the current directory:

#include <dirent.h>…DIR *dir;struct dirent *entry;

dir = opendir(“.”);

while(entry = readdir(dir)){printf(“%s\n”,entry->d_name);}

closedir(dir);

• Remember to include error checking!!

Page 19: CS 241 Section (12/3/10)

Links

Page 20: CS 241 Section (12/3/10)

Links

• Hard Link• Directory Entry e.g. all regular files

• Symbolic Link (Soft Link)

• Special file, serves as a reference to another file

#include <unistd.h>

int link(const char *oldpath, const char *newpath);int unlink(const char *path);

int symlink(const char *oldpath, const char *newpath);

Page 21: CS 241 Section (12/3/10)

Hard Link ExampleCommand Lineln /dirA/name1 /dirB/name2

C Code Segmentsif (link("/dirA/name1", "/dirB/name2") == -1) perror("Failed to make a new link in /dirB");

Page 22: CS 241 Section (12/3/10)

Hard Link Example (contd)

Q: What happens if /dirA/name1 is deleted and recreated?

Page 23: CS 241 Section (12/3/10)

Hard Link Example (contd)

A: /dirA/name1 and /dirB/name2 are now two distinct files.

Page 24: CS 241 Section (12/3/10)

Soft Link ExampleCommand Lineln –s /dirA/name1 /dirB/name2

C Code Segmentsif (symlink("/dirA/name1", "/dirB/name2") == -1) perror("Failed to create a symbolic link in /dirB");

Page 25: CS 241 Section (12/3/10)

Soft Link Example (contd)

Q: What happens if /dirA/name1 to is deleted and recreated?

Page 26: CS 241 Section (12/3/10)

Soft Link Example (contd)

A: /dirA/name1 has a different inode, but /dir/name2 still links to it.

Page 27: CS 241 Section (12/3/10)

Link number• The link number (the st_nlink field in stat)

tells how many directory entries link to this inode. The link number is:• Set to 1 when a file is created• Incremented when link is called• Decremented when unlink is called

• The link number appears in the second column of the output of ls –l. Try it!

• The link number only counts hard links, not soft links.