Lecture 25: Core File and goto Makefiles, Networking Flashcards

1
Q

core files

A
  • when your program has an unrecoverable error, the operating system saves the heap/stack memory at the exact time of the failure into a file named “core”
  • you can use the core file with the debugger
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

core dump file

A
  • man 5 core
  • unlimited -c unlimited
  • second command enables it, which you may have to do
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

why is goto bad?

A
  • prevents compiler from being able to make “nice” computer-science reductions of the program
  • makes your code unreadable
  • is really not necessary
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

why does C have a goto?

A
  • because the compiler doesn’t have any more difficulty analyzing a program with gotos
  • it is very useful and makes the program easier to read
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what does goto look like?

A
  • can define labels and goto those labels
    int func(int x) {
    int sum = 0;
    again:
    sum = sum + x’
    if (x <= 0) {
    goto get_out;
    else
    goto again;
    get_out
    return sum; }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

how can goto make a program clearer to read

A
  • when you really need to ditch the control flow of your program and take drastic measures
  • ex. multiple for, while, if within each other, start_over: outside of it, goto start_over; on innermost if
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

when is goto useful?

A
  • when it is necessary to break out of deeply nested loops (prev ex.)
  • when you’re building a state machine in software
  • should avoid goats unless there is a really good reason
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

makefile

A
  • simple way to help organize code compilation
    composed of rules:
  • target - usually a file to generate (can be an action like make clean)
  • prerequisites - used to create the target
  • recipe - action to carry out (must start with a tab)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

example of hard coded makefile

A

hello: hello.c hellofunc.c
gcc -o hello hello.c hellofunc.c -l
OR
CC=gcc
CFlAGS=-l
hello: hello.o hellofunc.o
$(CC) -o hello hello.o hellofunc.o $(CFLAGS)
- can also add object files and header files

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

bubble sort

A
  • lots of different ways with different performance/complexity
    void bubble_simple(int *arr, int size) {
    for (int k = 0; k < size; k++) {
    for (int I = 1; I < size; I++) {
    if (arr[I-1] > arr[i}) { // swap
    arr[i-1]^ = arr[i];
    arr[i] ^= arr[i-1];
    arr[i-1]^ = arr[i];
    }
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

bubble sort with better time complexity

A

void bubble(int *arr, int size) {
int swapped = 1;
while (swapped) {
swapped = 0;
for (int i = 1; I < size; I++) {
if (arr[I-1] > arr[i[) {
int tmp = arr[i-1];
arr[i-1] = arr[i];
arr[i] = tmp;
swapped = 1;
}
}
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

networking basics

A
  • given a network and a set of systems, how do we actually send data between a subset of systems?
  • first have to find systems (establish a route)
  • decide how we’re going to communicate (protocol)
  • establish a connection (socket)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

internet protocol

A
  • IP is an addressing an fragmentation protocol
  • breaks communication into chunks (packets)
  • routes packets from a source to a destination
  • inherently unreliable (no guarantee anything will make it)
  • different versions of it
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

IP Address

A
  • each host or system has at least one IP address
  • nnn.nnn.nnn.nnn (dotted decimal notation, 4 bytes, 32 bits)
  • packets are sent from/to IP addresses (may traverse multiple routers
  • public/private, NATs
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Domain Name System

A
  • DNS is a distributed system that resolves host names to IP addresses
  • hierarchical, root servers, many authoritative servers
  • name resolution can involve multiple queries, caching servers
  • reverse lookup
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

transmission control protocol

A
  • remember, IP is unreliable
  • TCP builds on IP to create a reliable network connection (referred to as TCP/IP)
  • supports acknowledgments and retransmission
  • hosts identified by IP address and a port number
17
Q

clients and servers

A
  • a server is a process that waits for a connection
  • a client is a process that connects to a server
  • processes can be both!
  • machines can have multiple servers and clients running at once
  • once connected, clients and servers read and write data from/to each other much like a file
18
Q

network sockets

A
  • clients and servers communicate via sockets
  • a socket is an endpoint for sending and receiving data
  • a socket address consists of the IP address and port number (at least for TCP)
  • port number is 16bits
  • ports < 1-24 are privileged
19
Q

server

A
  • man 7 ip
  • steps for listening
  • create a socket()
  • bind() that socket to an address and port
  • listen() for a connection
  • accept() the connection
  • close() the connection
20
Q

socket

A
  • create an endpoint for communication
  • int socket(int domain, int type, int protocol)
21
Q

bind()

A
  • assigning a name to a socket
  • bind (int sockfd, const struct sockaddr *addr, socklen_t addrlen)
  • socket() gives us a fd
  • bind() assigns an “address” to the socket
22
Q

listen()

A
  • marks the socket as a passive socket
  • accepts incoming connections
  • int listen(int sockfd, int backlog)
  • backlog is the max length for the pending connections queue
  • max # of waiting connections
23
Q

accept()

A
  • removes first connection request from pending connections queue
  • must be called on a listen()ing socket
  • add is filled with peer information
  • creates a NEW connected socket
  • this socket does not listen
  • original socket is left alone
24
Q

recv() and send()

A
  • recv() is read() when flags = 0
  • can behave differently depending on the flags
  • send() is write() when flags = 0
  • can behave differently depending on the flags
25
close()
- you should always check the return value of close()
26
steps for connecting a client
- create a socket() - optionally bind() for a specific source port - connect() to an address:port - close() the connection
27
connect()
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen); - connects sockfd to address addr
28