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

第十二周(11.23-11.29):
 

学习计时:共6小时

读书:

代码:

作业:

博客:

一、学习目标


掌握进程控制
掌握信号处理的方法
掌握管道和fifo进行进程间通信的方法

二、学习资源

 

 编译、运行、阅读、理解process.tar.gz压缩包中的代码

实验代码

 

代码说明均在代码里面

 exec1

 1 #include <stdio.h>
 2 #include <unistd.h>
 3 
 4 int main()
 5 {
 6     /*
 7     执行ls -l命令
 8     */
 9     char    *arglist[3];
10 
11     arglist[0] = "ls";
12     arglist[1] = "-l";
13     arglist[2] = 0 ;//NULL
14     printf("* * * About to exec ls -l\n");
15     execvp( "ls" , arglist );//进程控制函数,根据文件名找到对饮的可执行函数,并用它来取代调用进程的内容
16     printf("* * * ls is done. bye");
17 
18     return 0;
19 }

exec2

 1 #include <stdio.h>
 2 #include <unistd.h>
 3 
 4 /*
 5 和exec1一样,只是execvp第一个参数被替换成数组的首元素
 6 */
 7 int main()
 8 {
 9     char    *arglist[3];
10 
11     arglist[0] = "ls";
12     arglist[1] = "-l";
13     arglist[2] = 0 ;
14     printf("* * * About to exec ls -l\n");
15     execvp( arglist[0] , arglist );
16     printf("* * * ls is done. bye\n");
17 }

exec3

 1 #include <stdio.h>
 2 #include <unistd.h>
 3 
 4 int main()
 5 {
 6     /*
 7     和exce1一样
 8     */
 9     char    *arglist[3];
10     char    *myenv[3];
11     myenv[0] = "PATH=:/bin:";
12     myenv[1] = NULL;
13 
14     arglist[0] = "ls";
15     arglist[1] = "-l";
16     arglist[2] = 0 ;
17     printf("* * * About to exec ls -l\n");
18 //    execv( "/bin/ls" , arglist );
19 //    execvp( "ls" , arglist );
20 //  execvpe("ls" , arglist, myenv);
21 /*
22 从PATH 环境变量所指的目录中查找符合参数file的文件名,找到后便执行该文件.
23 然后将第二个以后的参数当做该文件的argv[0]、argv[1],最后一个参数必须用空指针(NULL)作结束。
24 */
25     execlp("ls", "ls", "-l", NULL);
26     printf("* * * ls is done. bye\n");
27 }

forkdemo1

 1 #include    <stdio.h>
 2 #include    <sys/types.h>
 3 #include    <unistd.h>
 4 int main()
 5 {
 6     /*
 7     fork()会产生一个和父进程一样的子进程
 8     父进程的fork()返回子进程的ID
 9     子进程的fork()返回0
10     */
11     int    ret_from_fork, mypid;
12     mypid = getpid();
13     printf("Before: my pid is %d\n", mypid);
14     ret_from_fork = fork();
15     sleep(1);
16     printf("After: my pid is %d, fork() said %d\n",
17             getpid(), ret_from_fork);
18 
19     return 0;
20 }

forkdemo2

 1 #include <stdio.h>
 2 #include <unistd.h>
 3 
 4 int main()
 5 {
 6     /*
 7     这里创建了两个fork(),故有一个父进程 三个子进程
 8     子进程只能打印第二个printf
 9     */
10     printf("before:my pid is %d\n", getpid() );
11     fork();
12     fork();
13     printf("aftre:my pid is %d\n", getpid() );
14 
15     return 0;
16 }

forkdemo3

 1 #include    <stdio.h>
 2 #include    <stdlib.h>
 3 #include    <unistd.h>
 4 
 5 int main()
 6 {
 7     /*
 8     打印父进程和子进程的ID
 9     */
10     int    fork_rv;
11 
12     printf("Before: my pid is %d\n", getpid());
13 
14     fork_rv = fork();        /* create new process    */
15 
16     if ( fork_rv == -1 )        /* check for error    */
17         perror("fork");
18     else if ( fork_rv == 0 ){
19         printf("I am the child.  my pid=%d\n", getpid());
20 
21         exit(0);
22     }
23     else{
24         printf("I am the parent. my child is %d\n", fork_rv);
25         exit(0);
26     }
27 
28     return 0;
29 }

forkdemo4

 1 #include    <stdio.h>
 2 #include    <stdlib.h>
 3 #include    <unistd.h>
 4 
 5 int main()
 6 {
 7     /*
 8     通过getppid查看父进程ID
 9     */
10     int    fork_rv;
11 
12     printf("Before: my pid is %d\n", getpid());
13 
14     fork_rv = fork();        /* create new process    */
15 
16     if ( fork_rv == -1 )        /* check for error    */
17         perror("fork");
18 
19     else if ( fork_rv == 0 ){
20         printf("I am the child.  my pid=%d\n", getpid());
21         printf("parent pid= %d, my pid=%d\n", getppid(), getpid());
22         exit(0);
23     }
24 
25     else{
26         printf("I am the parent. my child is %d\n", fork_rv);
27         sleep(10);
28         exit(0);
29     }
30 
31     return 0;
32 }

forkgdb

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <unistd.h>
 4 
 5 int  gi=0;
 6 /*
 7 由于sleep的存在,父进程在si会停1秒。而子进程会开始,在li之后又会停止,父进程继续运行,如此循环
 8 */
 9 int main()
10 {
11     int li=0;
12     static int si=0;
13     int i=0;
14 
15     pid_t pid = fork();
16     if(pid == -1){
17         exit(-1);
18     }
19     else if(pid == 0){
20         for(i=0; i<5; i++){
21             printf("child li:%d\n", li++);
22             sleep(1);
23             printf("child gi:%d\n", gi++);
24             printf("child si:%d\n", si++);
25         }
26         exit(0);
27 
28     }
29     else{
30         for(i=0; i<5; i++){
31             printf("parent li:%d\n", li++);
32             printf("parent gi:%d\n", gi++);
33             sleep(1);
34             printf("parent si:%d\n", si++);
35         }
36     exit(0);
37 
38     }
39     return 0;
40 }

psh1

 1 #include    <stdio.h>
 2 #include    <stdlib.h>
 3 #include    <string.h>
 4 #include    <unistd.h>
 5 
 6 #define    MAXARGS        20
 7 #define    ARGLEN        100
 8 
 9 /*
10 调用execvp函数,通过调用数组第一位为文件,数组作为可执行函数
11 */
12 int execute( char *arglist[] )
13 {
14     execvp(arglist[0], arglist);
15     perror("execvp failed");
16     exit(1);
17 }
18 
19 char * makestring( char *buf )
20 {
21     char    *cp;
22 
23     buf[strlen(buf)-1] = '\0';
24     cp = malloc( strlen(buf)+1 );
25     if ( cp == NULL ){
26         fprintf(stderr,"no memory\n");
27         exit(1);
28     }
29     strcpy(cp, buf);
30     return cp;
31 }
32 
33 int main()
34 {
35     char    *arglist[MAXARGS+1];
36     int        numargs;
37     char    argbuf[ARGLEN];
38 
39     numargs = 0;
40     while ( numargs < MAXARGS )
41     {
42         printf("Arg[%d]? ", numargs);
43         if ( fgets(argbuf, ARGLEN, stdin) && *argbuf != '\n' )
44             arglist[numargs++] = makestring(argbuf);
45         else
46         {
47             if ( numargs > 0 ){
48                 arglist[numargs]=NULL;
49                 execute( arglist );
50                 numargs = 0;
51             }
52         }
53     }
54     return 0;
55 }

psh2

 1 #include    <stdio.h>
 2 #include    <stdlib.h>
 3 #include    <string.h>
 4 #include    <sys/types.h>
 5 #include    <sys/wait.h>
 6 #include    <unistd.h>
 7 #include    <signal.h>
 8 
 9 #define    MAXARGS        20
10 #define    ARGLEN        100
11 
12 /*
13 和psh1一样,但加入了fork()所以会不断地从头开始运行
14 */
15 char *makestring( char *buf )
16 {
17     char    *cp;
18 
19     buf[strlen(buf)-1] = '\0';
20     cp = malloc( strlen(buf)+1 );
21     if ( cp == NULL ){
22         fprintf(stderr,"no memory\n");
23         exit(1);
24     }
25     strcpy(cp, buf);
26     return cp;
27 }
28 
29 void execute( char *arglist[] )
30 {
31     int    pid,exitstatus;
32 
33     pid = fork();
34     switch( pid ){
35         case -1:
36             perror("fork failed");
37             exit(1);
38         case 0:
39             execvp(arglist[0], arglist);
40             perror("execvp failed");
41             exit(1);
42         default:
43             while( wait(&exitstatus) != pid )
44                 ;
45             printf("child exited with status %d,%d\n",
46                     exitstatus>>8, exitstatus&0377);
47     }
48 }
49 
50 int main()
51 {
52     char    *arglist[MAXARGS+1];
53     int        numargs;
54     char    argbuf[ARGLEN];
55 
56     numargs = 0;
57     while ( numargs < MAXARGS )
58     {
59         printf("Arg[%d]? ", numargs);
60         if ( fgets(argbuf, ARGLEN, stdin) && *argbuf != '\n' )
61             arglist[numargs++] = makestring(argbuf);
62         else
63         {
64             if ( numargs > 0 ){
65                 arglist[numargs]=NULL;
66                 execute( arglist );
67                 numargs = 0;
68             }
69         }
70     }
71     return 0;
72 }

testbuf1

1 #include <stdio.h>
2 #include <stdlib.h>
3 int main()
4 {
5     printf("hello");
6     fflush(stdout);//强制输出命令
7     while(1);
8 }

testbuf2

1 #include <stdio.h>
2 int main()
3 /*打印hello*/
4 {
5     printf("hello\n");
6     while(1);
7 }

testbuf3

1 #include <stdio.h>
2 
3 int main()
4 {
5     fprintf(stdout, "1234", 5);//标准输出,将在缓冲流参数其打印到屏幕上
6     fprintf(stderr, "abcd", 4);//标准错误,将后面的参数和错误直接打印到屏幕上
7 }

testpid

 1 #include <stdio.h>
 2 #include <unistd.h>
 3 
 4 #include <sys/types.h>
 5 /*
 6 打印父进程ID,如果没有父进程则打印当前程序进程ID
 7 */
 8 int main()
 9 {
10     printf("my pid: %d \n", getpid());
11     printf("my parent's pid: %d \n", getppid());
12     return 0;
13 }

testpp

1 #include <stdio.h>
2 #include <stdlib.h>
3 int main()
4 {
5     char **pp;
6     pp[0] = malloc(20);//malloc 向系统申请分配指定size个字节的内存空间。
7 
8     return 0;
9 }

PS:并不知道为什么只找到一些资料,我觉得应该是内存访问出错或者非法内存访问

 

testsystem

 1 #include    <stdlib.h>
 2 
 3 /*
 4 发出两个命令,命令存在数组中
 5 */
 6 int main ( int argc, char *argv[] )
 7 {
 8 
 9     system(argv[1]);
10     system(argv[2]);
11     return EXIT_SUCCESS;
12 }                /* ----------  end of function main  ---------- */

 

waitdemo1

 1 #include    <stdio.h>
 2 #include    <stdlib.h>
 3 #include    <sys/types.h>
 4 #include    <sys/wait.h>
 5 #include    <unistd.h>
 6 
 7 #define    DELAY    4
 8 /*
 9 父进程执行parent_code时,因为wait函数会等待子进程运行完毕后再运行
10 */
11 void child_code(int delay)
12 {
13     printf("child %d here. will sleep for %d seconds\n", getpid(), delay);
14     sleep(delay);
15     printf("child done. about to exit\n");
16     exit(17);
17 }
18 
19 void parent_code(int childpid)
20 {
21     int wait_rv=0;        /* return value from wait() */
22     wait_rv = wait(NULL);
23     printf("done waiting for %d. Wait returned: %d\n",
24             childpid, wait_rv);
25 }
26 int main()
27 {
28     int  newpid;
29     printf("before: mypid is %d\n", getpid());
30     if ( (newpid = fork()) == -1 )
31         perror("fork");
32     else if ( newpid == 0 )
33         child_code(DELAY);
34     else
35         parent_code(newpid);
36 
37     return 0;
38 }

 

waitdemo2

 1 #include    <stdio.h>
 2 #include    <stdlib.h>
 3 #include    <sys/types.h>
 4 #include    <sys/wait.h>
 5 #include    <unistd.h>
 6 
 7 #define    DELAY    10
 8 /*
 9 和waitdemo1一样 但多了子进程的三个状态
10 */
11 void child_code(int delay)
12 {
13     printf("child %d here. will sleep for %d seconds\n", getpid(), delay);
14     sleep(delay);
15     printf("child done. about to exit\n");
16     exit(27);
17 }
18 
19 void parent_code(int childpid)
20 {
21     int wait_rv;
22     int child_status;
23     int high_8, low_7, bit_7;
24 
25     wait_rv = wait(&child_status);
26     printf("done waiting for %d. Wait returned: %d\n", childpid, wait_rv);
27 
28     high_8 = child_status >> 8;     /* 1111 1111 0000 0000 */
29     low_7  = child_status & 0x7F;   /* 0000 0000 0111 1111 */
30     bit_7  = child_status & 0x80;   /* 0000 0000 1000 0000 */
31     printf("status: exit=%d, sig=%d, core=%d\n", high_8, low_7, bit_7);
32 }
33 
34 int main()
35 {
36     int  newpid;
37 
38     printf("before: mypid is %d\n", getpid());
39 
40     if ( (newpid = fork()) == -1 )
41         perror("fork");
42     else if ( newpid == 0 )
43         child_code(DELAY);
44     else
45         parent_code(newpid);
46 }

 

参考资料:百度 深入理解计算机系统.pdf

 

总结

  这18个代码让我深入理解了exec函数和fork函数,同时还有一些其他函数。单总的来说,还是在这两上有很大的学习。

  对于exec函数:

    在Linux中要使用exec函数族。系统调用execve()对当前进程进行替换,替换者为一个指定的程序,其参数包括文件名(filename)、参数列表(argv)以及环境变量(envp)。exec函数族当然不止一个,但它们大致相同,在 Linux中,它们分别是:execl,execlp,execle,execv,execve和execvp。一个进程一旦调用exec类函数,它本身就"死亡"了,系统把代码段替换成新的程序的代码,废弃原有的数据段和堆栈段,并为新程序分配新的数据段与堆栈段,唯一留下的,就是进程号,也就是说,对系统而言,还是同一个进程,不过已经是另一个程序了。若是想启动另一程序的执行但自己仍想继续运行的话,那就得结合fork与exec的使用。

  对于fork函数:

    fork函数启动一个新的进程,前面我们说过,这个进程几乎是当前进程的一个拷贝:子进程和父进程使用相同的代码段;子进程复制父进程的堆栈段和数据段。这样,父进程的所有数据都可以留给子进程,但是,子进程一旦开始运行,虽然它继承了父进程的一切数据,但实际上数据却已经分开,相互之间不再有影响了,也就是说,它们之间不再共享任何数据了。

    系统如何来区分它们呢?这是由函数的返回值来决定的。对于父进程, fork函数返回了子程序的进程号,而对于子程序,fork函数则返回零。在操作系统中,我们用ps函数就可以看到不同的进程号,对父进程而言,它的进程号是由比它更低层的系统调用赋予的,而对于子进程而言,它的进程号即是fork函数对父进程的返回值。在程序设计中,父进程和子进程都要调用函数fork()下面的代码,而我们就是利用fork()函数对父子进程的不同返回值用if...else...语句来实现让父子进程完成不同的功能。

  以上内容多是出自网络,但对我的帮助很大。

 

posted @ 2015-11-28 17:35  20135327郭皓  阅读(149)  评论(0编辑  收藏  举报