scott dollinger unix final

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/97

flashcard set

Earn XP

Description and Tags

yes

98 Terms

1
New cards

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

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

2
New cards

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)

3
New cards

What are the areas of the C++ program memory model?

Text/code segment, initialized data segment, uninitialized data segment (BSS), heap, stack

4
New cards

C++ memory model: which segment has the lifetime of the program unless the program deletes it?

Heap

5
New cards

C++ memory model: which segment is where dynamic memory allocation (new/malloc) takes place?

Heap

6
New cards

C++ memory model: which segment contains variables explicitly initialized in the program (e.g. static int x = 7)?

Initialized data segment

7
New cards

C++ memory model: which area is initialized by the kernel to arithmetic 0 before the program starts executing?

Uninitialized data segment (BSS)

8
New cards

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)

9
New cards

T/F: In the C++ memory model, the BSS is where static memory variables are stored.

True

10
New cards

T/F: In the C++ memory model, global variables are automatically initialized to 0.

True

11
New cards

T/F: In the C++ memory model, uninitialized memory area (BSS) variables have a lifetime of the program.

True

12
New cards

C++ memory model: which segment stores the info saved each time a function is called (return address, arguments, local variables)?

Stack

13
New cards

C++ memory model: which segment consists of the machine instructions the CPU executes?

Text (code) segment

14
New cards

Postfix operator (x++) used in a statement like cout << x++: what happens?

Uses/prints the OLD value first, then increments x afterward

15
New cards

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

16
New cards

What does fork() return in the child process?

0

17
New cards

What does fork() return in the parent process?

The child's PID (a positive number)

18
New cards

What does fork() return if it fails?

-1

19
New cards

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

20
New cards

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

2

21
New cards

if (fork()) { cout << "Hello"; } How many lines are displayed?

  1. Only the parent prints (fork returns nonzero in parent, 0 in child)
22
New cards

if (fork()) { fork(); } Including the initial process, how many total processes?

  1. Parent forks child 1, then parent (nonzero) forks again; child 1 got 0 so it skips
23
New cards

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)

24
New cards

fork(); fork(); fork(); Including the initial process, how many total processes?

8 (2^3)

25
New cards

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

26
New cards

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

27
New cards

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)

28
New cards

What does dup() do to a file descriptor?

Duplicates it into the LOWEST-numbered unused file descriptor

29
New cards

int fd[2]; pipe(fd); Which index is used for READING from the pipe?

fd[0]

30
New cards

int fd[2]; pipe(fd); Which index is used for WRITING to the pipe?

fd[1]

31
New cards

Which statement is NOT true about pipes?

'Pipes work across a network / between different machines' (false: pipes are local only, between related processes)

32
New cards

T/F: With a named pipe, a correct open() will block until a corresponding open() occurs across the named pipe.

True

33
New cards

T/F: When using named pipes, both the client and the server must use a known named pipe to establish communication.

True

34
New cards

Correct order of named pipe (FIFO) operations?

mkfifo (create) -> open -> read/write -> close -> unlink. mkfifo is always first, unlink always last

35
New cards

T/F: In a socket client and server program, both programs must know the port address to communicate.

True

36
New cards

Correct order of operations on the CLIENT side of a socket?

socket -> connect -> send/receive (read/write) -> close

37
New cards

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)

38
New cards

On the server side, is there a connect() operation?

No. connect() is client-only

39
New cards

Correct order of the five operations on the SERVER side of a socket?

socket -> bind -> listen -> accept -> read/write (send/receive)

40
New cards

Which IPC mechanism can work across a network between different machines?

Sockets

41
New cards

Which item is NOT related to the socket mechanism?

Anything non-network, e.g. shared memory

42
New cards

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)

43
New cards

When you call socket() with SOCK_STREAM, what does SOCK_STREAM indicate?

A stream-oriented TCP socket: reliable, connection-oriented

44
New cards

What is the IP loopback address?

127.0.0.1

45
New cards

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)

46
New cards

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

47
New cards

T/F: A process by default has only one thread of control (one set of machine instructions executing at a time).

True

48
New cards

T/F: To build a C++ app using pthreads on Linux, #include must be in the source and -pthread must be on the g++ build line.

True

49
New cards

Which memory segment is NOT shared by multiple threads of the same program?

Stack (each thread has its own stack)

50
New cards

T/F: Threads improve program performance because threads in a process can execute for times greater than the time allocated for the process.

False

51
New cards

T/F: Threads execute within a process and share the process's memory/resources.

True

52
New cards

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

53
New cards

What API calls lock and unlock a critical code section?

pthread_mutex_lock() / pthread_mutex_unlock() (a mutex)

54
New cards

What is a mutex?

Mutual exclusion lock: only one thread at a time can hold it / enter the critical section

55
New cards

What does pthread_join do?

Makes the calling (main) thread wait for the specified thread to finish

56
New cards

In the main program thread, what waits for a started thread to complete?

pthread_join

57
New cards

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)

58
New cards

T/F: cout is NOT thread safe.

True. Concurrent couts can interleave / garble output

59
New cards

What is thread safety?

A function/object can be used by multiple threads at the same time without errors or corrupted data

60
New cards

Definition of a process?

An executing instance of a program, with its own memory space, resources, and CPU state

61
New cards

What type of operating system is Linux?

Preemptive, time-sharing

62
New cards

What does PaaS stand for (cloud computing)?

Platform as a Service

63
New cards

Batch operating system

Runs a series of jobs (batch) with no user interaction (e.g. payroll)

64
New cards

Time-sharing operating system

Lets multiple users/processes share the CPU and resources at the same time

65
New cards

Real-time operating system (RTOS)

Handles tasks with strict timing deadlines (hard RTOS: missed deadline unacceptable, e.g. flight control; soft: e.g. streaming)

66
New cards

Distributed operating system

Manages a group of independent computers so they appear as a single system

67
New cards

fork()

System call that creates a new child process, a copy of the calling (parent) process

68
New cards

exec family (execvp, execlp, …)

Replaces the current process's program with a new program from disk; no new process is created

69
New cards

getpid() / getppid()

getpid returns the calling process's own PID; getppid returns its parent's PID

70
New cards

PID

Process ID: unique number the kernel assigns to each process

71
New cards

Parent process / child process

The parent calls fork(); the child is the new process fork() creates

72
New cards

File descriptor

Small non-negative integer the kernel uses to identify an open file (0 stdin, 1 stdout, 2 stderr)

73
New cards

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

74
New cards

Pipe

One-way (half-duplex) channel between related processes; fd[0] = read end, fd[1] = write end; local only

75
New cards

Named pipe (FIFO)

A pipe with a name in the file system (made with mkfifo) so UNRELATED processes can communicate

76
New cards

mkfifo

Creates a named pipe (FIFO)

77
New cards

unlink

Removes the named pipe's (or file's) name from the file system. Last step after close

78
New cards

IPC

Inter-Process Communication: ways processes exchange data (pipes, FIFOs, sockets, shared memory, message queues, semaphores)

79
New cards

Socket

IPC endpoint for communication between processes, on the same machine or across a network

80
New cards

socket()

Creates a socket and returns its descriptor (first call on both client and server)

81
New cards

bind()

Server: attaches the socket to an IP address and port number

82
New cards

listen()

Server: marks the socket as ready to accept incoming connections

83
New cards

accept()

Server: waits for (blocks on) a client connection and returns a new connected socket

84
New cards

connect()

Client: connects its socket to the server's IP address and port

85
New cards

Port

Number identifying a specific service/program on a host; client and server must both know it

86
New cards

TCP vs UDP

TCP = SOCK_STREAM: connection-oriented, reliable, ordered. UDP = SOCK_DGRAM: connectionless datagrams, no guarantees

87
New cards

Thread

A single sequence of execution inside a process; threads share the process's memory but each has its own stack

88
New cards

pthread_create()

Creates and immediately starts a new thread. Args: &thread_id, attributes, function to run, ONE void* argument

89
New cards

pthread_exit()

Terminates the calling thread only

90
New cards

Deadlock

Two or more threads/processes wait forever on each other's locked resources

91
New cards

Race condition

Result depends on the timing of threads accessing shared data without synchronization

92
New cards

Semaphore

Counter controlling access to a shared resource: decrement to use it (sleep if 0), increment when done

93
New cards

Shared memory

IPC where processes share one region of memory; fastest IPC but needs synchronization; NOT network-capable

94
New cards

Preemptive OS

The kernel can interrupt a running process to give the CPU to another

95
New cards

Kernel

Core of the OS; manages CPU, memory, devices. Programs request its services through system calls

96
New cards

System call

A program's entry point into the kernel to request a service (fork, open, read, write, pipe…)

97
New cards

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)

98
New cards

Cloud computing

On-demand computing resources (servers, storage, platforms, apps) delivered over the internet