You are on page 1of 2

CSSE2310 Computer Systems Principles and Programming

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, ...);

Unix – -c only report when a change is made


– -R change files and directories recurisvely
An organisation’s network can be divided into subnets. A host (returns EOF or number of tokens matched)
can directly communicate with everything on the same subnet. FILE *fopen(const char *path, const char *
– -v output a diagnostic for eveyr file processed Broadcasts will reach all hosts in the subnet. Network ↔ sub- ,→ mode);
Examples (courtesy of Ainsley Nand on Attic) – -f suppress most error messages net. We can describe the subnet in two ways, CIDR notation int fclose(FILE *fp);
• rm remove or subnet mask. int fgetc(FILE *stream);
• Select the first and third column delimited by ‘ ‘ and sort
by the first – -r remove directories and their contents recursively, 130.102.0.0/16 means set all host bits to 0 and the value after char *fgets(char *s, int size, FILE *stream);
cat file.txt | cut -d’ ‘ -c1,3 | sort -k1 i.e. delete everything in the specified subdirectories / is how many bits are in the network part.
int feof(FILE *stream);
• Get only the 12th line of a given file – -f force -> ignore nonexistent files and arguments, 130.102.12.0/24 is subnet of all addresses starting with
(returns NULL, EOF, EOF and NULL on errors)
cat file.txt | head -12 | tail -1 never prompt 130.102.12 for example. Note that the /x of CIDR notation does
feof is only set after a failed read!
• Get each line with cat and dog in a given file – -d remove empty directories not need to fall on a byte boundary.
– -v explain what is being done (verbose) #include <stdlib.h>
cat file.txt | grep “cat” | grep “dog” Each subnet will have two addresses reserved: all host bits =
• Find each line which contains “CSSE1001” and does not – * remove all zero (minimum host address) and all host bits = one (maximum void *malloc(size_t size);
contain the word “boring” • mkdir make directories host address). So subnet A.B.C.D/x has 32 − x host bits void free(void *ptr);
cat file.txt | grep “CSSE1001” | grep -v “boring” – -m set file mode like chmod and 232−x − 2 usable host addresses (31 is a special case). void *calloc(size_t nmemb, size_t size);
• Find the number of times “rowing” occurs in a given file – -p no error if existing parent, make parent directories A netmask is a bit pattern which will map any IP address to void *realloc(void *ptr, size_t size);
cat ainsley.txt | grep -o “rowing” | wc -l as needed the corresponding network address. (returns NULL on error)
• For all files f1,f2,f3 show all lines containing “song”, “river” • rmdir remove empty directories int atoi(const char *nptr);
• Set all network bits to 1
and “terrible” – -p remove directory and its ancestors e.g. ‘rmdir -p long int strtol(const char *nptr, char **
• Set all host bits to 0.
cat f1 f2 f3 | grep song | grep river | grep terrible a/b/c’ becomes ‘rmdir a/b/c a/b a’ ,→ endptr,
For example, /24 with mask 255.255.255.0.
• For all files g1,g2,g3 show all lines not containing “song”, • cp copy files
• 130.102.24.17 → 130.102.24.0 int base);
“river” and “awful” – -r copy directories recursively
• 130.102.24.250 → 130.102.24.0 (sets errno on error)
cat g1 g2 g3 | grep -v song | grep -v river | grep -v awful • scp secure copy
– -c selects the cipher to use for encrypting data, this • 130.102.21.16 → 130.102.21.0 #include <string.h>
OR grep -ve grep -ve song -ve awful f1 f2 f3
option is directly passed to ssh(1) Needs to be a contiguous string of 1s. Example: void *memset(void *s, int c, size_t n);
• Find all lines in file1 containing the word “dinosaur” and
• mv move files / rename 130.102.160.0/20 is void *memcpy(void *dest, const void *src,
store in file called london
– -f force; do not prompt before overwriting 130.102.10100000.00000000 network bits ,→ size_t n);
cat file1 | grep “dinosaur” > london OR grep dinosaur file1
> london – -i interactive; prompt before overwrite 255.255.11110000.00000000 netmask char *strcpy(char *dest, const char *src);
• Show all lines in file1 starting with W • vim programmers text editor 255.255.240.0 char *strncpy(char *dest, const char *src,
grep ^W file1 • pico simple text editor • 130.102.163.19 → 130.102.160.0 — yes ,→ size_t n);
• Show all lines in file2 ending with S • less allows backward and forward movement • 130.102.171.99 → 130.102.160.0 — yes int strcmp(const char *s1, const char *s2);
grep S$ file2 • ln makes links between files (source is first, destination is • 130.102.176.14 → 130.102.176.0 — yes
• Modify path second) What is the broadcast address for use by 117.98.141.19 net-
export PATH = $PATH:newpath
• Show second last line of file
– -s for symbolic link
– -p for physical/hard link
mask = 255.254.0.0? Netmask tells us that the network is
117.98.0.0/15 or
System Calls
cat file | tail -n 2 | head -n 1 01110101.01100010.00000000.00000000/15
• Show fifth line of file Setting the 32 − 15 = 17 least significant bits to 1 gives
#include <unistd.h>
cat file | head -n 5 | tail -n 1 01110101.01100010.00000000.00000000/15
• Show the third line and later (hide the first 2 lines)
cat file | tail -n +3 Networks 01110101.01100011.11111111.11111111/15
117.99.255.255
=
pid_t fork(void);
(returns 0 to child, child PID to parent, -1
• Show all but the last 10 lines (hide the last 10 lines) ,→ on error)
Give the CIDR form and netmask for the largest net-
cat file | head -n -10 Five-layer TCP/IP stack: work which includes 100.89.19.80, 100.89.19.82 and int execl(const char *path, const char *arg,
Unix commands: 1. Physical layer Medium through which signals travel does not include 100.89.19.97 yes 100.89.19.80 ,→ ...);
• grep print lines matching pattern through. 01100100...00010011.01010000 int execlp(const char *file, const char *arg,
– -v invert-match: displays lines not containing the 2. (Data-)link layer where peers can communicate directly yes 100.89.19.82 01100100...00010011.01010010 ,→ ...);
string via messages (MAC addresses) no 100.89.19.97 01100100...00010011.01100001 (does not return on success)
– -o only show part of line that matches pattern 3. Network layer exchange messages with any other host So 100.89.18.80/26 is as big as possible without including 97. int pipe(int pipefd[2]);
– -i case insensitive on the “internet”, uses IP protocol, IPv4 addresses are The netmask is 255.255.255.224 #include <sys/wait.h>
– -R search all directories 32bit dotted quads, sends messages via the link layer.
Special networks pid_t wait(int *stat_loc);
– $ fish$, for example, looks for the word fish as the 4. Transport layer UDP user datagram protocol (data-
• 10.0.0.0/8 pid_t waitpid(pid_t pid, int *stat_loc, int
last word of a line grams)/TCP transmission control protocol (segments),
– ^ fish^, for example, looks for the word fish as the • 172.16.0.0/12 ,→ options);
sends messages using the network layer, addresses are
first word of a line • 192.168.0.0/16 (returns PID if reaped, 0 if nothing
ports (16 bit int, restricted below 1024)
– . any character in regex except newlines • 169.254.0.0/16 (for auto config) ,→ available)
5. Application layer “Everything else” / web, ssh, games,
– * 0 or more of previous expression in regex SMB. addresses include URL/URI but can be anything. All addresses in 127.0.0.0/8 are loopback addresses (224 − 2

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;

You might also like