zoukankan      html  css  js  c++  java
  • 20145207《信息安全系统设计基础》第十二周学习总结

    我的代码终于检查通过了。但愿能多加些分数回来,这周我看是总结三周的总结,我先把网址打上。

    十一周: http://www.cnblogs.com/20145207lza/p/6107397.html

    十周 :http://www.cnblogs.com/20145207lza/p/6081248.html#3562176

    九周:http://www.cnblogs.com/20145207lza/p/6056976.html

    加上一些总结:

    sigactdemo

    #include    <stdio.h>
    #include    <unistd.h>
    #include    <signal.h>
    #define INPUTLEN    100
    void inthandler();  
    int main()
    {
        struct sigaction newhandler;    
        sigset_t blocked;   
        char x[INPUTLEN];
        newhandler.sa_handler = inthandler; 
        newhandler.sa_flags = SA_RESTART|SA_NODEFER|SA_RESETHAND;   
        sigemptyset(&blocked);  
        sigaddset(&blocked, SIGQUIT);   
        newhandler.sa_mask = blocked;   
        if (sigaction(SIGINT, &newhandler, NULL) == -1)
            perror("sigaction");
        else
            while (1) {
                fgets(x, INPUTLEN, stdin);
                printf("input: %s", x);
            }
        return 0;
    }
    void inthandler(int s)
    {
        printf("Called with signal %d
    ", s);
        sleep(s * 4);
        printf("done handling signal %d
    ", s);
    }
    • 参数结构sigaction定义如下

      struct sigaction {
          void (*sa_handler)(int);
          void (*sa_sigaction)(int, siginfo_t *, void *);
          sigset_t sa_mask;
          int sa_flags;
          void (*sa_restorer)(void);
      }
    • flag
      • SA_RESETHAND:当调用信号处理函数时,将信号的处理函数重置为缺省值SIG_DFL
      • SA_RESTART:如果信号中断了进程的某个系统调用,则系统自动启动该系统调用
      • SA_NODEFER :一般情况下, 当信号处理函数运行时,内核将阻塞该给定信号。但是如果设置SA_NODEFER标记, 那么在该信号处理函数运行时,内核将不会阻塞该信号
    • 函数sigaction

      int sigaction(int signum,const struct sigaction *act ,struct sigaction *oldact);
      • sigaction()会依参数signum指定的信号编号来设置该信号的处理函数。参数signum可以指定SIGKILL和SIGSTOP以外的所有信号。

    sigactdemo2

    #include <unistd.h>
    #include <signal.h>
    #include <stdio.h>
    
    void sig_alrm( int signo )
    {
        /*do nothing*/
    }
    
    unsigned int mysleep(unsigned int nsecs)
    {
        struct sigaction newact, oldact;
        unsigned int unslept;
    
        newact.sa_handler = sig_alrm;
        sigemptyset( &newact.sa_mask );
        newact.sa_flags = 0;
        sigaction( SIGALRM, &newact, &oldact );
    
        alarm( nsecs );
        pause();
    
        unslept = alarm ( 0 );
        sigaction( SIGALRM, &oldact, NULL );
    
        return unslept;
    }
    
    int main( void )
    {
        while( 1 )
        {
            mysleep( 2 );
            printf( "Two seconds passed
    " );
        }
        return 0;
    }
    • 每两秒输出一次

    sigdemo1

    #include    <stdio.h>
    #include    <signal.h>
    void    f(int);         
    int main()
    {
        int i;
        signal( SIGINT, f );        
        for(i=0; i<5; i++ ){        
            printf("hello
    ");
            sleep(2);
        }
    
        return 0;
    }
    
    void f(int signum)          
    {
        printf("OUCH!
    ");
    }
    • 连续输出五个hello,每两个间隔是两秒
    • 在这期间,每次输入的Ctrl+C都被处理成打印OUCH

    sigdemo2

    #include    <stdio.h>
    #include    <signal.h>
    
    main()
    {
        signal( SIGINT, SIG_IGN );
    
        printf("you can't stop me!
    ");
        while( 1 )
        {
            sleep(1);
            printf("haha
    ");
        }
    }
    • 一直输出haha,按Ctrl+C不能停止。Ctrl+x才行
    • SIG_DFL,SIG_IGN 分别表示无返回值的函数指针,指针值分别是0和1,这两个指针值逻辑上讲是实际程序中不可能出现的函数地址值。
      • SIG_DFL:默认信号处理程序
      • SIG_IGN:忽略信号的处理程序

    sigdemo3

    #include    <stdio.h>
    #include    <string.h>
    #include    <signal.h>
    #include    <unistd.h>
    
    #define INPUTLEN    100
    
    int main(int argc, char *argv[])
    {
        void inthandler(int);
        void quithandler(int);
        char input[INPUTLEN];
        int nchars;
    
        signal(SIGINT, inthandler);//^C 
        signal(SIGQUIT, quithandler);//^
    
        do {
            printf("
    Type a message
    ");
            nchars = read(0, input, (INPUTLEN - 1));
            if (nchars == -1)
                perror("read returned an error");
            else {
                input[nchars] = '';
                printf("You typed: %s", input);
            }
        }
        while (strncmp(input, "quit", 4) != 0);
        return 0;
    }
    void inthandler(int s)
    {
        printf(" Received signal %d .. waiting
    ", s);
        sleep(2);
        printf("  Leaving inthandler 
    ");
    }
    void quithandler(int s)
    {
        printf(" Received signal %d .. waiting
    ", s);
        sleep(3);
        printf("  Leaving quithandler 
    ");
    }
    • 多信号处理SIGX打断SIGX的情况

    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;
    }
    int execvp(const char file ,char const argv []);
    • execvp()会从PATH 环境变量所指的目录中查找符合参数file 的文件名,找到后便执行该文件,然后将第二个参数argv传给该欲执行的文件。
    • 如果执行成功则函数不会返回,执行失败则直接返回-1,失败原因存于errno中。
    • 在执行时exevp函数调用成功没有返回,所以没有打印“* * * ls is done. bye”

    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
    ");
    }
    • exec1传的是ls,exec2传送的是arglist[0],但运行结果是相同的。

    exer3

    #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
    ");
        execlp("ls", "ls", "-l", NULL);
        printf("* * * ls is done. bye
    ");
    }
    • int execlp(const char * file,const char * arg,....);
    • execlp()会从PATH 环境变量所指的目录中查找符合参数file的文件名,找到后便执行该文件,然后将第二个以后的参数当做该文件的argv[0]、argv[1]……,最后一个参数必须用空指针(NULL)作结束。
    • 指定了环境变量,然后依然执行了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
    ", 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。
    • 父进程通过调用fork函数创建一个新的运行子进程。
    • 调用一次,返回两次。一次返回到父进程,一次返回到新创建的子进程。

    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输出。

    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;
    }
    • 父进程打印是先打印两句,然后休眠一秒,然后打印一句,子进程先打印一句,然后休眠一秒,然后打印两句。并且这两个线程是并发的,所以可以看到在一个线程休眠的那一秒,另一个线程在执行,并且线程之间相互独立互不干扰。

    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;
    }
    • 依次你输入要执行的指令与参数,回车表示输入结束,然后输入的每个参数对应到函数中,再调用对应的指令。
    • 第一个是程序名,然后依次是程序参数。
    • 一个字符串,一个字符串构造参数列表argist,最后在数组末尾加上NULL
    • 将arglist[0]和arglist数组传给execvp。
    • 程序正常运行,execvp命令指定的程序代码覆盖了shell程序代码,并在命令结束之后退出,shell就不能再接受新的命令。

    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多了循环判断,不退出的话就可以一直保持在输入指令,并且对于子程序存在的状态条件。
    • 为了解决这个问题,程序通过调用fork来复制自己。
    • 调用fork函数之后内核的工作过程:

      分配新的内存块和内核数据结构
      复制原来的进程到新的进程
      向运行进程集添加新的进程
      将控制返回给两个进程

    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);
    }
    • 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。

    考试加油!

  • 相关阅读:
    Linux内核tracepoints
    Linux 设备驱动的固件加载
    Android HIDL HAL 接口定义语言详解
    Android HIDL 详解
    Android各版本对应的SDK版本
    ifconfig调用过程
    Input系统—启动篇
    Linux下2号进程的kthreadd--Linux进程的管理与调度
    rest_init函数分析(续)
    【python】-常用模块2
  • 原文地址:https://www.cnblogs.com/20145207lza/p/6130822.html
Copyright © 2011-2022 走看看