美文网首页
linux 进程相关

linux 进程相关

作者: 嵌入式工作 | 来源:发表于2018-07-13 17:33 被阅读0次

1概念

进程ID
• 每一个进程都有一个唯一的标识符,进程ID 简称pid
– 进程id 一般默认的最大值为32768,不过也是可以修改的,当然一般情
况下不需要这么做。如果当前进程是1000,那么下一个分配的进程就是
1001,它是严格线性分配的
– 除了init 进程,其它进程都是由其它进程创立的。创立新进程的进程叫父
进程,新进程叫子进程
• man 2 getpid
• man 2 getppid

2获取进程编号

进程ID
• 获取子进程的函数
• pid_t getpid(void)
– 参数:无
– 返回值:成功返回进程号
• 获取父进程的函数
• pid_t getppid(void);
– 参数:无
– 返回值:成功返回父进程

3exec运行程序

exec函数族
• linux中,可以使用exec函数族将程序载入内存,实现多个程序的运

man 3 exec
• exec函数族参数
– “l”和“v”表示参数是以列表还是以数组的方式提供的
– “p”表示这个函数的第一个参数是*path,就是以绝对路径来提供程序的
路径,也可以以当前目录作为目标
– “e”表示为程序提供新的环境变量

4 fork函数创建新的process

linux中,可以使用fork函数创建和当前进程一模一样的进程,叫子进
程,原来的进程叫父进程
创建进程函数
• pid_t fork(void);
– 参数:无
– 返回值:执行成功,子进程pid 返回给父进程,0 返回给子进程;出现错
误-1,返回给父进程。执行失败的唯一情况是内存不够或者id 号用尽,
不过这种情况几乎很少发生

fork创建新进程
• fork函数(看了例子之后再回头理解一次下面几句话)
– 系统函数fork 调用成功,会创建一个新的进程,它几乎会调用差不多完
全一样的fork 进程
– 子进程的pid 和父进程不一样,是新分配的
– 子进程的ppid 会设置为父进程的pid,也就是说子进程和父进程各自的
“父进程”不一样
– 子进程中的资源统计信息会清零
– 挂起的信号会被清除,也不会被继承
– 所有文件锁也不会被子进程继承

5应用程序举例

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

#if 0
extern char **environ;

int execl(const char *path, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execle(const char *path, const char *arg,
..., char * const envp[]);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
int execvpe(const char *file, char *const argv[],
char *const envp[]);
#endif


int main(int argv ,char*argc[])
{
        char *arg[] = {"ls","-a",NULL};
    printf("\n=================show how to user getpid==============================================\n");
    printf("getpid() returns the process ID of the calling process pid:%d \n",getpid());
    printf("getppid()returns the process ID of the parent of the calling process pid:%d \n",getppid());
    printf("//======================================================================================\n");
    
    
    
    
    printf("\n=================show how to user fork excel ==============================================\n");

    
    if(fork() == 0)
    {
        //in child1
        printf("fork1 is OK;execl\n");
        
        if(execl("/bin/ls","ls","-a",NULL) == -1)
        {
            perror("execl error");
            exit(1);
        }
    }
    
    usleep(20000);
    if(fork() == 0)
    {
        //in child2
        printf("fork2 is OK;execlp\n");
        
        if(execlp("ls","ls","-a",NULL) == -1)
        {
            perror("execlp error");
            exit(1);
        }
    }

    usleep(20000);
    if(fork() == 0)
    {
        //in child3
        printf("fork3 is OK;execle\n");
        
        if(execle("/bin/ls","ls","-a",NULL,NULL) == -1)
        {
            perror("execle error");
            exit(1);
        }
    }

        usleep(20000);
    if(fork() == 0)
    {
        //in child4
        printf("fork4 is OK;execv\n");
        
        if(execv("/bin/ls",arg) == -1)
        {
            perror("execv error");
            exit(1);
        }
    }
    
    
        usleep(20000);
    if(fork() == 0)
    {
        //in child5
        printf("fork5 is OK;execvp\n");
        
        if(execvp("ls",arg) == -1)
        {
            perror("execvp error");
            exit(1);
        }
    }
    
    
        usleep(20000);
    if(fork() == 0)
    {
        //in child6
        printf("fork6 is OK;execvpe\n");
        
        if(execve("bin/ls",arg,NULL) == -1)
        {
            perror("execvpe error");
            exit(1);
        }
    }
    
    //加入小延时可以避免发生混乱的情况
    usleep(20000);
    printf("//======================================================================================\n");
    
    
    
    
}



相关文章

  • linux 进程相关

    1概念 进程ID• 每一个进程都有一个唯一的标识符,进程ID 简称pid– 进程id 一般默认的最大值为32768...

  • linux进程相关

    列出相关进程 ps -ef | grep xxx ps -ef | grep nginx UID P...

  • Linux进程相关

    1. 进程管理事例情况 当一个程序编写得不完善,导致在内存中产生一个有问题的进程时,需要找到此进程并移除(kill...

  • linux如何查看端口被哪个进程占用,并杀死相关进程?

    linux如何查看端口被哪个进程占用,并杀死相关进程? 本文介绍linux如何查看端口被哪个进程占用的方法: 1、...

  • Linux查看进程、杀死进程、启动进程等常用命令

    关键字: linux 查进程、杀进程、起进程 查进程ps 命令查找与进程相关的PID号:ps a 显...

  • Linux内核进程管理

    一、Linux进程管理基础 1.1 进程的概念 进程:处于执行期的程序已经相关资源的总称。相关资源如:打开的文件、...

  • Linux Shell脚本经典案例(二)

    11.统计 /proc 目类下 Linux 进程相关数量信息,输出总进程数,running 进程数,stoped ...

  • linux进程内存相关

    linux内存寻址 3种地址:虚拟地址、物理地址、逻辑地址物理地址:内存的电路地址,对应内存地址线上的高低电平,物...

  • linux进程相关命令

    进程相关的命令 ps 显示当前系统中由该用户运行的进程列表(掌握)kill 输出特定的信号给指定的进程,并根据该信...

  • 进程和计划任务详解(一)

    学习内容: 1、进程相关知识(用户空间、内核空间、进程创建、进程优先级、进程内存)2、Linux进程查看及管理工具...

网友评论

      本文标题:linux 进程相关

      本文链接:https://www.haomeiwen.com/subject/xhnjpftx.html