1/45
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
When a program starts, how many standard file descriptor ids are already open and ready to use?
three: stdin (0), stdout(1), stderr(2)
How does freopen() work?
It redirects an existing stream to a new file
What are the 3 arguments to freopen() and what do they do?
filename, mode (e.g., “r”, “w”), existing FILE pointer (e.g., stdout)
i) filename: the file to open
ii) mode: the mode (e.g., “r'“, “w'“, “a”)
iii) stream: the existing stream to redirect (like stdout or stdin) —> closes the original stream and reopens it as the file you specify
What does the freopen() command do?
reopens a stream with a different file or mode, effectively redirecting it
After a fork, identify the differences between parent and child
Different process IDs (getpid()), child gets 0 from fork, parent get child’s PID, separate memory spaces
After a fork command, the return value in the child is equal to what number?
0
When a fork fails, what numeric value is returned?
-1
Including the initial parent, how many processes exist after one fork()?
2 (parent + child)
Given code: if (fork) { cout } —> How many lines output?
2 lines
After fork with getpid(), what does getpid() display a 0?
FALSE. It doesn’t. getpid() returns the process ID (> 0); 0 is returned by fork() in child.
Given a loop with a fork in it, how many processes are created (including initial)?
Depends on loop logic. If fork inside loop with n iterations: creates 2^n total processes
After fork, the pid of the parent is always 0. True or false?
False (pid 0 is returned in the child, not the parent’s PID)
Code: if (fork) { fork } —> total number of processes created?
3 (1 parent forks once —> 2, then one of them forks again —> 3 total)
What command allows multiple file descriptors to point to the same file table entry?
dup() or dup2(
What is the file descriptor equivalent of freopen()?
dup2()
Three forks in a row —> total number of processes created (including initial)?
2³ = 8 processes
Given a pipe array, which index is used to read from the pipe?
0
Given pipe array, which index is used to write to pipe?
1
With named pipes: a correct open will block until corresponding open across the pipe. True or false?
True
Using named pipes: client and server must use the same known name. True or false?
True
Order of operations to set up a named pipe?
Mkfifo —> open (one and blocks until other end opens) —> read/write
What does execvp() do?
Executes a program, replacing the current process image; uses PATH environment to find executable
Socket client/server must know port number to communicate. True or false?
True
Order of operations for client socket:
Socket —> connect —> send/recv (write/read) —> close
Which socket operation is not used by client socket?
bind() is typically not used by clients (used by servers), accept(), listen()
IPC mechanism can work across a network?
Sockets
Sockets cannot be used between processes on the same machine. True or false?
False (they can!)
Order of socket server operations:
socket —> bind —> listen —> accept —> send/recv (write/read) —> close
SOCK_STREAM indicates what protocol?
TCP
Identify the socket loopback address?
127.0.0.1
Pthread_create takes only one argument for the 3rd-to-last param. True or false?
False —> pthread_create takes 4 arguments; the 3rd is the function pointer, and the 4th is its argument
Do you have to call a pthread start command to start a thread?
False —> pthread_create start command to start a thread?
One thread or multiple threads executes at once in a process?
ON single-core CPU: one at a time; on multi-core: multiple can execute simultaneously
To build a pthread program in Linux, must include pthread.h and use -pthread flag. True or false?
True
Which memory segment is not shared by threads of the same process?
Stack (each thread has its own stack)
Threads improve performance because threads in a process can run longer than the process itself. True or false?
False —> threads cannot outlive their process
What is a critical code section?
a section where only one thread can execute at a time to avoid race conditions
What is the lock called in pthreads to protect critical sections?
mutex (pthread_mutex_lock and pthread_mutex_unlock)
What pthread call waits for a thread to complete?
pthread_join()
Cout is thread safe. True or false?
False —> by default, std::cout is not thread-safe (though can be made thread-safe with mutex)
What type of OS is Linux?
Preemptive, time-shared multitasking OS
In cloud computing, PaaS stands for what?
Platform as a Service
Statement about forks: msut choose one that’s not true
❌ “The parent process always receives 0 from fork.” → False (child gets 0).
❌ “fork() returns the same value in both parent and child.” → False.
❌ “The child and parent share the same process ID.” → False (they get different PIDs).
❌ “fork() duplicates the parent process’s memory exactly, and they continue to share memory.” → False (each gets a separate memory space).
❌ “After fork(), there is only one process running.” → False (there are two).
❌ “If fork fails, both parent and child receive -1.” → False (only parent, because child isn't created).
Which statement is not true about pipes?
❌ "Pipes can be used between unrelated processes."
→ Only true for named pipes (FIFOs) or sockets.
→ Anonymous pipes (created with pipe()
) only work between related processes (e.g., parent-child).
❌ "Pipes are bidirectional by default."
→ False. Standard Unix pipes are unidirectional — you need two pipes for two-way communication.
❌ "Once a pipe is created, it cannot be closed."
→ False. You can (and should) close unused ends of a pipe to avoid deadlocks and resource leaks.
❌ "Pipes automatically synchronize data between processes."
→ False. Pipes are just byte streams — synchronization must be handled by the application logic.
❌ "A pipe can store unlimited data."
→ False. Pipes have a limited buffer (typically 4–64 KB) — writing too much can block the writer.
❌ "Named pipes (FIFOs) are private to the process that created them."
→ False. Named pipes exist in the filesystem and can be accessed by unrelated processes with the correct permissions.
❌ "Reading from an empty pipe always returns an empty string."
→ False. If all writers are closed, a read returns EOF (0
bytes), not an empty string.
❌ "Pipes preserve message boundaries."
→ False. Pipes are byte streams, not message queues. There's no guarantee about chunk sizes in reads.
❌ "Only one process can read from a pipe at a time."
→ False. Multiple processes can read from the same pipe, but they’ll compete for data (like any shared file descriptor).
❌ "You can seek within a pipe using lseek()."
→ False. Pipes are non-seekable — using lseek()
on a pipe will result in an error.
Which item is not related to sockets?
❌ "Sockets can only be used on the internet."
→ False. Sockets can be used for local (UNIX domain) communication too, not just networked.
❌ "UDP sockets guarantee message delivery."
→ False. UDP is connectionless and does not guarantee delivery, order, or duplication protection.
❌ "TCP is faster than UDP."
→ False. UDP is generally faster because it skips connection setup and reliability guarantees. TCP is more reliable, not faster.
❌ "A client socket must bind to a port."
→ False. Clients typically don't bind; the OS assigns an ephemeral port automatically.
❌ "The same socket can be used to accept multiple clients at once."
→ False. In TCP, the listening socket calls accept()
to create new connected sockets — it doesn't handle all clients itself.
❌ "Sockets preserve message boundaries."
→ False for TCP. TCP is a stream-oriented protocol — message boundaries are not preserved. Only UDP preserves boundaries.
❌ "You must use connect()
for both TCP and UDP sockets."
→ False. UDP sockets don't require connect()
; you can use sendto()
and recvfrom()
without connecting.
❌ "Only servers use listen()
and accept()
."
→ True, but sometimes misunderstood — clients never use listen()
or accept()
.
❌ "Once a socket is closed on one side, the other side can’t read anymore."
→ False. A socket close is half-duplex — closing one direction doesn’t immediately affect the other side unless fully shut down.
❌ "Socket communication is always faster than pipes."
→ False. Local IPC (like pipes or UNIX domain sockets) is often faster than TCP sockets, especially for short data.
What statement is not true about threads?
❌ "Threads have separate memory space."
→ False. Threads within the same process share memory, including global variables, heap, and file descriptors.
❌ "Each thread runs in its own process."
→ False. Multiple threads run within a single process. They do not create separate processes.
❌ "Threads cannot communicate with each other."
→ False. Threads can easily communicate via shared memory, since they share the same address space.
❌ "Threads do not require synchronization."
→ False. Shared access to memory requires synchronization tools (like mutexes or semaphores) to avoid race conditions.
❌ "Killing a thread terminates the entire process."
→ False. Killing one thread does not necessarily kill the whole process — unless it’s the main thread or unless pthread_exit()
and cleanups aren't handled properly.
❌ "Each thread has its own copy of all variables."
→ False. Local (stack) variables are per-thread, but global and heap variables are shared.
❌ "Threads execute one at a time, never concurrently."
→ False. On multi-core CPUs, threads can execute truly in parallel.
❌ "Thread creation is more expensive than process creation."
→ False. Threads are generally lighter and cheaper to create than processes.
❌ "Threads cannot be scheduled independently."
→ False. Modern thread libraries allow independent scheduling of threads by the OS.
❌ "You cannot have more than one thread per process."
→ False. You can have many threads per process, depending on system limits.