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


    process.tar代码

    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;
    }

    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的区别就在于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函数:从PATH 环境变量所指的目录中查找符合参数file的文件名,找到后便执行该文件,然后将第二个以后的参数当做该文件的argv[0]、argv[1]……,最后一个参数必须用空指针(NULL)作结束。如果用常数0来表示一个空指针,则必须将它强制转换为一个字符指针,否则将它解释为整形参数,如果一个整形数的长度与char * 的长度不同,那么exec函数的实际参数就将出错。如果函数调用成功,进程自己的执行代码就会变成加载程序的代码,execlp()后边的代码也就不会执行了.

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

    也就是说,这个代码指定了环境变量,然后依然执行了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.

    运行示例:

     

     

    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();		/* 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());

    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;
    }

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

    运行示例:

     

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

    效果同上。结论: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。

    运行示例:

     

    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发送一条指令。这里是后面可以跟两个参数,然后向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 ", 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来就是多了一个子进程的状态区分,把状态拆分成三块,exit,sig和core。

    运行示例:

     

    参考资料:1.《深入理解计算机系统》第八章

         2.课程交流群中代码压缩文件process.tar

         3.20125202博客

  • 相关阅读:
    2.iptables规则管理
    十二.zabbix自动化添加主机
    ④.触发器
    ⑨.zabbix监控phpfpm
    动态dns
    ③.监控项
    十一.zabbix web监测
    ②.添加监控主机
    13.ansible get_url unarchive archive
    Planning in Factored State and Action Spaces with Learned Binarized Neural Network Transition Models
  • 原文地址:https://www.cnblogs.com/20135310cqr/p/5005181.html
Copyright © 2020-2023  润新知