> For the complete documentation index, see [llms.txt](https://xiayingp.gitbook.io/build_a_os/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://xiayingp.gitbook.io/build_a_os/os-interfaces/process-and-memory.md).

# 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
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
