Final Review Flashcards

1
Q

Explain the behind the scene steps behind “gcc” command

A
  1. The C preprocessor processed macros (#define and #include) in
    the source code (“.c” file) and produced a new source code file
    (unnamed)
2. The **C compiler** processed the output of the C preprocessor into assembly
language code (“.s” file).
  1. The assembler processed the assembly language file and produced a
    binary object file (“.o” file).
  2. The linker combined the object file with necessary libraries and produced
    the executable file (“a.out”).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does it mean that C is “weakly typed”?

A

This means the compiler assumes the programmer knows what she/he is doing and allows casting of anything to anything else.

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

Explain the two types of C conversions: Explicit vs. Implicit.

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

What is a variable?

A

Memory that can hold the value. *Note: the deault value is mostly undefined, so treat it as random idea before initilization

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

List the components that everyta variable in C has.

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

When and where is the size of variable determined when a global variable is created? A local variable? A dynamic data varaible?

A
  • Global data variables at compile time (.data)
  • local data vairable at run-time (stack)
  • dynamic data variables by the programmer (heap)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a pointer?

A

a variable continaing the address of another variable

(when initiated without a value, points to a random location in memory)

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

If we have the 2D array

int weekends[52][2];

What is the equivalent of weekends[3][1] in pointer arithmetic?

A

*(weekends + (3*2) + 1)

NOTE: “*” surrounds everything

General formula:

arr[r][c] = *(arr + (r*num_cols) + c)

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

All data objects of type FILE *…

A
  • can be connected to file system files for reading and writing
  • represent a buffered systemf chars (bytes) to be written or read
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What do processors do?

A

From startup to shut down, CPU simply readsnd executes (intereprets) a sequence of instructions, one at a time… this sequenece is called the CPU’s control flow (of flow of control)

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

Define exception

A

An execpetion is a transfer of control to the OS kernel in response to some event (i.e. change in processor state)

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

What are asynchronous events/ interrupts?

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

What are synchonous exepctions?

A

Exceptions caused by events that occur as a result of executing an instruction. This includes:

  • Traps
  • Faults
  • Aborts
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Explain a Trap Exception and provide examples.

A
  • Intentional, returns control to “next” instruction
  • EX. system calls, breakpoint traps, special instructions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Explain Fault Exception and give an example.

A
  • Unintential but possibly recoverable, either re-exectues faulting/current instruction or aborts
  • EX: page fualts (recoverable), rotection faults (unrecoverable), floating point exceptions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Explain abort exceptions and provide an example.

A
  • Unintentional and unreciverable, aborts the current program
  • EX: llegal instruction, party error, machine check
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What are exception tables?

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

What is a process?

A

A process is in an instance of a running programnd provides each program with two key abstractions

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

Explain the three states a process could be in.

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

What is the kernel?

A

The kernel is a shared chunk memory-resident OS code that manages processes

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

What is context switching?

A

Context switching is when control flow passes from one process to another.

Some examples of causes could be

  • OS scheduling processs
  • Kernel executing a system for user
  • interrupts
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Explain fork()

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

What are the three reasons a process becomes terminated?

A

1) Recieving a signal whose default action is to terminate
2) Returning from the main routine
3) Calling the exit function

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

Explain exit()

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

Explain reaping child processes.

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

Explain wait()

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

What macros can we use to get information about the exit status of a child process?

A

WIFEXITED and WEXITSTATUS

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

explain waitpid()

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

explain execve()

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

What is a signal in terms of ECF (Exceptional Control Flow)?

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

ECF: Exlpain sending a signal.

A
32
Q

ECF: Explain recieving a signal.

A
33
Q

ECF: Explain pending and blocking signals.

A
34
Q

Explain signal()

A
35
Q

Explain a Client Server Transaction.

A
36
Q

How is a connection uniquely identified

A

by the socket adresses of its endpoints aka a socket pair

37
Q

What is a socket?

A
38
Q

Explain accept().

A

Accept waits for a connection request from the client to arrive on the given listening descriptor, then fills in the given socket address, and returns a connected descriptor that can be used to communicate with the client

39
Q

Describe the socket interface as a whole. What functions/ actions do the client and the server each need to do for a successful connection?

A
40
Q

What are races and give an example.

A

Races occur when the outcome depends on the arbitrary scheduling decisions elsewhere in the system

EX. who gets the last seat in an ariplane

41
Q

What is a deadlock and provide an example.

A

Deadlocks occur when there is imporpper resource allocation prevents forward progress

EX. traffic gridlock

42
Q

What is livelock?

A

Also known as startvation and fairness, this is when external events and or system schedulingdecisions can preventsub-task progress

EX. people always jump in front of you in line

43
Q

What are the three different approaches to writing concurrent servers.

A
44
Q

Explain the flaw in in Iterative Servers, where requests are processed one at a time.

A
45
Q

Explain Process-based servers.

A
  • Spawns a separate process for each client
    • No shared state between them
  • Simple and straightforward, but has issues…
    • Listening sever process must reapzombie children to avoid fatal memory leak
    • The parent process must close its copy of confd because the kernel wont close a connection until the refcount to confd is 0
46
Q

Explain Event-based Servers

A
  • ALso known as multiplexing
  • Server maintains set of active connections
    • an Array of connfds
  • Repeats:
    • Determine which desriptors (connfd’s pr listenfd) have pending inputs using select() - the arrival of a pending input is considered an event
    • If listenfs has input, then accept connection and add new connfd to array
    • Service all connfds withpending inputs
47
Q

Explain thread-based servers.

A
  • Very similar to the approach of using processes but instead just with threads
  • Uses Pthreads library
  • Need to malloc for connfdp to avoid race condition
  • In the thread function, must
    • copy the given void *vargo, into a new local stack variable connfd, basically removing that data from the heap and placing it on the stack
    • call Pthread_detach() in order to run independtently of other threads and get reaped automatically by the kernel when it terminates
    • free storage allocated to hold connfd
    • close connfd, which closes this fd for all the threads because we share one fd table
      • because if we didn’t close it here, the main thread would not know when to close it
      • Not doing this can lead to running out of fd for long running programs
48
Q

How do we know which variables in threaded C Porograms are share?

A

A variable x is shared if and only if multiple threads reference some instance of x

49
Q

What does volatile mean?

A

It means please dont keep the intermidate value in a register. So, every instance of updating must be pushed back to memory

50
Q

What does it mean when a function is atomic?

A

This means they can not be interrupted and taken on or off the processor. They are allowed to continue their execution until they are complete

51
Q

What is a semaphore?

A

A semaphor is a non-negative global integer synchonization variable that is manipulated by P and V operations

52
Q

Explain why mutexes work.

A

Since Semaphores can never be a nagitive number, mutexes essentially block off the unsade region where the variable would be less than 0 and ensures a safe trajectory

53
Q

Explain the Producer-Consumer Problem.

A
  • Key components of a Producer thread, Consumer thread, and shared buffer between the threads
  • Common synchronization pattern:
    • Producer waits for empty slot, inserts item in buffer, and notifies consumer
    • Consumer waust for item, removes it from buffer,a nd notifers producer
  • EX:
    • Multimedia processessing: Producer creates MPEG video frames, consumer renders them
54
Q

How many mutexes and semaphores are need to solve the Producer-Consumer Problem? And what are each of there uses?

A

Requires 1 mutex and two counting semaphores:

  • mutex: enforces mutally exclusive access to the buffer
  • slots: counts the available slots in the buffer
  • items: counts the available items in the buffer
55
Q

Explain the Readers-Writers Problem

A

It is a generalization of the mutal exclusion problem…

  • Problem statement:
    • Reader threads only read the object
    • Writer threads modift the object
    • Writers must have exclusive access to the object
    • Unlimited number of readers can access the object
  • Real World EX: Online airline reservation system
  • There are variants of this problem:
    • First readers-writers problem (favor readers)
      • No reader should be kept waiting unless a writer has already been granted permission to use the object
      • A reader that arrives after a waiting writer gets priority over the writer
    • Secind readers-writers problem (favors writers)
      • Once a writer is ready to write, it performs its write as soon as possible
      • A reader that arrives after a writer must wait, even if the writer is also waiting
56
Q

What is starvation?

A

When threads are blocked indefinitely because the other type of

57
Q

How do we know if a function is thread-safe?

A

A fucntions is thread-safe iff it will always produce correct results when called repeatedly from multiple concurrent threads

58
Q

What are the 4 classes of thread-unsafe functions?

A
  • Class 1: Functions taht do not protect shared variables
  • Class 2: Functiosn that keep state across multiple invocations
  • Class 3: Functions that return a pointer to a static varibable
      • Class 4: Functions that call thread-unsafe functions
59
Q

Explain Class 1 Thread-Unsafe Function’s Fixes and Examples.

A
  • Fix by using P and V semaphore operations
  • EX. the badcnt.c & goodcnt.c examples where the volatile global variable of cnt was initially unprotected
  • Issue: synchronization operations will slow down code
60
Q

Explain Class 2 Thread Unsafe Function’s Fixes and Examples.

A
  • Fix: Pass state as part of argument, and, thereby eliminate global state
  • EX: Random number generator that relies o static state
  • Consequence: programmer using rand_r (the thread safe version of rand) must maintain seed
61
Q

Explain Class 3 Thread Unsafe Function’s Fixes and Examples.

A
  • Fix 1: Rewrite function arguments so caller passes address of variable to store result into
    • Requrires changes in caller and callee
  • Fix 2: Lock and copy
    • Requires simple changes in caller (and non in callee)
    • However, caller must free memory
  • EX. ctime & gethostbyname
62
Q

When is a function reentrant?

A

A function is reentrant iff it accesses no shared variables when called by multiple threads

63
Q

Explain Bind()

A

Bind asks the Kernel to associate the server’s given socket address with the given socket descriptor

64
Q

Explain Socket()

A

Socket is used by clients and servers to create a socket descriptor given a domain, typem and protocol

65
Q

Explain Listen()

A

Listen converts the given socketfd from an active socket to a listening socket, telling the kernel that the descriptor will be used by the server instead of the client

66
Q

Explain Connect()

A

Connect attempts to establish a connections iwth the given server’s socket address, If successful, the given clientfd is ready for reading and writing

67
Q

What are all the default actions of a signal?

A
  • The process terminates
    • EX. SIGKILL
  • The process terminates and dumps core
    • EX. SIGSEGV
  • The process stops until restated by SIGCONT signal
    • EX. SIGSTOP
  • The process ignores the signal
    • EX. SIGCHILD
68
Q

What is implicit blocking mechanism?

A
  • Kernel locks any pending signals of type currently being handled
    • Ex. A SIGINT handler can’t be interrupted by another SIGINT
69
Q

What is explicit blocking and unblocking mechanism?

A
  • Using sigpromask function and its suppproting functions…
    • sigemotyset - creates empty set
    • sigfillset - adds every signal number to set
    • sigaddset - add signal number to set
    • sigdelset - deletes signal unmber from set
70
Q

What is sigsuspend()?

A

Equivalent to atomic (uniterruptable) version of:

sigpeomask(SIG_BLOCK, &mask, &prev);

pause();

sigpromask(SIG_SETMASK, &prev, NULL);

71
Q

What are all the items shared between threads?

A
  • Global variables
  • Program Code
  • File Descriptor Table
  • Heap
  • Heap Pointer
72
Q

What items are unique to each thread?

A
  • Program Counter
  • Stack Ppointer
  • Register Values
73
Q

When file sharing, information about the files are kept in what 3 levels/tables?

A
  • Despriptor Table
  • File Table
  • V-node Table
74
Q

Describe what a Desciptor Table is (shared or unique to processes? What kind of entries?)

A
  • each process has its own table
  • entries are index of file descriptors
  • each open descriptor entry points to an entry in the file table
75
Q

Describe what a File Table is (shared or unique to processes? What kind of entries?)

A
  • shared by all processes
  • entries contain informayion about file (current position, # of references, pointer to an entry in v-node table)
  • Kernel only removes entry if reference count = 0
76
Q

Describe what a V-node Table is (shared or unique to processes? What kind of entries?)

A
  • Shared by all processes
  • each entry contains info from stat structure
    • Info in stat struct includes File access, File size, File type, etc