- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
为什么这个程序永远不会返回并继续创建子进程?
int main()
{
pid_t pid;
int foo1 = 1, foo2 = 2;
printf("before fork()\n");
if ((pid = vfork()) < 0 ) {
printf("fork failed.\n");
}else if (pid == 0) {
foo1++;
foo2++;
printf("child process: pid is %d, my parent pid is %d\n", getpid(), getppid());
}else if (pid > 0){
printf("parent process: pid is %d\n", getpid());
}
printf("%s: foo1 is %d, foo2 is %d\n",pid == 0 ? "child process" : "parent process", foo1, foo2);
return 0;
}
输出如下:
before fork()
child process: pid is 17244, my parent pid is 15839
child process: foo1 is 2, foo2 is 3
parent process: pid is 15839
parent process: foo1 is -1079005816, foo2 is -1218256081
before fork()
child process: pid is 17245, my parent pid is 15839
child process: foo1 is 2, foo2 is 3
parent process: pid is 15839
parent process: foo1 is -1079005816, foo2 is -1218256081
before fork()
.....
.....
如果在第二个 if block 中添加一个 _exit 就可以了。我知道 vfork 与父进程共享相同的地址空间,但如果程序以崩溃而不是无限循环结束则更合理。
最佳答案
vfork
是一个非常棘手的系统调用,它的唯一用途是立即在子系统中拥有一个 execve
- 对于其他类型的用途,它是危险且不可预测的。
另请注意,与 fork
不同,父级会暂停,直到子级退出或调用 execve
。
关于c - vfork 永不返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6676436/
是否可以使用克隆来模拟 vfrok 的行为?到目前为止我有 pid=clone(fn,cStack,SIGCHLD|CLONE_FS | CLONE_FILES | CLONE_VM | CLONE_
这是“APUE”第 8 章中的练习(练习 8.2,第 2 版)。所有描述是: Recall the typical arrangement of memory in Figure 7.6. Becau
我的问题是执行时父堆栈会发生什么? main() { f(); g(); } f() { vfork(); } g() { int blast[100],i; f
我在 C 中使用 vfork() 工作。我的程序运行良好,但我收到有关隐式声明的警告。 我的代码: if(vfork()==0){ ... } 我的警告是: 函数“vfork”的隐式声明 [-Wimp
因为 vfork 在与父进程相同的地址空间中创建子进程,当在子进程上调用 execv() 时,父进程如何恢复,因为 exec 加载文件并在相同的地址运行它 parent 和 child 的空间? 最佳
#include #include #include #include int createproc(); pid_t pid; int main() { createproc();
为什么这个程序永远不会返回并继续创建子进程? int main() { pid_t pid; int foo1 = 1, foo2 = 2; printf("before fo
考虑以下代码: int main() { int pid; pid=vfork(); if(pid==0) printf("child\n"); else prin
我有一个具有未定义行为的程序(vfork() 使用不当): #include #include #include int main ( int argc, char *argv[] ) {
我读到使用 vfork() 系统调用创建的新进程作为父进程地址空间中的线程执行,直到子线程不调用 exit() 或 exec() 系统调用,父进程被阻塞。所以我用 vfork() 系统调用写了一个程序
下面的代码永远不会结束。这是为什么? #include #include #include #define SIZE 5 int nums[SIZE] = {0, 1, 2, 3, 4}; in
子进程在vfork()后如何修改或读取父进程中的数据?子进程是否可以直接访问父进程中声明的变量? 我有一个创建一些数据结构的过程。然后我需要 fork 一个子进程需要读/写这些数据结构。子进程将是一个
我在玩 fork/vfork 函数,有些东西让我感到困惑。在史蒂文斯的书中写道: Note in Figure 8.3 that we call _exit instead of exit. As w
本文研究的主要是Linux进程函数fork(),vfork(),execX()的相关内容,具体介绍如下。 函数fork() fork函数:创建一个新进程 1、fork()成功后,将为子进程
使用 vfork() 创建的进程是否具有与创建者进程(即父进程)相同的权限级别? 示例:如果我以 root 身份运行进程,vfork() 子进程是否拥有相同的执行权限? 最佳答案 fork() 的开放
#include #include #include #include #include #include int main() { pid_t child_pid = vfork();
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 6 年前。 Improve th
我想用 vfork() 编写一个程序,父级创建 n 个子级,我想用参数插入儿子的数量。然后我想计算儿子的数量,例如: ./sum 4 The sum of the child: 10 The sum
我必须创建一个程序: 索要电话号码 创建子进程(使用 vfork) 计算平方根(在子进程中) 显示父进程的平方根 这是我的代码 #include #include #include #inclu
这是我的代码...我不知道为什么会出现错误段...有人可以向我解释原因吗? #include #include // Required by for routine #include #incl
我是一名优秀的程序员,十分优秀!