UNIX Exam 2

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/45

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

46 Terms

1
New cards

When a program starts, how many standard file descriptor ids are already open and ready to use?

three: stdin (0), stdout(1), stderr(2)

2
New cards

How does freopen() work?

It redirects an existing stream to a new file

3
New cards

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

4
New cards

What does the freopen() command do?

reopens a stream with a different file or mode, effectively redirecting it

5
New cards

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

6
New cards

After a fork command, the return value in the child is equal to what number?

0

7
New cards

When a fork fails, what numeric value is returned?

-1

8
New cards

Including the initial parent, how many processes exist after one fork()?

2 (parent + child)

9
New cards

Given code: if (fork) { cout } —> How many lines output?

2 lines

10
New cards

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.

11
New cards

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

12
New cards

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)

13
New cards

Code: if (fork) { fork } —> total number of processes created?

3 (1 parent forks once —> 2, then one of them forks again —> 3 total)

14
New cards

What command allows multiple file descriptors to point to the same file table entry?

dup() or dup2(

15
New cards

What is the file descriptor equivalent of freopen()?

dup2()

16
New cards

Three forks in a row —> total number of processes created (including initial)?

2³ = 8 processes

17
New cards

Given a pipe array, which index is used to read from the pipe?

0

18
New cards

Given pipe array, which index is used to write to pipe?

1

19
New cards

With named pipes: a correct open will block until corresponding open across the pipe. True or false?

True

20
New cards

Using named pipes: client and server must use the same known name. True or false?

True

21
New cards

Order of operations to set up a named pipe?

Mkfifo —> open (one and blocks until other end opens) —> read/write

22
New cards

What does execvp() do?

Executes a program, replacing the current process image; uses PATH environment to find executable

23
New cards

Socket client/server must know port number to communicate. True or false?

True

24
New cards

Order of operations for client socket:

Socket —> connect —> send/recv (write/read) —> close

25
New cards

Which socket operation is not used by client socket?

bind() is typically not used by clients (used by servers), accept(), listen()

26
New cards

IPC mechanism can work across a network?

Sockets

27
New cards

Sockets cannot be used between processes on the same machine. True or false?

False (they can!)

28
New cards

Order of socket server operations:

socket —> bind —> listen —> accept —> send/recv (write/read) —> close

29
New cards

SOCK_STREAM indicates what protocol?

TCP

30
New cards

Identify the socket loopback address?

127.0.0.1

31
New cards

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

32
New cards

Do you have to call a pthread start command to start a thread?

False —> pthread_create start command to start a thread?

33
New cards

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

34
New cards

To build a pthread program in Linux, must include pthread.h and use -pthread flag. True or false?

True

35
New cards

Which memory segment is not shared by threads of the same process?

Stack (each thread has its own stack)

36
New cards

Threads improve performance because threads in a process can run longer than the process itself. True or false?

False —> threads cannot outlive their process

37
New cards

What is a critical code section?

a section where only one thread can execute at a time to avoid race conditions

38
New cards

What is the lock called in pthreads to protect critical sections?

mutex (pthread_mutex_lock and pthread_mutex_unlock)

39
New cards

What pthread call waits for a thread to complete?

pthread_join()

40
New cards

Cout is thread safe. True or false?

False —> by default, std::cout is not thread-safe (though can be made thread-safe with mutex)

41
New cards

What type of OS is Linux?

Preemptive, time-shared multitasking OS

42
New cards

In cloud computing, PaaS stands for what?

Platform as a Service

43
New cards

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

44
New cards

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.

45
New cards

Which item is not related to sockets?

  1. "Sockets can only be used on the internet."
    → False. Sockets can be used for local (UNIX domain) communication too, not just networked.

  2. "UDP sockets guarantee message delivery."
    → False. UDP is connectionless and does not guarantee delivery, order, or duplication protection.

  3. "TCP is faster than UDP."
    → False. UDP is generally faster because it skips connection setup and reliability guarantees. TCP is more reliable, not faster.

  4. "A client socket must bind to a port."
    → False. Clients typically don't bind; the OS assigns an ephemeral port automatically.

  5. "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.

  6. "Sockets preserve message boundaries."
    → False for TCP. TCP is a stream-oriented protocol — message boundaries are not preserved. Only UDP preserves boundaries.

  7. "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.

  8. "Only servers use listen() and accept()."
    → True, but sometimes misunderstood — clients never use listen() or accept().

  9. "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.

  10. "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.

46
New cards

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.