请说明fork() [英] please Explain fork()

查看:110
本文介绍了请说明fork()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Unix上搜索过有关fork的任何地方,但是我还不了解. 例如,当我们进入外壳(bash)并运行命令时(假设为"ls") 我们在调用fork()系统调用吗? 'ls'是孩子,而当前的shell是父母?

I have already search anywhere about fork in Unix but i haven't yet understand something. For example when we are in our shell (bash) and we run a command (let's say 'ls') Are we calling fork() system call ? 'ls' is the child and the current shell is the parent?

在我读过的书中确切地说:当复制当前正在运行的程序以生成一个孩子,即正在运行的程序的精确副本时,就会产生一个fork".这是什么意思? bash的确切副本?当我运行ps -ef时,我可以看到并理解PID和PPID(父级). 但是,为什么书能说明这一点?确切的副本不是同一程序(该程序的相同过程)吗?

In the book i read says exactly "A fork is produced when the current running program is copied to make a child, an exact copy of the running program". What is this means ? Exact copy of the bash? When i run ps -ef i can see and understand the PID and the PPID(parent). But why book tells that ? Exact copy isn't the same program (same process of the program)?

我可以理解exec()系统调用....请帮助兄弟....谢谢

And I can understand the exec() system call.... Please somebody help Brothers.... THANK YOU

推荐答案

您了解单个CPU如何执行多个进程,对吗?距离其中的两个过程可能完全相同(即它们执行完全相同的代码并共享相同的资源)很短,这是一个短暂的飞跃.本质上,这是在调用fork()系统调用时发生的情况.

You understand how a single CPU can execute multiple processes, right? It's a short leap from there to imagine that it is possible that two of these processes can be identical i.e. they execute the exact same code, and share the same resources. This is essentially what happens when the fork() system call is called.

fork()系统调用被设计为原始类型,这是从现有进程中产生更多进程的第一步. fork()调用基本上会在内核内部创建表示当前进程的数据结构的副本,并开始执行.然后,新进程可以使用完全不同的程序的代码覆盖自身(使用exec()系统调用),然后执行该程序.

The fork() system call was designed to be a primitive that is the first step in spawning more processes from an existing process. The fork() call basically creates a copy of the data structure(s) inside the kernel that represent the current process, and starts its execution. The new process can then overlay itself with the code for an entirely different program (using the exec() system call), and execute that instead.

任何需要创建另一个进程的进程都需要首先调用fork()来创建自身的副本.副本然后执行其他程序.每次需要执行"ls"或"cp"之类的程序时,Bash都必须执行此操作.它进行forks(),然后复制过程继续并执行目标程序.

Any process that needs to create another process, needs to first call fork() to create a copy of itself. The copy then executes some other program. Bash has to do this every time it needs to execute another program like "ls" or "cp" or whatever. It forks(), and then the copy process goes ahead and executes the target program.

这篇关于请说明fork()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆