1/97
yes
When a program starts, how many standard file descriptors are already open and ready to use?
3: stdin (0), stdout (1), stderr (2)
What does freopen() do?
Closes an existing FILE stream and reopens it on the given filename/mode (redirects a stream, e.g. freopen("out.txt","w",stdout) sends cout/printf output to a file)
What are the areas of the C++ program memory model?
Text/code segment, initialized data segment, uninitialized data segment (BSS), heap, stack
C++ memory model: which segment has the lifetime of the program unless the program deletes it?
Heap
C++ memory model: which segment is where dynamic memory allocation (new/malloc) takes place?
Heap
C++ memory model: which segment contains variables explicitly initialized in the program (e.g. static int x = 7)?
Initialized data segment
C++ memory model: which area is initialized by the kernel to arithmetic 0 before the program starts executing?
Uninitialized data segment (BSS)
C++ memory model: what is the BSS? What is stored there?
The uninitialized data segment; holds uninitialized global and static variables (set to 0 by the kernel)
T/F: In the C++ memory model, the BSS is where static memory variables are stored.
True
T/F: In the C++ memory model, global variables are automatically initialized to 0.
True
T/F: In the C++ memory model, uninitialized memory area (BSS) variables have a lifetime of the program.
True
C++ memory model: which segment stores the info saved each time a function is called (return address, arguments, local variables)?
Stack
C++ memory model: which segment consists of the machine instructions the CPU executes?
Text (code) segment
Postfix operator (x++) used in a statement like cout << x++: what happens?
Uses/prints the OLD value first, then increments x afterward
After a fork, what are the differences between the parent and the child?
Different PIDs and different fork() return values (child gets 0, parent gets the child's PID); child gets its own copy of memory
What does fork() return in the child process?
0
What does fork() return in the parent process?
The child's PID (a positive number)
What does fork() return if it fails?
-1
T/F: After a fork, the PID of the parent process is always 0.
False. The PARENT gets the child's PID returned; the CHILD gets 0 returned. Nobody's actual PID is 0
Including the initial parent, how many total processes exist after one fork() executes?
2
if (fork()) { cout << "Hello"; } How many lines are displayed?
if (fork()) { fork(); } Including the initial process, how many total processes?
After fork(), each process does cout << getpid(). What is displayed?
Each process prints its OWN PID (child prints child PID, parent prints parent PID). You can't know the exact numbers ahead of time (trick question)
fork(); fork(); fork(); Including the initial process, how many total processes?
8 (2^3)
A for loop executes 2 times with childpid = fork() inside. Total processes after the code?
4 (2^2). n forks in a loop = 2^n processes
Which is NOT true about fork?
Statements like: 'it splits the input data into multiple parts', 'the parent's PID becomes 0', or 'it replaces the child with a new program' (that's exec) are NOT true
T/F: execvp() creates a new process that is a mirror copy of the process executing it.
False. execvp REPLACES the current process image with a new program (fork is what makes the copy)
What does dup() do to a file descriptor?
Duplicates it into the LOWEST-numbered unused file descriptor
int fd[2]; pipe(fd); Which index is used for READING from the pipe?
fd[0]
int fd[2]; pipe(fd); Which index is used for WRITING to the pipe?
fd[1]
Which statement is NOT true about pipes?
'Pipes work across a network / between different machines' (false: pipes are local only, between related processes)
T/F: With a named pipe, a correct open() will block until a corresponding open() occurs across the named pipe.
True
T/F: When using named pipes, both the client and the server must use a known named pipe to establish communication.
True
Correct order of named pipe (FIFO) operations?
mkfifo (create) -> open -> read/write -> close -> unlink. mkfifo is always first, unlink always last
T/F: In a socket client and server program, both programs must know the port address to communicate.
True
Correct order of operations on the CLIENT side of a socket?
socket -> connect -> send/receive (read/write) -> close
Which socket API is NOT used in client socket code? (e.g. listen, socket, read, connect, write)
listen (also bind and accept are server-only)
On the server side, is there a connect() operation?
No. connect() is client-only
Correct order of the five operations on the SERVER side of a socket?
socket -> bind -> listen -> accept -> read/write (send/receive)
Which IPC mechanism can work across a network between different machines?
Sockets
Which item is NOT related to the socket mechanism?
Anything non-network, e.g. shared memory
T/F: Sockets cannot be used to communicate between processes on the same machine.
False. They can (e.g. using the loopback address 127.0.0.1)
When you call socket() with SOCK_STREAM, what does SOCK_STREAM indicate?
A stream-oriented TCP socket: reliable, connection-oriented
What is the IP loopback address?
127.0.0.1
T/F: pthread_create's last (4th) parameter can only take ONE argument.
True. It's a single void* (pack multiple values into a struct)
T/F: After pthread_create sets up the thread, a pthread_start command must be used to start it.
False. There is no pthread_start; pthread_create starts the thread immediately
T/F: A process by default has only one thread of control (one set of machine instructions executing at a time).
True
T/F: To build a C++ app using pthreads on Linux, #include
True
Which memory segment is NOT shared by multiple threads of the same program?
Stack (each thread has its own stack)
T/F: Threads improve program performance because threads in a process can execute for times greater than the time allocated for the process.
False
T/F: Threads execute within a process and share the process's memory/resources.
True
What is a critical code section? What does it do to threads?
Code that accesses shared resources; it allows only ONE thread through at a time
What API calls lock and unlock a critical code section?
pthread_mutex_lock() / pthread_mutex_unlock() (a mutex)
What is a mutex?
Mutual exclusion lock: only one thread at a time can hold it / enter the critical section
What does pthread_join do?
Makes the calling (main) thread wait for the specified thread to finish
In the main program thread, what waits for a started thread to complete?
pthread_join
Which statement is NOT true about threads?
'Threads are created by fork()' or 'threads have separate memory spaces' (both false; threads are made with pthread_create and share memory except the stack)
T/F: cout is NOT thread safe.
True. Concurrent couts can interleave / garble output
What is thread safety?
A function/object can be used by multiple threads at the same time without errors or corrupted data
Definition of a process?
An executing instance of a program, with its own memory space, resources, and CPU state
What type of operating system is Linux?
Preemptive, time-sharing
What does PaaS stand for (cloud computing)?
Platform as a Service
Batch operating system
Runs a series of jobs (batch) with no user interaction (e.g. payroll)
Time-sharing operating system
Lets multiple users/processes share the CPU and resources at the same time
Real-time operating system (RTOS)
Handles tasks with strict timing deadlines (hard RTOS: missed deadline unacceptable, e.g. flight control; soft: e.g. streaming)
Distributed operating system
Manages a group of independent computers so they appear as a single system
fork()
System call that creates a new child process, a copy of the calling (parent) process
exec family (execvp, execlp, …)
Replaces the current process's program with a new program from disk; no new process is created
getpid() / getppid()
getpid returns the calling process's own PID; getppid returns its parent's PID
PID
Process ID: unique number the kernel assigns to each process
Parent process / child process
The parent calls fork(); the child is the new process fork() creates
File descriptor
Small non-negative integer the kernel uses to identify an open file (0 stdin, 1 stdout, 2 stderr)
dup2(fd, fd2)
Duplicates fd onto the specific number fd2 (closes fd2 first if it's open). Used to redirect stdin/stdout into a pipe
Pipe
One-way (half-duplex) channel between related processes; fd[0] = read end, fd[1] = write end; local only
Named pipe (FIFO)
A pipe with a name in the file system (made with mkfifo) so UNRELATED processes can communicate
mkfifo
Creates a named pipe (FIFO)
unlink
Removes the named pipe's (or file's) name from the file system. Last step after close
IPC
Inter-Process Communication: ways processes exchange data (pipes, FIFOs, sockets, shared memory, message queues, semaphores)
Socket
IPC endpoint for communication between processes, on the same machine or across a network
socket()
Creates a socket and returns its descriptor (first call on both client and server)
bind()
Server: attaches the socket to an IP address and port number
listen()
Server: marks the socket as ready to accept incoming connections
accept()
Server: waits for (blocks on) a client connection and returns a new connected socket
connect()
Client: connects its socket to the server's IP address and port
Port
Number identifying a specific service/program on a host; client and server must both know it
TCP vs UDP
TCP = SOCK_STREAM: connection-oriented, reliable, ordered. UDP = SOCK_DGRAM: connectionless datagrams, no guarantees
Thread
A single sequence of execution inside a process; threads share the process's memory but each has its own stack
pthread_create()
Creates and immediately starts a new thread. Args: &thread_id, attributes, function to run, ONE void* argument
pthread_exit()
Terminates the calling thread only
Deadlock
Two or more threads/processes wait forever on each other's locked resources
Race condition
Result depends on the timing of threads accessing shared data without synchronization
Semaphore
Counter controlling access to a shared resource: decrement to use it (sleep if 0), increment when done
Shared memory
IPC where processes share one region of memory; fastest IPC but needs synchronization; NOT network-capable
Preemptive OS
The kernel can interrupt a running process to give the CPU to another
Kernel
Core of the OS; manages CPU, memory, devices. Programs request its services through system calls
System call
A program's entry point into the kernel to request a service (fork, open, read, write, pipe…)
IaaS / PaaS / SaaS
Infrastructure as a Service (VMs, storage) / Platform as a Service (platform to build and deploy apps) / Software as a Service (finished apps, e.g. Gmail)
Cloud computing
On-demand computing resources (servers, storage, platforms, apps) delivered over the internet