#include <unistd.h>
#include <stdlib.h>
#include <time.h>
void main(void)
{
printf("本程序PID=%d\n",getpid());
printf("本程序父进程PPID=%d\n",getppid());//bash;
int count=0;
pid_t pid;
//fork,//子进程的数据空间、堆栈空间都是父进程的拷贝;父子进程运行次序不确定
pid=fork();
count++;
if(pid<0) printf("error in fork!\n");
else if(pid==0)
{
sleep(1);
printf("fork,i am the child process,ID is %d,count=%d\n",getpid(),count);
}
else
{
wait(NULL);//wait(),等待子进程退出;
printf("fork,i am the parent process,ID is %d,count=%d\n",getpid(),count);
}
//vfork,//子进程的数据空间、堆栈空间与父进程的共享;子进程先,父进程后运
pid=vfork();
count++;
if(pid<0) printf("error in fork!\n");
else if(pid==0)
{
printf("vfork,i am the child process,ID is %d,count=%d\n",getpid(),count);
sleep(1);
}
else
{
printf("vfork,i am the parent process,ID is %d,count=%d\n",getpid(),count);
sleep(1);
}
char *argv[]={"ls","-al","/etc/passwd",NULL};
execl("/bin/ls","ls","-al","/etc/passwd",(char*)0);
execlp("ls","ls","-al","/etc/passwd",NULL);
execv("/bin/ls",argv);
system("ls -al /etc/passwd");
exit(0);
}
linux control
最新推荐文章于 2023-03-10 11:23:26 发布