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

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\n");
    execvp( "ls" , arglist );
    printf("* * * ls is done. bye");

    return 0;
}

本代码中:

arglist属于命令行参数,表示不定参数列表,表示后跟不定个参数。

这里使用execvp()函数

int execvp(const char* file, const char* argv[]);

argv列表最后一个必须是 NULL

execvp()会从PATH 环境变量所指的目录中查找符合参数file 的文件名,找到后便执行该文件,然后将第二个参数argv传给该欲执行的文件。

如果执行成功则函数不会返回,执行失败则直接返回-1,失败原因存于errno中。

运行结果:

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\n");
    execvp( arglist[0] , arglist );
    printf("* * * ls is done. bye\n");
}

exevp函数的第一个参数,exec2的参数是arglist[0],与exec1等价,运行结果相同。

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\n");
//    execv( "/bin/ls" , arglist );
//    execvp( "ls" , arglist );
//  execvpe("ls" , arglist, myenv);

    execlp("ls", "ls", "-l", NULL);
    printf("* * * ls is done. bye\n");
}

这里使用了execlp()函数,

#include<unistd.h>
int execlp(const char file, const char argv ...)

用法如下:

execlp("ps","ps","-au","-x",(char)0);

1.最后一个参数必须是(char)0, 如果不强制转换成char,就会自动转换成int 有未知的错误。

2.第一个参数是要运行的文件,会在环境变量PATH中查找file.

3.失败会返回-1, 成功无返回值,但是,会在当前进程运行,执行成功后,直接结束当前进程。可以在子进程中运行。 

4.第二个参数,是一个参数列表,如同在shell中调用程序一样,参数列表为0,1,2,3……因此,ps作为第0个参数,需要重复一遍。

这个代码指定了环境变量,然后依然执行了ls -l指令,成功后没有返回,所以最后一句话不会输出。运行结果同exec1。

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\n", mypid);
    ret_from_fork = fork();
    sleep(1);
    printf("After: my pid is %d, fork() said %d\n",
            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\n", getpid() );
    fork();
    fork();
    printf("aftre:my pid is %d\n", 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\n", 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\n", getpid());
    
        exit(0);
    }
    else{
        printf("I am the parent. my child is %d\n", 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\n", 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\n", getpid());
        printf("parent pid= %d, my pid=%d\n", getppid(), getpid());
        exit(0);
    }

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

    return 0;
}

该代码先打印进程pid,然后调用一个fork创建子进程,父进程返回子进程pid,所以输出父进程,休眠十秒;子进程返回0,所以输出子进程。

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\n", li++);
            sleep(1);
            printf("child gi:%d\n", gi++);
            printf("child si:%d\n", si++);
        }
        exit(0);
        
    }
    else{
        for(i=0; i<5; i++){
            printf("parent li:%d\n", li++);
            printf("parent gi:%d\n", gi++);
            sleep(1);
            printf("parent si:%d\n", si++);
        }
    exit(0);    
    
    }
    return 0;
}

该代码调用一次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] = '\0';        
    cp = malloc( strlen(buf)+1 );        
    if ( cp == NULL ){            
        fprintf(stderr,"no memory\n");
        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 != '\n' )
            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] = '\0';        
    cp = malloc( strlen(buf)+1 );        
    if ( cp == NULL ){            
        fprintf(stderr,"no memory\n");
        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\n",
                    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 != '\n' )
            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,然后换行。

此处用了fflush(stdout),在printf()后使用fflush(stdout)的作用是立刻将要输出的内容输出。 
当使用printf()函数后,系统将内容存入输出缓冲区,等到时间片轮转到系统的输出程序时,将其输出。 
使用fflush(out)后,立刻清空输出缓冲区,并把缓冲区内容输出。 

testbuf2

代码:

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

该程序也是先输出hello,然后换行。

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 \n", getpid());
    printf("my parent's pid: %d \n", getppid());
    return 0;
}

该代码作用是输出当前进程pid和当前进程的父进程的pid。

testpp

代码:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char **pp;
    pp[0] = malloc(20);

    return 0;
}

这里用一个二级指针,结果却输出了段错误。

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命令也就是像dos发送一条指令。

system("pause")可以实现冻结屏幕,便于观察程序的执行结果;system("CLS")可以实现清屏操作。而调用color函数可以改变控制台的前景色和背景。

返回值:

1、如果 system()在调用/bin/sh 时失败则返回127, 其他失败原因返回-1.。
2、若参数string 为空指针(NULL), 则返回非零值.
3、如果system()调用成功则最后会返回执行shell 命令后的返回值, 但是此返回值也有可能为system()调用/bin/sh 失败所返回的127, 因此最好能再检查errno 来确认执行成功.

这里是后面可以跟两个参数,然后向dos发送这两个命令,分别执行。

运行该代码时,输入ls和dir两个指令后,可以看到分别执行了。

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\n", getpid(), delay);
    sleep(delay);
    printf("child done. about to exit\n");
    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\n", 
            childpid, wait_rv);
}
int main()
{
    int  newpid;
    printf("before: mypid is %d\n", getpid());
    if ( (newpid = fork()) == -1 )
        perror("fork");
    else if ( newpid == 0 )
        child_code(DELAY);
    else
        parent_code(newpid);

    return 0;
}

先打印进程pid,然后fork创建子进程,休眠4秒,终止子进程,返回子进程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\n", getpid(), delay);
    sleep(delay);
    printf("child done. about to exit\n");
    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\n", 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\n", high_8, low_7, bit_7);
}

int main()
{
    int  newpid;

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

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

该程序比waitdemo1多了一个子进程的状态区分,把状态拆分成三块,exit,sig和core。

参考资料:

代码

百度百科

posted @ 2015-11-29 13:40  bonjourvivi  阅读(229)  评论(0编辑  收藏  举报