⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test9_3.txt

📁 人民邮电出版社的经典著作《Linux下的C编程》配套源码!初学者的必备!共14章
💻 TXT
字号:
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>

int main(void)
{
    	pid_t pid;
    	if((pid=vfork())<0)
	{
    		printf("fork error! \n");
    		exit(1);
	}
	else if(pid==0)
	{
    		printf("Child process PID: %d.\n",getpid());
    		setenv("PS1", "CHILD\\$", 1);
		printf("Process%4d: calling exec.\n",getpid());
		if(execl("/bin/sh", "/bin/sh", "arg2", NULL)<0)
		{
    			printf("Process%4d: execle error!\n",getpid());
    			exit(0);
		}
		printf("Process%4d: You should never see this because the child is already gone. \n", 
			getpid());
            	printf("Precess%4d: The child process is exiting.");
	}
	else
	{
    		printf("Parent process PID:%4d.\n", getpid());
    		printf("Process%4d: The parent has fork process %d.\n", pid);
    		printf("Process%4d: The child has called exec or has exited.\n", getpid());
	}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -