I/O Redirection

How does a shell implement output redirection (e.g., ls > output.txt) between fork() and exec()?

A

The shell modifies the kernel's file system table to redirect all writes from fd 1 to the file

B

The shell passes the filename as a special argument to exec(), which handles the redirection internally

C

The shell opens output.txt and uses dup2() to replace stdout (fd 1) with the file's descriptor, so when exec() runs ls, its stdout writes to the file

D

The shell captures ls's output in a buffer after exec() returns, then writes it to the file