linux control

#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);
}
     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值