What is the differences between fork and exec
In Unix-like systems, and are two critical system calls for process management. They are commonly used to create new processes and execute new programs, but they serve distinct purposes.1.The system call creates a new process, known as the child process, which is a copy of the current process. The child process inherits most of the parent's environment, including code segments, heap, stack, and file descriptors. However, it has its own independent process identifier (PID). In the parent process, returns the PID of the newly created child process, while in the child process it returns 0. If an error occurs, such as insufficient memory, returns a negative value.Example:2.The family of functions executes a new program within the context of the current process. This means the current process's code and data are replaced by the new program, but the process ID remains unchanged. This is typically used after , where the child process loads and runs a completely new program using .The family includes multiple variants such as , , , and , which differ primarily in how parameters and environment variables are passed.Example:SummaryPurpose: creates a child process identical to the current process; executes a new program within the current process context.Mechanism: creates a full copy with a different PID; replaces the process content while keeping the PID unchanged.Usage: and are often used together: first creates a child process, then the child calls to replace itself with another program. This pattern enables executing new programs without terminating the original process.In practical applications, the combination of and is widely used, for example, in implementing shell programs where this mechanism is extensively employed to create and run user-specified commands.