用户程序如何使用fork()

liuling_8008 2013-01-23 12:07:46
linux中,用户程序使用fork()创建进程。
我想用户程序如果要fork(),一定已经在内存中,用户程序是如何从硬盘加载内存中的呢?操作系统好像没有提供这样的API。
...全文
330 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
hmqwsdcv 2013-02-13
  • 打赏
  • 举报
回复
linux中,所有进程都是从一个PID为1的名为init的子孙进程。 用户程序总是有一个父进程,所以你就不用担心是谁把它调进内存的了。 init进程是一个由内核启动的用户级别的进程
High_High 2013-02-09
  • 打赏
  • 举报
回复
用户程序运行的时候已经在内存中了吧,调用fork后操作系统把内存中的用户程序copy了一份出来,就变成两个一模一样的用户程序在运行了,不需要读硬盘。 好比克隆一个人,原先的人已经存在,克隆出来的人和原来的人长相,思维一模一样。 exec需要读硬盘,用新程序的代码取代原来程序在内存中的代码。 好比是给克隆人洗脑,洗完克隆人拥有完全不同的思维,就和之前的人不一样了。
nbnitboy 2013-02-03
  • 打赏
  • 举报
回复
do_execve会把环境变量和参数压入到用户堆栈中,设置入口为_start,_start(在库中),然后调用main;main总的argc就是do_execve中压入的参数个数,argv为参数指针;
nbnitboy 2013-02-03
  • 打赏
  • 举报
回复
fork时,会把父进程的进程结构copy到子进程中;do_execve时,会把页目录和页表copy到子进程中,会设置好内核堆栈和用户堆栈;
nbnitboy 2013-02-03
  • 打赏
  • 举报
回复
bash用fork创建一个子进程,然后调用加载程序do_execve(类似系统调用)加载程序到指定进程中(此时并未copy程序,只是读取了可执行文件头,然后根据可执行文件头找到入口_entry);把原来eip指向_entry,当进程从do_execve系统调用返回时,pop出eip,就去执行加载的代码了;(具体细节太多,缺页,堆栈设置)
nanguapanpan 2013-01-29
  • 打赏
  • 举报
回复
楼主对操作系统没概念啊。
牛晨光 2013-01-28
  • 打赏
  • 举报
回复
NAME execl, execlp, execle, execv, execvp, execvpe - execute a file SYNOPSIS #include <unistd.h> extern char **environ; int execl(const char *path, const char *arg, ...); int execlp(const char *file, const char *arg, ...); int execle(const char *path, const char *arg, ..., char * const envp[]); int execv(const char *path, char *const argv[]); int execvp(const char *file, char *const argv[]); int execvpe(const char *file, char *const argv[], char *const envp[]);
liuling_8008 2013-01-28
  • 打赏
  • 举报
回复
内核中有些程序已经被提前放到内存了。
ww2000e 2013-01-23
  • 打赏
  • 举报
回复
Linux Programmer's Manual NAME fork - create a child process SYNOPSIS #include <unistd.h> pid_t fork(void); DESCRIPTION fork() creates a new process by duplicating the calling process. The new process, referred to as the child, is an exact duplicate of the calling process, referred to as the parent, except for the following points: * The child has its own unique process ID, and this PID does not match the ID of any existing process group (setpgid(2)). * The child's parent process ID is the same as the parent's process ID. * The child does not inherit its parent's memory locks (mlock(2), mlockall(2)). * Process resource utilizations (getrusage(2)) and CPU time counters (times(2)) are reset to zero in the child. * The child's set of pending signals is initially empty (sigpend‐ ing(2)). * The child does not inherit semaphore adjustments from its parent (semop(2)). * The child does not inherit record locks from its parent (fcntl(2)). * The child does not inherit timers from its parent (setitimer(2), alarm(2), timer_create(2)). * The child does not inherit outstanding asynchronous I/O operations from its parent (aio_read(3), aio_write(3)), nor does it inherit any asynchronous I/O contexts from its parent (see io_setup(2)).
liuling_8008 2013-01-23
  • 打赏
  • 举报
回复
只是想了解有关操作系统实现相关的问题呀
wwyyxx26 2013-01-23
  • 打赏
  • 举报
回复
你想干什么?
liuling_8008 2013-01-23
  • 打赏
  • 举报
回复
我看到操作系统的系统调用:exeve(name, argv, envp)函数可以指定文件名称,操作系统会自动指定文件(可执行程序)加载到内存。 可是第一个用户程序是如何放到内存里的?
fdl19881 2013-01-23
  • 打赏
  • 举报
回复
楼主你问的问题说明你需要学习下操作系统原理的知识了。 虚拟内存,分页,页面交换,内核态与用户态等等。

4,441

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 内核源代码研究区
社区管理员
  • 内核源代码研究区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧