Linux学习笔记之---进程

6:进程控制
    6.1:    读取进程ID号
            #include <sys/types.h>
            #include <unistd.h>
            pid_t   getpid();       当前进程ID;
            pid_t   getpgrp();      当前进程组ID;
            pid_t   getppid();      父进程ID;
            pid_t是进程标识类型,可以用long或者int代替
    6.2     读取进程用户ID号
            #include <unistd.h>
            uid_t   getuid();       实际用户ID号
            uid_t   geteuid();      有效用户ID号
            gid_t   getgid();       实际组ID号
            gid_t   getegid();      有效组ID号 
    6.3     环境变量的获取:
            a:
            extern char **environ 导入所有变量
            
            b:
            #include <stdlib.h>
            char *getenv(char *name);       返回变量name的值
            int putenv(const char *string)
    6.4     进程的创建
            #include <unistd.h>
            pid_t fork();   成功返回子进程标识号,失败返回-1.
    6.5     新程序的执行
            1.exec函数族
            #include <unistd.h>
            int execl(const char *path,const char *arg0, ... ,(char *)0);
            int execle(const char *path,const char *arg0,...,(char *)0,char *constenvp[]);
            int execlp(const char *file,const char *arg0,...,(char *)0);
            int execv(const char *path,const char *argv[]);
            int execve(const char *path,const char *argv[],const char *envp[]);
            int execvp(const char *file,const char *argv[]);
            extern char **environ;
            
            file和path指定新程序,path必须为绝对路径,file可以为相对路径
            const char *arg0, ... ,(char *)0, 或 const char *argv指定程序执行的命令行
            调用失败返回-1
            
            例:通过以上函数执行 "ls -l /u"
            ls的决定路径是/bin/ls,命令行参数分别为‘-l‘,’/u‘.各函数调用方式如下
            
            extern char **environ;
            char *argv[] = {"-l", "/u", 0};
            execl("/bin/ls", "-l", "/u", 0};
            execle("/bin/ls", "-l", "/u", 0, environ);
            execlp("ls", "-l", "/u", 0);
            execv("/bin/ls", argv);
            execve("/bin/ls",argv,environ);
            execvp("ls",argv);
            
            2.fork-exec模型            
            #include <unistd.h>
            #include <stdio.h>
            int main()
            {   
                pid_t pid;
                if ((pid = fork()) == 0) //子进程
                {   
                    execl("/bin/ls","-l","/home",0);
                }
                else if (pid > 0)       //父进程
                {   
                    ...;
                }
                return 0;
            }


            3.vfork-exec模型
            #include <unistd.h>
            pid_t vfork();      父进程会被阻塞,直到子进程结束
        
            4.system模型
            #include <stdlib.h>
            int system(char *string);
            阻塞调用子进程,执行string中的shell命令
            
    6.6     进程的休眠
            #include <unistd.h>
            unsigned int sleep(unsigned int seconds);            
            
    6.7     进程的终止
            #include <stdlib.h>
            void exit (int status); (结束自己,子进程调用)
            
    6.8     进程的同步
            #incldue <sys/types.h>
            #incldue <sys/wait.h>
            pid_t wait (int *status);
            pid_t waitpid(pid_t pid, int *status,int options);        
            
            wait挂起调用它的进程,直到它的任一子进程退出或收到一个不能忽略的信号为止
            如果在父进程执行wait调用前就已经有子进程退出,则立即返回,
            成功调用后返回结束子进程的id,否则返回-1,status返回子进程退出的状态:
            
            子进程终止原因         status低8位          status高8位
            调用exit退出           0                     exit的参数值的低8位
            接收信号退出          引起终止的信号        0
        
            waitpid是wait的加强版,用于多个子进程的操作
            pid取值情况表
            
            pid>0           等待进程标识号为pid的子进程结束
            pid=0           等待进程标识号等于父进程组标识号的子进程结束
            pid=-1          等待任意子进程结束
            pid<-1          等待进程组标识号等于pid的绝对值的子进程结束
            
            options的取值
            WNOHANG     非阻塞调用waitpid,没有符合条件的子进程结束时,立即返回
            WUNTRACED   如果在父进程执行waitpid前就有子进程退出,立即返回
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值