Fork and pipe Flashcards

(48 cards)

1
Q

What is a process?

A

A running instance of a program

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

What is the benefits of using multiple processes?

A

(1) Do multiple tasks at once
(2) If one process fails, others are still running
(3) Make use of existing processes

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

What are the numbers corresponding to stdin, stdout, stderr?

A

stdin 0, stdout 1, stderr 2

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

Which of stdin, stdout, stderr goes to the display?

A

stdout and stderr

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

Where does the input of stdin come from?

A

Keyboard

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

Describe the virtual memory layout of a process from top to bottom.

A

OS kernel space, stack (which grows down), empty space, heap (which grow up), bss, data, text

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

What is BSS in the virtual memory layout?

A

BSS is uninitialized data.

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

When a process is being created, what is this state called?

A

The process is idle

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

When a process is waiting for resources, what is this state called?

A

The process is waiting.

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

When a process ended and is waiting to be collect, what is this state called?

A

The process is a zombie.

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

Describe the life cycle of a process.

A

Idle -> Ready -> Run -> Zombie
Run -> Wait (for resources) -> Ready ->
Run -> Ready

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

What is a command to check the current running processes?

A

ps -f

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

What is a system call?

A

A system call is a way for a program (in the user space) to request services from the OS (in kernel space). Some services including requesting OS managed resources like files, memories, devices.

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

How does syscalls work depending on the programming language and the OS?

A

syscalls work the same in every programming language, but it is dependent on OS

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

What is the difference between a normal function call and a syscall?

A

A syscall looks like a function call but it transfers control to the OS

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

Describe what fork() does.

A

fork() creates a copy of the current process and starts it as a child.

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

Describe what execv() and execl() does.

A

It takes an executable as the input, which completely replaces the current process (so in memory, the new code takes over). The executable starts running and the only way to terminate is through termination of the program. (Since the original process is replaced, this new process does not return to the calling program).

If execv() or execl() fails, it returns -1 and the main program continues to run.

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

What does wait() and waitpid() do?

A

These system calls make a parent process wait for a child process to terminate. This allows the parent to manage child processes, retrieve their exit status, and properly cleanup resources. This also prevents creating zombie processes.

wait() waits until one child terminates.

waitpid() allows the parent to specify which child to wait for.

These two syscalls both return the terminated child’s PID.

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

What does kill() do?

A

Send a signal (SIGTERM, SIGKILL, SIGINT) to another process

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

Does each process have a parent?

21
Q

What is a parent process’ id?

A

Ppid (Parent process identifier)

22
Q

How to get a process and its parent’s id?

A

getpid(), getppid()

23
Q

What is the reason to kill a process?

A

When a process is frozen or non-responding and it takes up too much resources.

24
Q

What does fork() return?

A

In the parent, fork() returns the process id of the newly created child.

In the child, fork() returns 0 to indicate that it is a child process.

25
What happens to the parent process after calling fork()?
The parent continues executing concurrently.
26
What does fork() do?
fork() creates a child process
27
What does exec() do?
It executes a program in a process.
28
What is the difference between different versions of exec()?
They provide different ways to pass command line arguments.
29
What is argv and argv[0]?
argv is an array of arguments. argv[0] is the name of the program being executed
30
What does SIGTERM do?
It asks a process to terminate.
31
Can a process send a signal to any other process?
Yes, usually the parent sends signals to its children
32
What happens when a parent process exits?
All of its children processes are terminated.
33
What is good practice before exiting a parent process?
To kill and wait for the children to terminate before exiting.
34
What are signals?
Signals are special messages sent to a process.
35
What signal is it when we press ctrl-c?
SIGINT (terminal interrupt)
36
Can SIGKILL be caught or ignored? What is one other signal like that?
No. SIGSTOP means stop executing and cannot be caught or ignored as well.
37
What does SIGSEGV mean?
Invalid memory segment access
38
What does SIGPIPE mean?
Writing on a pipe with no reader, broken pipe.
39
What are the input arguments to waitpid()?
pid_t pid, int * status, int options
40
What does waitpid() return?
If it successfully waits for the child to terminate, it returns the pid (pid_t type) of the child process. -1 for error. 0 If WNOHANG is specified as an option and no child process has yet terminated, waitpid() returns 0 immediately without blocking. It saves the exit status of the child in the status parameter. If options is set to 0, waitpid() will only return when a child process terminates (exits) and will not return for stopped or continued processes.
41
What does pipe() do? Describe in detail.
It creates a one directional pipe. It sets up a pair of file descriptors that represent the two ends of the pipe: pipefd[0]: The read end of the pipe. pipefd[1]: The write end of the pipe. After creating a pipe, one process can write data to pipefd[1], and another process can read that data from pipefd[0]. This allows for inter-process communication. Usually, a parent process creates a pipe and shares it with a child. pipe() returns 0 if it is create successfully, -1 if there is an error.
42
Draw out the flow of the pipe setup.
N/A
43
Describe the blocking behaviour of pipe().
If a process tries to read from an empty pipe, it will block (wait) until data is written. Similarly, writing to a full pipe will block until space becomes available.
44
What does dup2() do?
It duplicates a file descriptor, allowing redirection of stdin, stdout, stderr to a pipe or another file. int dup2(int oldfd, int newfd)
45
What are the constant names for each input and output?
stdin (0) is STDIN_FILENO stdout (1) is STDOUT_FILENO stderr (2) is STDERR_FILENO
46
What is the function to process CLI options? How to denote if we want no argument after flag k and an option value after -f?
getopt(argc, argv, "kf:")
47
In "$ foo -s -t 10 eth.txt bigdo.txt", what are argc, argv[0], argv[1], etc?
argc is the number of CLI arguments argv[0] is "foo" the executable name argv[1] is -s argv[2] is -t argv[3] is 10 argv[4] is eth.txt (positional arguments) argv[5] is bigdo.txt (positional arguments)
48
What to use when we have an error with getopt()?
optopt, a global variable used by getopt() to store the option character that caused an error