• 信息安全系统设计基础第十二周学习总结


    第八章代码

    exec1


    `#include <stdio.h>
     #include <unistd.h>
     int main()
     {
         char   *arglist[3];
         arglist[0] = "ls";
         arglist[1] = "-l";
         arglist[2] = 0 ;//NULL
         printf("* * * About to exec ls -l
    ");
         execvp( "ls" , arglist );
         printf("* * * ls is done. bye");
         return 0;
     }
    
    • exec系统调用会从当前进程中把当前程序的机器指令清除,然后在空的进程中载入调用时指定的程序代码,最后运行这个新的程序。

    • execvp函数:从PATH 环境变量所指的目录中查找符合参数file 的文件名,找到后便执行该文件,然后将第二个参数argv传给该欲执行的文件,如果执行成功则函数不会返回,执行失败则直接返回-1,失败原因存于errno中。

    所需头文件 #include <unistd.h>

    函数原形 execvp(const char *file,char *const argv[])

    函数返回值 1:出错  

    exec2

    `#include <stdio.h>
     #include <unistd.h>
     int main()
     {
          char  *arglist[3];
          arglist[0] = "ls";
          arglist[1] = "-l";
          arglist[2] = 0 ;
          printf("* * * About to exec ls -l
    ");
          execvp( arglist[0] , arglist );
          printf("* * * ls is done. bye
    ");
     }`
    

    区别:exevp函数的第一个参数,exec1传的是ls,exec2直接用的arglist[0],但是二者等价,运行结果相同。

    exec3

    `#include <stdio.h>
     #include <unistd.h>
     int main()
     {
        char    *arglist[3];
        char    *myenv[3];
        myenv[0] = "PATH=:/bin:";
        myenv[1] = NULL;
        arglist[0] = "ls";
        arglist[1] = "-l";
        arglist[2] = 0 ;
        printf("* * * About to exec ls -l
    ");
         // execv( "/bin/ls" , arglist );
         // execvp( "ls" , arglist );
         // execvpe("ls" , arglist, myenv);
        execlp("ls", "ls", "-l", NULL);
        printf("* * * ls is done. bye
    ");
     }`
    
    • execlp函数的作用:调用execlp函数进行命令的执行,execlp函数是可变参数函数,第一个参数需要设置系统环境变量中所能获取的命令文件,或者自己设置绝对路径的命令文件,最后一个参数必须设置为空,以便标记这个函数的参数已经设置完成,中间的所有参数设置为第一个命令的参数,完成输出重定向。
    • 返回值:如果执行成功则函数不会返回,执行失败则直接返回-1,失败原因存于errno 中。
    • 综上,exec函数名及对应含义

    forkdemo1

    `#include <stdio.h>
     #include    <sys/types.h>
     #include    <unistd.h>
     int main()
     {
         int    ret_from_fork, mypid;
         mypid = getpid();             
         printf("Before: my pid is %d
    ", mypid);
         ret_from_fork = fork();
         sleep(1);
         printf("After: my pid is %d, fork() said %d
    ",
            getpid(), ret_from_fork);
          return 0;
     }`

    效果:先打印进程pid,然后调用fork函数生成子进程,休眠一秒后再次打印进程id,这时父进程打印子进程pid,子进程返回0

    forkdemo2

    #include <stdio.h>
    #include <unistd.h>

    int main()
    {
    printf("before:my pid is %d ", getpid() );
    fork();
    fork();
    printf("aftre:my pid is %d ", getpid() );

    return 0;
    }

    效果:调用两次fork,共产生四个子进程,打印出四个aftre并输出

    forkdemo3

    #include <stdio.h>
    #include    <stdlib.h>
    #include    <unistd.h>

    int main()
    {
    int fork_rv;

    printf("Before: my pid is %d ", getpid());

    fork_rv = fork();

    if ( fork_rv == -1 )
    perror("fork");
    else if ( fork_rv == 0 ){ 
    printf("I am the child.  my pid=%d ", getpid());

    exit(0);
    }
    else{
    printf("I am the parent. my child is %d ", fork_rv);
    exit(0);
    }

    return 0;
    }

    效果:fork产生子进程,父进程返回子进程pid,不为0,所以输出父进程的那句话,子进程返回0,所以会输出子进程

    forkdemo4

    #include <stdio.h>
    #include    <stdlib.h>
    #include    <unistd.h>

    int main()
    {
    int fork_rv;

    printf("Before: my pid is %d ", getpid());

    fork_rv = fork(); /* create new process */

    if ( fork_rv == -1 ) /* check for error */
    perror("fork");

    else if ( fork_rv == 0 ){ 
    printf("I am the child.  my pid=%d ", getpid());
    printf("parent pid= %d, my pid=%d ", getppid(), getpid());
    exit(0);
    }

    else{
    printf("I am the parent. my child is %d ", fork_rv);
    sleep(10);
    exit(0);
    }

    return 0;
    }

    效果:先打印进程pid,然后fork创建子进程,父进程返回子进程pid,所以输出parent一句,休眠十秒;子进程返回0,所以输出child和后一句。

    forkgdb

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>

    int  gi=0;
    int main()
    {
    int li=0;
    static int si=0;
    int i=0;

    pid_t pid = fork();
    if(pid == -1){
    exit(-1);
    }
    else if(pid == 0){
    for(i=0; i<5; i++){
    printf("child li:%d ", li++);
    sleep(1);
    printf("child gi:%d ", gi++);
    printf("child si:%d ", si++);
    }
    exit(0);

    }
    else{
    for(i=0; i<5; i++){
    printf("parent li:%d ", li++);
    printf("parent gi:%d ", gi++);
    sleep(1);
    printf("parent si:%d ", si++);
    }
    exit(0);

    }
    return 0;
    }

    效果:父进程打印是先打印两句,然后休眠一秒,然后打印一句,子进程先打印一句,然后休眠一秒,然后打印两句。并且这两个线程是并发的,所以可以看到在一个线程休眠的那一秒,另一个线程在执行,并且线程之间相互独立互不干扰。

    综上, fork()函数通过系统调用创建一个与原来进程几乎完全相同的进程,也就是两个进程可以做完全相同的事,但如果初始参数或者传入的变量不同,两个进程也可以做不同的事。一个进程调用fork()函数后,系统先给新的进程分配资源,例如存储数据和代码的空间。然后把原来的进程的所有值都复制到新的新进程中,只有少数值与原来的进程的值不同。相当于克隆了一个自己。

    fork函数特性:被调用一次,却返回两次,它可能有三种不同的返回值:
        1)在父进程中,fork返回新创建子进程的进程ID;
        2)在子进程中,fork返回0;
        3)如果出现错误,fork返回一个负值;

    fork函数执行完毕后,如果创建新进程成功,则出现两个进程,一个是子进程,一个是父进程。在子进程中,fork函数返回0,在父进程中,fork返回新创建子进程的进程ID。我们可以通过fork返回的值来判断当前进程是子进程还是父进程。

     

     

    psh1

     

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include    <unistd.h>

    #define MAXARGS 20
    #define ARGLEN 100

    int execute( char *arglist[] )
    {
    execvp(arglist[0], arglist);
    perror("execvp failed");
    exit(1);
    }

    char * makestring( char *buf )
    {
    char *cp;

    buf[strlen(buf)-1] = '';
    cp = malloc( strlen(buf)+1 );
    if ( cp == NULL ){
    fprintf(stderr,"no memory ");
    exit(1);
    }
    strcpy(cp, buf);
    return cp;
    }

    int main()
    {
    char *arglist[MAXARGS+1];
    int numargs;
    char argbuf[ARGLEN];

    numargs = 0;
    while ( numargs < MAXARGS )
    {
    printf("Arg[%d]? ", numargs);
    if ( fgets(argbuf, ARGLEN, stdin) && *argbuf != ' ' )
    arglist[numargs++] = makestring(argbuf);
    else
    {
    if ( numargs > 0 ){
    arglist[numargs]=NULL;
    execute( arglist );
    numargs = 0;
    }
    }
    }
    return 0;
    }

    作用:代码相当于你输入要执行的指令,回车表示输入结束,然后输入的每个参数对应到函数中,再调用对应的指令。

    psh2

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

    #define MAXARGS 20
    #define ARGLEN 100

    char *makestring( char *buf )
    {
    char *cp;

    buf[strlen(buf)-1] = '';
    cp = malloc( strlen(buf)+1 );
    if ( cp == NULL ){
    fprintf(stderr,"no memory ");
    exit(1);
    }
    strcpy(cp, buf);
    return cp;
    }

    void execute( char *arglist[] )
    {
    int pid,exitstatus;

    pid = fork();
    switch( pid ){
    case -1:
    perror("fork failed");
    exit(1);
    case 0:
    execvp(arglist[0], arglist);
    perror("execvp failed");
    exit(1);
    default:
    while( wait(&exitstatus) != pid )
    ;
    printf("child exited with status %d,%d ",
    exitstatus>>8, exitstatus&0377);
    }
    }

    int main()
    {
    char *arglist[MAXARGS+1];
    int numargs;
    char argbuf[ARGLEN];

    numargs = 0;
    while ( numargs < MAXARGS )
    {
    printf("Arg[%d]? ", numargs);
    if ( fgets(argbuf, ARGLEN, stdin) && *argbuf != ' ' )
    arglist[numargs++] = makestring(argbuf);
    else
    {
    if ( numargs > 0 ){
    arglist[numargs]=NULL;
    execute( arglist );
    numargs = 0;
    }
    }
    }
    return 0;
    }

    作用:psh1对比,多了循环判断,不退出的话就会一直要你输入指令。

    testbuf1

    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
    printf("hello");
    fflush(stdout);
    while(1);
    }

    效果:先输出hello,然后换行。之后不退出。

    testbuf2

    #include <stdio.h>
    int main()
    {
    printf("hello ");
    while(1);
    }

    效果:先输出hello,然后换行。之后不退出。

    (fflush(stdout)和换行符 效果相同)

    testbuf3

    #include <stdio.h>

    int main()
    {
    fprintf(stdout, "1234", 5);
    fprintf(stderr, "abcd", 4);
    }

    效果:将内容格式化输出到标准错误、输出流中。

    testpid

    #include <stdio.h>
    #include <unistd.h>

    #include <sys/types.h>

    int main()
    {
    printf("my pid: %d  ", getpid());
    printf("my parent's pid: %d  ", getppid());
    return 0;
    }

    作用:输出当前进程pid和当前进程的父进程的pid 

    testsystem

    #include <stdlib.h>

    int main ( int argc, char *argv[] )
    {

    system(argv[1]);
    system(argv[2]);
    return EXIT_SUCCESS;
    } /* ----------  end of function main  ---------- */

    system(执行shell 命令):
             相关函数:forkexecvewaitpidpopen
             表头文件:i nclude<stdlib.h>
             定义函数:int system(const char * string);
             函数说明:system()会调用fork()产生子进程,由子进程来调用/bin/sh-c string来执行参数string字符串所代表的命令,此命>令执行完后随即返回原调用的进程。在调用system()期间SIGCHLD 信号会被暂时搁置,SIGINTSIGQUIT 信号则会被忽略。
    返回值:=-1:出现错误 
              =0:调用成功但是没有出现子进程 
              >0:成功退出的子进程的id
    总的来说,system函数执行shell命令,也就是向dos发送一条指令。这里是后面可以跟两个参数,然后向dos发送这两个命令,分别执行。

    waitdemo1

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

    #define DELAY 4

    void child_code(int delay)
    {
    printf("child %d here. will sleep for %d seconds ", getpid(), delay);
    sleep(delay);
    printf("child done. about to exit ");
    exit(17);
    }

    void parent_code(int childpid)
    {
    int wait_rv=0; /* return value from wait() */
    wait_rv = wait(NULL);
    printf("done waiting for %d. Wait returned: %d ", 
    childpid, wait_rv);
    }
    int main()
    {
    int  newpid;
    printf("before: mypid is %d ", getpid());
    if ( (newpid = fork()) == -1 )
    perror("fork");
    else if ( newpid == 0 )
    child_code(DELAY);
    else
    parent_code(newpid);

    return 0;
    }

    效果:如果有子进程,则终止子进程,成功返回子进程pid

    waitdemo2

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

    #define DELAY 10

    void child_code(int delay)
    {
    printf("child %d here. will sleep for %d seconds ", getpid(), delay);
    sleep(delay);
    printf("child done. about to exit ");
    exit(27);
    }

    void parent_code(int childpid)
    {
    int wait_rv;
    int child_status;
    int high_8, low_7, bit_7;

    wait_rv = wait(&child_status);
    printf("done waiting for %d. Wait returned: %d ", childpid, wait_rv);

    high_8 = child_status >> 8;     /* 1111 1111 0000 0000 */
    low_7  = child_status & 0x7F;   /* 0000 0000 0111 1111 */
    bit_7  = child_status & 0x80;   /* 0000 0000 1000 0000 */
    printf("status: exit=%d, sig=%d, core=%d ", high_8, low_7, bit_7);
    }

    int main()
    {
    int  newpid;

    printf("before: mypid is %d ", getpid());

    if ( (newpid = fork()) == -1 )
    perror("fork");
    else if ( newpid == 0 )
    child_code(DELAY);
    else
    parent_code(newpid);
    }

    效果:1来多了一个子进程的状态区分,把状态拆分成三块,exitsigcore

    综上,wait的函数原型是

    #include <sys/types.h> /* 提供类型pid_t的定义*/ 

    #include <sys/wait.h> 

    pid_t wait(int *status); 

    进程一旦调用了wait,就立即阻塞自己,由wait自动分析是否当前进程的某个子进程已经退出,如果让它找到了这样一个已经变成僵尸的子进程,wait就会收集这个子进程的信息,并把它彻底销毁后返回;如果没有找到这样一个子进程,wait就会一直阻塞在这里,直到有一个出现为止。



  • 相关阅读:
    2019.6.15刷题统计
    入门组完成情况
    2019.6.14刷题统计
    2019.6.13刷题统计
    绑定与非绑定方法 继承 继承与抽象 查找属性关系 派生与覆盖 访问父类的内容
    23 xml 面向对象
    day22 configparser模块 subprocsee模块 表格
    Python常用模块
    20.logging日志 re正则
    导入模块 包
  • 原文地址:https://www.cnblogs.com/20135239-yxlm/p/5008200.html
Copyright © 2020-2023  润新知