0
0
Operating Systemsknowledge~20 mins

Process creation (fork and exec) in Operating Systems - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Process Creation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding fork() behavior

What happens immediately after a fork() system call in a Unix-like operating system?

AThe child process starts executing a different program automatically.
BThe parent process is replaced by a new program.
CThe operating system terminates the parent process.
DA new child process is created as a copy of the parent process.
Attempts:
2 left
💡 Hint

Think about whether the parent process continues to run after fork().

📋 Factual
intermediate
2:00remaining
Role of exec() in process creation

What is the primary purpose of the exec() family of system calls in process creation?

ATo create a new process by duplicating the current one.
BTo terminate the current process and all its children.
CTo replace the current process's memory space with a new program.
DTo pause the current process until a child finishes execution.
Attempts:
2 left
💡 Hint

Consider what happens to the program code when exec() is called.

🔍 Analysis
advanced
2:00remaining
Output of combined fork() and exec() code

Consider the following code snippet in a Unix-like system:

pid = fork()
if pid == 0:
    exec("/bin/ls")
else:
    wait()

What is the expected behavior of this code?

AThe code causes a runtime error because exec() is called incorrectly.
BThe child process lists directory contents, parent waits for child to finish.
CBoth parent and child list directory contents simultaneously.
DThe parent process lists directory contents, child waits.
Attempts:
2 left
💡 Hint

Remember what fork() returns in parent and child, and what exec() does.

Comparison
advanced
2:00remaining
Difference between fork() and exec()

Which statement best describes the difference between fork() and exec() system calls?

A<code>fork()</code> creates a new process; <code>exec()</code> replaces the current process's program.
B<code>fork()</code> replaces the current process's program; <code>exec()</code> creates a new process.
CBoth <code>fork()</code> and <code>exec()</code> create new processes but in different ways.
D<code>fork()</code> terminates the current process; <code>exec()</code> pauses it.
Attempts:
2 left
💡 Hint

Think about whether each call creates a new process or changes the current one.

Reasoning
expert
2:00remaining
Why use fork() before exec()?

Why do most Unix-like programs use fork() followed by exec() to run a new program instead of just calling exec() directly?

ATo create a new process so the original program can continue running separately.
BBecause <code>fork()</code> cleans up memory before <code>exec()</code> runs.
CBecause <code>exec()</code> cannot be called without <code>fork()</code> first.
DTo ensure the new program runs with higher priority than the original.
Attempts:
2 left
💡 Hint

Consider what happens if exec() is called without fork().