3 进程

进程:一个程序执行的过程,包括内存的申请、释放,程序的创建、调度及消亡。

   特点:是一个程序执行的动态过程,有执行的概念。而程序是保存在硬盘中的一个代码的集合,没有执行的概念。

a.操作系统中最小的操作和调度单元:进程

b.CPU中最小的操作和调度单元:线程

进程组成:1.文本段

     2.数据段

     3.系统数据段

进程的状态:

    运行态:R

    睡眠态:S(可唤醒);D(不可唤醒)

    停止态:T

    僵尸态:Z(进程结束但空间未释放的状态)

    结束态:X

查看进程执行状态的命令:

  ps -aux

  ps -ef

  top

进程分类:

  交互进程

  批处理进程

  守护进程

===================================================================

fork_1.c

 1 #include <stdio.h>
 2 #include <unistd.h>
 3 int main(void)
 4 {
 5     pid_t pid;
 6     if(-1 == (pid = fork())) {
 7         perror("fail to fork!");
 8             return -1;
 9     }
10 
11     if(pid == 0) {
12         printf("我是子进程!\n");
13         printf("子进程:pid = %d,其父:ppid = %d\n",getpid(),getppid());
14     }
15     else if(pid > 0) {
16         printf("我是父进程!\n");
17         printf("父进程:pid = %d,其子:childpid = %d,其父:ppid = %d\n",getpid(),pid,getppid());
18     }//父子进程执行的先后顺序是随机的!!!
19 
20     getchar();
21     return 0;
22 }

fork_2.c

 1 #include <stdio.h>
 2 #include <unistd.h>
 3 int main(void)
 4 {
 5     pid_t pid;
 6     if(-1 == (pid = fork()))
 7     {
 8         perror("fail to fork!");
 9         return -1;
10     }
11 
12     if(pid == 0)
13     {
14         printf("我是子进程!\n");
15         printf("子进程:pid = %d,其父:ppid = %d\n",getpid(),getppid());
16     }
17 
18     else if(pid > 0)
19     {
20         pid_t pid1;//准备生二胎
21         if(-1 == (pid1 = fork())) 
22         {
23             perror("fail to fork1!");
24             return -1;
25         }
26         if(pid1 == 0)
27         {
28             printf("我是子2进程!\n");
29             printf("子2进程:pid = %d,其父:ppid = %d\n",getpid(),getppid());
30         }
31 
32         printf("我是父进程!\n");
33         printf("父进程:pid = %d,其子:childpid = %d,其父:ppid = %d\n",getpid(),pid,getppid());
34     }//父子进程执行的先后顺序是随机的!!!
35 
36     getchar();
37     return 0;
38 }

fork_3.c

 1 #include <stdio.h>
 2 #include <unistd.h>
 3 int main(void)
 4 {
 5     pid_t pid;
 6     if(-1 == (pid = fork())) {
 7         perror("fail to fork!");
 8             return -1;
 9     }
10 
11     if(pid == 0) {
12         printf("我是子进程!\n");
13         printf("子进程:pid = %d,其父:ppid = %d\n",getpid(),getppid());
14         getchar();//让父进程先执行完(先死),自己变成孤儿进程!!!
15     }
16     else if(pid > 0) {
17         printf("我是父进程!\n");
18         printf("父进程:pid = %d,其子:childpid = %d,其父:ppid = %d\n",getpid(),pid,getppid());
19     }//父子进程执行的先后顺序是随机的!!!
20 
21 //    getchar();
22     return 0;
23 }

fork_5.c

 1 #include <stdio.h>
 2 #include <unistd.h>
 3 #define N 5
 4 int main(void)
 5 {
 6     pid_t pid[N];
 7     int i = 0;
 8     for(i = 0;i < N;i++)//生5胎
 9     {
10         if(-1 == (pid[i] = fork()))
11         {
12             perror("fail to fork!");
13             return -1;
14         }
15 
16         if(pid[i] == 0) {
17             printf("我是子进程%d!\n",i);
18             printf("子进程%d:pid = %d,其父:ppid = %d\n",i,getpid(),getppid());
19             while(1);
20         }
21     }
22 
23 //      else if(pid > 0) {
24         printf("我是父进程!pid:%d \n",getpid());
25 /*        for(i = 0;i < N;i++)
26         {
27             printf("子进程%d:%d\n",i+1,pid[i]);
28 //    }//父子进程执行的先后顺序是随机的!!!
29         }
30 */    getchar();
31     return 0;
32 }

wait_1.c

 1 #include <stdio.h>
 2 #include <unistd.h>
 3 #include <sys/types.h>
 4 #include <sys/wait.h>
 5 #include <stdlib.h>
 6 int main(void)
 7 {
 8     pid_t pid,pid1;
 9     if(-1 == (pid = fork()))
10     {
11         perror("fail to fork!");
12         return -1;
13     }
14 
15     if(pid == 0) {
16         printf("我是子进程!\n");
17         printf("我即将死亡!请父进程为我收尸!\n");
18         sleep(5);
19         exit(0);
20     }
21     else if(pid > 0) {
22         int status;
23     //    pid1 = wait(NULL);//为子进程收尸,NULL表不接受退出状态/功能是回收子进程空间及退出状态
24         pid1 = wait(&status);//为子进程收尸,参数:status保存子进程退出状态空间的首地址/功能:是回收子进程空间及退出状态
25         /*返回值:成功表示回收到子进程空间的PID号;失败返回-1.
26          * 特点:1.阻塞等待直到有子进程结束并回收到子进程资源才向下执行;
27          *          2.回收多个结束的子进程则需要多次调用 wait()    */
28         printf("我是父进程!我已收尸其PID:%d其退出状态为%d\n",pid1,status);
29     }
30     return 0;
31 }

waitpid_1.c

 1 #include <stdio.h>
 2 #include <unistd.h>
 3 #include <sys/types.h>
 4 #include <sys/wait.h>
 5 #include <sys/stat.h>
 6 #include <stdlib.h>
 7 int main(void)
 8 {
 9     pid_t pid;
10     if(-1 == (pid = fork()))
11     {
12         perror("fail to fork!");
13         return -1;
14     }
15 
16     if(pid == 0) {
17         printf("我是子进程,我PID:%d!\n",getpid());
18         getchar();
19         printf("子进程即将退出!\n");
20         exit(0);
21     }
22     else if(pid > 0) {
23         int status;
24         pid_t ret;
25         /*pid_t waitpid(pid_t pid,int *status,int options);
26          *功能:是回收子进程空间
27          *参数:pid:为-1时回收任意一个子进程
28          *返回值:成功返回接受到的子进程空间的pid;失败返回-1
29                      若WNOHANG被设置,没有回收到子进程则返回0*/
30         while(0 == (ret = waitpid(-1,&status,WNOHANG)));/*功能:是回收子进程空间*/
31         {    
32             printf("我还没退出!\n");
33             sleep(1);
34         }
35         printf("我是父进程!我已收尸其PID:%d其退出状态为%d\n",ret,status);
36     }
37     return 0;
38 }//执行没出效果!!

daemon.c

守护进程可以脱离终端的依赖,一般完成对操作系统服务,或者系统级维护,维护操作系统安全的某些特定功能。

 1 /*守护进程的创建*/
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #include <unistd.h>
 5 #include <sys/types.h>
 6 #include <sys/wait.h>
 7 #include <fcntl.h>
 8 
 9 int main(void)
10 {
11     pid_t pid;
12     int i = 0;
13     if (-1 == (pid = fork())) {//1.创建一个子进程
14         perror("fail to fork!/n");
15         return -1;
16     }
17     if (pid > 0) {//2.让子进程变成孤儿进程从而脱离终端/父进程自杀
18         exit(0);
19     }
20     setsid();//3.让子进程变成会话组组长
21     //setsid runs a program in a new session.
22     chdir("/");//4.修改子进程的工作路径
23     umask(0);//5.修改该进程的掩码值
24     for (i=0; i<getdtablesize(); i++) {//6.将进程所有打开的文件描述符关闭
25         close(i);
26     }
27     // getdtablesize()  returns the maximum number of files a process can have
28     // open, one more than the largest possible value for a file descriptor.
29     while(1) {//幕后守护....
30         sleep(1);
31     }
32     return 0;
33 }

 

posted @ 2017-03-14 20:54  bkycrmn  阅读(114)  评论(0)    收藏  举报