Final
Final
Question Tips – add put files in directory under version control. C Standard Library
IP headers have a packet length 216 bytes incl. header, pro-
Added to repository in next commit tocol and TTL (reduced each time the packet reaches an inter-
– remove remove files and directories from VC. Sched- face, packet is dropped if TTL reaches 0).
• Unused addr: 2host bits − 2 − number of addresses. uled for deletion upon next commit and removed ping sends a message to a device, sends a copy back and #include <stdio.h>
• Thread diagrams are always valid. from working copy. calculates the travel time.
int printf (const char *format, ...);
• socket, bind, listen, accept – move move and/or name something in working copy IPv4 structure has 32 bits, divided into network and host parts
or repository int fprintf (FILE *stream, const char *format
• socket, connect eg
• LOOK AT PRE AND POST INCREMENT!!!! – update bring changes from repository into working ,→ , ...);
• TRANSLATION LOOKASIDE BUFFER copy int sprint (char *str, const char *format,
• TCP: send message back always. – info display information about a local or remote item 130.102 | 72.9 ,→ ...);
• TLB: Memory cache for pages and page to frame refer- – log show log messages for a set revision(s) and/or 10000010.01100100 | 01001000.00001001 int snprintf(char *str, size_t size, const
ences. path(s) ,→ char
• One level: if in the TLB 0, otherwise 1 page read – status print the status of working copy and files and *format, ...);
Increasing the number of bits in the network parts means a
• Two level: if in the TLB 0, otherwise, 2 pages read directories (returns -1 on error)
“smaller” network. When sending a message, the network layer
• Number of memory access: 1 access if in the TLB, 3 if – diff display differences between two revisions or int scanf(const char *format, ...);
needs to make a decision:
two-level,2 if one-level paths
• chmod change file modes (permissions) 1. Send direct to the destination (find the MAC of the desti- int fscanf(FILE *stream, const char *format,
nation) ,→ ...);
– u/g/o/a user/group/others/all
– +/- add remove 2. Send via another mahcine (find the MAC of the intermedi- int sscanf(const char *str, const char *
– r/w/x read/write/execute ary) ,→ format, ...);
Threading
• gcc create executable We have four main addresses: addresses)
– -o cretaes an executable file from a c file, the or- • Application specific addresses NAT Host X wants to connect with address G (sends {src-
der needs to be: gcc -o <executable-name> • Port (differentiate between processes) ip=X, src-port=sp, dest-ip=G, dest-port=80}). Packet arrives
<name-of-file> • IP (which computer is this process on) at G. G tries to reply with {src-ip=G, src-port=80, dest-ip=X,
– -c compiles a c file and creates an .o file of the same • MAC (which device is this direct message to) dest-port=sp} but reply doesn’t go anywhere because nobody #include <pthread.h> (gcc -pthread)
name IP addresses and MAC addresses technically don’t identify de- knows where X is.
int pthread_create(pthread_t *thread, const
– -g include debugging symbols vices. The layer isolation is not completely enforced. NAT is network address translation. pthread_attr_t *attr,
• ls list directory contents What is the transport layer for? The network layer deals with 1. X → . . . → R → . . . → G void *(*start_routine) (void *), void *
– -l use a long listing format packets. We’d like streams of bytes and reliable delivery. TCP {src-ip=X, src-port=sp, dest-ip=G, dest-port=80}
– -a shows all hidden directories, do not ignore entries ,→ arg);
is good for this. 2. Packet arrives at R
starting with . int pthread_join(pthread_t thread, void **
• Establish connection 3. R modifies address information
– -d list directories themselves not their content {src-ip=R, src-port=np, dest-ip=G, dest-port=80} ,→ retval);
• Making a connection requires messages to travel there
– -i print the index number of each file and back 4. ... int pthread_mutex_init(pthread_mutex_t *
• ps process status • Connections are bi-directional 5. G receives packet and replies ,→ restrict
– -e show all processes (-> to see every process on UDP deals with discrete messages and no guarantees of de- {src-ip=G, src-port=80, dest-ip=R, dest-port=np} mutex, const pthread_mutexattr_t *
the system using standard syntax) livery or acknowledgment. So streaming video, games, con- 6. ... ,→ restrict attr);
– -u user’s processes, if wishing to specify a user, gested networks (many small operations) UDP is appropriate 7. R receives packet and modifies info int pthread_mutex_destroy(pthread_mutex_t *
specify -u for. {src-ip=G, src-port=80, dest-ip=X, dest-port=sp} ,→ mutex);
– -f do full format listing, adds additional columns 8. X receives the message
• sort write sorted concatenation to stdout
Networking
Bandwidth ̸= Latency NAT only works because R remembers that port np corre-
– -r reverse result of comparisons sponds to port sp on X . R does not need to be directly con-
– -k sort via key nected to X or G.
• uniq report or omit repeated lines Where do ports come from?
• Ports: you choose DNS IP packets need to use IP addresses. Map names to IP
– -c count number of occurrences addresses. Hosts file is still used but useless.
• cat concatenate files to print on stdout • IP: internal (admin chooses) or external (DHCP) #include <sys/types.h>
• MAC: default address, can be changed DNS is domain name service. Each “domain” will have at #include <sys/socket.h>
• head output first part of file to stdout (def. 10)
IPv4 has 32 bit addresses. IPv6 is 128bit. We use IPv4. Server least two servers which know the name to address mapping
– -n output first N number of lines (with leading - print #include <netdb.h>
a process which waits for requests from clients. Client a pro- for that domain. Have a collection of root nameservers. The
all but last N)
cess which submits requests to a server. root servers know the information for the nameservers for the int socket(int domain, int type, int protocol
– -c print first N bytes of each file (leading - as above) ,→ );
TCP: there is always a client and a server. There is no differ- TLDs. Those servers each know the nameservers for subdo-
– -q quiet, never print headers giving file name int connect(int sockfd, const struct sockaddr
ence between what a client can do and what a server can do. mains. DNS is essentially a distributed phonebook.
• tail output last part of file to stdout (def. 10) ,→ *addr,
Connections are bi-directional. Queries are UDP messages. Servers can operate iteratively or
– -n output last N number of lines socklen_t addrlen);
Client steps: recursively. DNS responses have TTL (more stable mappings
• cut remove sections from each line of flines
1. Find out the address of the machine you wish to connect will have longer TTL). Load balancing: different requests for the int bind(int sockfd, const struct sockaddr *
– -f select only these fields; print any line that contains ,→ addr,
to same name could get different answers. Give answers which
no delimiter character, unless -s is specified. Can socklen_t addrlen);
2. Make a socket (fd) are close to the query source. DNS domains are independent
specify multiple fields with a comma (e.g. -f1,3,4 ).
3. connect() to the server of networks. int listen(int sockfd, int backlog);
Can specify ranges with dashes. N- Nth onwards,
-M up to M, N-M n to m 4. Wrap socket descriptor for nicer IO (dup() before calling HTTP is HyperText transfer protocol. Runs on top of TCP (so int accept(int sockfd, struct sockaddr *addr,
– -s do not print lines not containing delimiters fdopen()) layer 5). We send: socklen_t *addrlen);
– -d specify delimiter (e.g. -d’:’) Server steps: • GET / (i want a page, this is the name)
• wc print newline, word and byte counts for each file 1. Make a socket • HTTP/1.1 (the protocol we are using) int getaddrinfo(const char *node, const char
– -l number of newlines 2. (Optional) set parameters • A newline ,→ *service,
– -c byte count 3. bind() the socket to a port We receive: const struct addrinfo *hints,
– -m character count 4. Set the socket to listen() for connections • HTTP/1.1 400 Bad Request (response is in this pro- struct addrinfo **res);
• diff compare files line by line 5. Call accept() to allow a connection (use the new fd to tocol, status code, readable version of the status) void freeaddrinfo(struct addrinfo *res);
– -q report only when files differ interact with the client) • Headers (Content-Type: text/html (what sort of file),
– -s report identical files ntohs converts a 16 bit value from network representation to Content-Length: 173 (how big is the file))
• svn subversion
– commit send changes from working copy to reposi-
the machines normal ordering.
Note that accept() is a blocking call, so fork or create a
• Blank line
• HTML content telling us it was a bad request
Misc
tory pthread or use a non-blocking call but don’t actually tho. Note ACK means acknowledgment, so go BACK!!!!
void qsort(void *base, size_t nmemb, size_t result[position++] = (char)next; 2018 Question 11 pthread_t ids[cols];
,→ size, } for (int i = 0; i < rows; i++) {
int (*compar)(const void *, const if (position > 30) { int* matrixRow = matrix[i];
,→ void *)); result = realloc(result, sizeof( ThreadArgs threadArgs;
typedef struct { threadArgs.matrixRow = matrixRow;
,→ char)*position+1);
int* matrixRow; threadArgs.numCols = cols;
}
Read line example }
}
int numCols;
} ThreadArgs;
pthread_create(&ids[i], rowsum, &
,→ threadArgs);
}
void* rowsum(void* v) { for (int i = 0; i < rows; i++) {
char* read_line(FILE* file) {
char* result = malloc(sizeof(char)*40);
Equations ThreadArgs* threadArgs = (ThreadArgs*)v;
int rowSum = 0;
void* val;
pthread_join(ids[i], &val);
int position = 0; for (int i = 0; i < threadArgs->numCols; sum += (int)val;
int next = 0; ⌊ ⌋ ,→ i++) { }
Address rowSum += threadArgs->matrixRow[i]; return sum;
Block Number =
while (1) { Block Size } }
next = fgetc(file); return (void*)rowSum;
Offset = Address % Block Size }
if (next == EOF || next == '\n') {
result[position] = '\0'; Block Size
result = reverse(result); No. of Block Pointers = int matrixsum(int** matrix, int rows, int
return result; Block Pointer Size ,→ cols) {
} else { Subdirectories = Link Count − 2 int sum = 0;