CPU Virtualization-Process API

1. fork() System Call

fork() system call is used to create a new process. One process calls the fork() system call, the process that is created is an (almost) exact copy of the calling process and both are about to return from the fork() system call. The newly-created process doesn’t start running at main().

Although it now has its own copy of the address space (i.e., its own private memory), its own registers, its own PC, and so forth, the value it returns to the caller of fork() is different. Specifically, while the parent receives the PID of the newly-created child, the child receives a return code of zero. This differentiation is useful, because it is simple then to write the code that handles the two different cases. The output (of p1.c) is not deterministic. When the child process is created, there are now two active processes in the system that we care about: the parent and the child. Assuming we are running on a system with a single CPU (for simplicity), then either the child or the parent might run at that point.

fork()之后,操作系统会复制一个与父进程完全相同的子进程进程共享代码空间,但是数据空间是互相独立的,子进程数据空间中的内容是父进程的完整拷贝,指令指针也完全相同,子进程拥有父进程当前运行到的位置(两进程的程序计数器pc值相同,也就是说,子进程是从fork返回处开始执行的),但有一点不同,如果fork成功,子进程中fork的返回值是0,父进程中fork的返回值是子进程的进程号,如果fork不成功,父进程会返回错误。这两个进程的执行顺序也是不确定的,需要有OS的scheduler来决定。

2. wait() System Call

It is quite useful for a parent to wait for a child process to finish what it has been doing. It returns the pid of the child process.

3. exec() System Call

This system call is useful when you want to run a program that is different from the calling program. Given the name of an executable, and some arguments, it loads code (and static data) from that executable and overwrites its current code segment (and current static data) with it; the heap and stack and other parts of the memory space of the program are re-initialized. Thus, it does not create a new process; rather, it transforms the currently running program into a different running program.

4. Motivation

The separation of fork() and exec() lets the shell run code after the call to fork() but before the call to exec(); this code can alter the environment of the about-to-be-run program. Here is the steps of using shell:

1. It shows you a prompt and then waits for you to type something into it.

2. Then type a command (i.e., the name of an executable program, plus any arguments) into it; in most cases, the shell then figures out where in the file system the executable resides.

3. Calls fork() to create a new child process to run the command.

4. Calls some variant of exec() to run the command, and then waits for the command to complete by calling wait().

5. When the child completes, the shell returns from wait() and prints out a prompt again, ready for your next command.

5. Process Control And Users

There are other interfaces for interacting with processes in UNIX systems:

kill(): kill() system call is used to send signals to a process, including di- rectives to pause, die, and other useful imperatives.

signal(): signal() system call “catches” various signals, doing so ensures that when a particular signal is delivered to a process, it will suspend its normal execution and run a particular piece of code in response to the signal.

6. Other tools

1. ps: allows you to see which processes are running.

2. top: displays the processes of the system and how much CPU and other resources they are eating up.

3. kill: sends arbitrary signals to processes.


Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2019-2021 Zirun Lin

Thanks~

支付宝
微信