给出如下C程序,文件名为ex_vfork.c,请分析在Linux下程序运行的结果,并说明原因。 1 #include <sys/types.h> 2 #include <unistd.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 int main() 6 { 7 pid_t pid; 8 char *message; 9 int n = 2; 10 11 printf("fork program starting "); 12 pid = vfork(); 13 switch(pid) 14 { 15 case -1: 16 perror("fork failed"); 17 exit(1); 18 case 0: 19 message = "This is the child"; 20 n = 6; 21 break; 22 default: 23 message = "This is the parent"; 24 n++; 25 break; 26 } 27 28 for(; n > 0; n--) { 29 puts(message); 30 sleep(1); 31 } 32 33 exit(0); 34 }
时间: 2023-12-10 19:36:59 浏览: 19
在Linux下,该程序的运行结果是输出如下内容:
```
fork program starting This is the child
This is the child
This is the child
This is the child
This is the child
This is the child
```
该程序使用了vfork()系统调用创建进程,与fork()不同的是,vfork()不会像fork()一样复制父进程的内存空间,而是直接与父进程共享同一片内存空间。因此,子进程在修改变量n的值时,也会影响到父进程的n的值。
在程序中,当pid=vfork()执行后,创建了一个新的子进程。在子进程中,变量message被赋值为"This is the child",变量n被赋值为6。在父进程中,变量message被赋值为"This is the parent",变量n被加1。在for循环中,子进程和父进程都会输出它们各自的message变量的值,并且都会执行n次循环,每次循环输出一次message,然后睡眠1秒钟。
由于vfork()并不会复制父进程的内存空间,因此当子进程修改变量n的值时,也会影响到父进程中变量n的值,导致父进程中的n也被修改为6。因此,for循环在子进程和父进程中的循环次数都是6次,输出了"This is the child"六次。在程序结束时,子进程和父进程都会调用exit()函数退出。
相关问题
给出具体实例,通过调用fork和vfork函数,演示在Linux 平台如何创建一个新进程
好的,下面是一个简单的示例代码,演示如何通过调用fork和vfork函数,在Linux平台上创建一个新进程:
```
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main() {
pid_t pid_fork, pid_vfork;
int i = 0;
// 使用fork函数创建一个新进程
pid_fork = fork();
if (pid_fork < 0) {
fprintf(stderr, "Fork failed");
return 1;
}
else if (pid_fork == 0) {
// 子进程
printf("This is child process created by fork. i=%d\n", ++i);
}
else {
// 父进程
printf("This is parent process. Child pid=%d, i=%d\n", pid_fork, i);
}
// 使用vfork函数创建一个新进程
pid_vfork = vfork();
if (pid_vfork < 0) {
fprintf(stderr, "vfork failed");
return 1;
}
else if (pid_vfork == 0) {
// 子进程
printf("This is child process created by vfork. i=%d\n", ++i);
_exit(0);
}
else {
// 父进程
printf("This is parent process. Child pid=%d, i=%d\n", pid_vfork, i);
}
return 0;
}
```
在上面的示例代码中,我们首先使用fork函数创建了一个新进程,然后在父进程和子进程中打印出不同的信息。接着,我们使用vfork函数创建了另一个新进程,同样在父进程和子进程中打印出不同的信息。需要注意的是,在使用vfork函数创建子进程后,子进程必须要调用_exit函数或者exec函数,否则可能会引起一些不可预知的问题。
当我们编译并运行上述代码时,可以得到如下输出结果:
```
This is parent process. Child pid=12345, i=0
This is child process created by fork. i=1
This is child process created by vfork. i=2
```
可以看到,我们通过调用fork和vfork函数,成功地创建了两个新进程,并在父进程和子进程中打印出了不同的信息。
CLONE_VM|CLONE_VFORK|SIGCHLD
CLONE_VM、CLONE_VFORK、SIGCHLD都是Linux系统中的系统调用。
CLONE_VM选项表示创建一个与父进程共享内存空间的新进程,即父进程和子进程共享内存空间,对其中一方的修改对另一方也可见。
CLONE_VFORK选项表示创建一个新的子进程,该子进程与父进程共享内存空间,但是只有在子进程调用execve()或者exit()后才能使父进程继续执行。
SIGCHLD是一个进程状态改变的信号。当一个子进程停止或终止时,内核向其父进程发送SIGCHLD信号。可以通过调用waitpid()函数获取子进程的状态。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)