# Process and Memory

Xv6 time-share processes. Save its CPU registers when process not running, and restoring them back when it resumes. Process can create another process using `fork`

`wait` returns the pid of the exited child of the current process. `wait` waits until one child to exit.

Parent process and child process are executing with different memory and different registers. Changing a value does not affect another.

`exec` system call replaces the calling process’ memory with a new memory image loaded from a file. The file is ELF format.

Xv6 Shell calls `getcmd` to reads a line of user input, then call `fork`, which creates a copy of the shell process. The parents calls `wait`, while the child is running the command.

Process wants more memory could call `sbrk(n)`.

### Questions: Why `fork` and `exec` not combine together?

#### Answer:

If they are separate, the shell can fork a child, use open, close, dup in the child to change the standard input and output file descriptors, and then exec! Executed program does not need to understand how to redirect I/O. It is simple.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xiayingp.gitbook.io/build_a_os/os-interfaces/process-and-memory.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
