免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 854 | 回复: 0
打印 上一主题 下一主题

我的第一个实验总结 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-10-12 16:26 |只看该作者 |倒序浏览
实验1    Linux基本命令

(这没什么好说的,只有一用了才知道。)
找了一些比较好的网站以做参考:
  Unix/Linux 命令速查表:
  
http://linuxtoy.org/archives/unix-linux-command-cheat-sheet.html

  2008初学Linux推荐教程下载
  
http://www.linuxdiyf.com/bbs/thread-73635-1-1.html

初学者Linux电子书籍下载:
  
http://www.linux286.com/linux/linuxdzsj.htm

  Linux命令大全(CHM版)
  
http://new.flagnet.net/Soft/dbook/shiyong/200710/20071022161541_105.html


实验2 进程

我的实验结果:
一、实验环境:
lzel@lzel-desktop:~$ uname -a
Linux lzel-desktop 2.6.24-21-generic #1 SMP Mon Aug 25 17:32:09 UTC
2008 i686 GNU/Linux
lzel@lzel-desktop:~$ cat /proc/cpuinfo
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 14
model name      : Genuine Intel(R) CPU           T2050  @ 1.60GHz
stepping        : 8
cpu MHz         : 1595.323
cache size      : 2048 KB
.....
processor       : 1
vendor_id       : GenuineIntel
cpu family      : 6
model           : 14
model name      : Genuine Intel(R) CPU           T2050  @ 1.60GHz
stepping        : 8
cpu MHz         : 1595.323
cache size      : 2048 KB
lzel@lzel-desktop:~$ cat /proc/meminfo
MemTotal:       515532 kB
MemFree:         15656 kB
Buffers:         18708 kB
Cached:         153428 kB
SwapCached:      37228 kB
......
lzel@lzel-desktop:~$
二、程序源码:
lzel@lzel-desktop:~/os/src$ ls
a.out  process_t.c   thread_t.c
lzel@lzel-desktop:~/os/src$ cat process_t.c
#include  
#include  
#include  

#define MAX_CHILD_NUMBER 5 /* 允许建立的子进程个数最大值 */
#define SLEEP_INTERVAL 2 /* 子进程睡眠时间 */
int proc_number=0; /* 子进程的自编号,从0开始 */
void do_something();
int main(int argc, char* argv[])
{
         int child_proc_number = MAX_CHILD_NUMBER; /* 子进程个数 */
         int i, ch;
         pid_t child_pid;
         pid_t pid[10]={0}; /* 存放每个子进程的id */
         if (argc > 1)
         {
                 child_proc_number = atoi(argv[1]); /*
命令行参数中的第一个参数表示建立几个子进程,最多10个 */
                 child_proc_number = (child_proc_number > 10) ? 10 :
child_proc_number;
         }
         for (i=0; i 0)  {
                         pid = child_pid;
                         printf("the %d child process id is %d\n",i,pid);
                 } else if(child_pid==0) {
                         proc_number = i;    /*记录创建了第几个子进程*/
                         do_something();   /*让子进程做一些事情*/
                 } else  {
                         perror("fork");
                         return -1;
                 }
         }
         /* 让用户选择杀死哪个进程。输入数字(自编号)表示杀死该进程
         * 输入q退出 */
         while ((ch = getchar()) != 'q')
         {
                 if (isdigit(ch))
                 {
                 /* 在这里填写代码,向pid[ch-'0']发信号SIGTERM,
                 * 杀死该子进程 */
                    kill(pid[ch-'0'],SIGTERM);
                 }
         }
         /* 在这里填写代码,杀死本组的所有进程 */
         kill(0,SIGTERM);
         return;

}

void do_something()
{
         for(;;)
         {
                 /* 打印子进程自编号。为清晰,在每个号码前加"号码+3"个空格
                 * 比如号码是1,就打印" 1" */
                 printf("This is process No.%d  %d\n",proc_number,getgid());
                 sleep(2); /* 主动阻塞两秒钟 */
         }
}

lzel@lzel-desktop:~/os/src$

三、编译及运行情况:
编译:
lzel@lzel-desktop:~/os/src$ gcc -c process_t.o  process_t.c //编译 -c
是编译参数, 其后的process_t.o是指定要输出的目标文件名,如果不加参数则默认于文件的文件名但末尾改为.o
lzel@lzel-desktop:~/os/src$ gcc process_t.o -o process_t //连接 -o
其后的参数指定可执行文件的名称,默认a.out
lzel@lzel-desktop:~/os/src$ ls
a.out  process_t  process_t.c  process_t.c~  process_t.o  thread_t.c
lzel@lzel-desktop:~/os/src$
运行:
lzel@lzel-desktop:~/os/src$ ./process_t //默认产生5个进程
This is process No.0  1000
the 0 child process id is 8072
This is process No.1  1000
the 1 child process id is 8073
This is process No.2  1000
the 2 child process id is 8074
This is process No.3  1000
the 3 child process id is 8075
This is process No.4  1000
the 4 child process id is 8076
This is process No.0  1000
This is process No.1  1000
This is process No.2  1000
This is process No.3  1000
This is process No.4  1000
0 //杀死(kill)0号进程
This is process No.1  1000
This is process No.2  1000
This is process No.3  1000
This is process No.4  1000
1 //杀死(kill)1号进程
This is process No.2  1000
This is process No.3  1000
This is process No.4  1000
2 //杀死(kill)2号进程
This is process No.3  1000
This is process No.4  1000
3 //杀死(kill)3号进程
This is process No.4  1000
4 //杀死(kill)4号进程
q //杀死(kill)全部子进程
已终止
lzel@lzel-desktop:~/os/src$ ./process_t
This is process No.0  1000
the 0 child process id is 8107
This is process No.1  1000
the 1 child process id is 8108
This is process No.2  1000
the 2 child process id is 8109
This is process No.3  1000
the 3 child process id is 8110
This is process No.4  1000
the 4 child process id is 8111
q //杀死(kill)全部子进程
已终止
lzel@lzel-desktop:~/os/src$ ./process_t 20 //10以上之产生十个进程
This is process No.0  1000
the 0 child process id is 8139
This is process No.1  1000
the 1 child process id is 8140
This is process No.2  1000
the 2 child process id is 8141
This is process No.3  1000
the 3 child process id is 8142
This is process No.4  1000
the 4 child process id is 8143
This is process No.5  1000
the 5 child process id is 8144
This is process No.6  1000
the 6 child process id is 8145
This is process No.7  1000
the 7 child process id is 8146
This is process No.8  1000
the 8 child process id is 8147
This is process No.9  1000
the 9 child process id is 8148
This is process No.0  1000
This is process No.1  1000
This is process No.2  1000
This is process No.3  1000
This is process No.4  1000
This is process No.5  1000
This is process No.6  1000
This is process No.7  1000
This is process No.8  1000
This is process No.9  1000
q
已终止
lzel@lzel-desktop:~/os/src$
五、结论及分析:
先猜想一下这个程序的运行结果。假如运行"./process 20",输出会是什么样?
     会产生10个进程,以后每个进程都在打印指定信息。
然后按照注释里的要求把代码补充完整,运行程序。可以多运行一会儿,并在

此期间启动、关闭一些其它进程,看process 的输出结果有什么特点,记录下这个结果。

开另一个终端窗口,运行"ps aux|grep process"命令,看看process
究竟启动了多少个进程。回到程序执行窗口,按"数字键+回车"尝试杀掉一两个进程,再到另一个窗口看进程状况。

按q 退出程序再看进程情
lzel@lzel-desktop:~/os/src$ ps aux|grep process
lzel      8217  0.0  0.0   1568   368 pts/3    S+   14:15   0:00 ./process_t 20
lzel      8218  0.0  0.0   1564   228 pts/3    S+   14:15   0:00 ./process_t 20
lzel      8219  0.0  0.0   1564   204 pts/3    S+   14:15   0:00 ./process_t 20
lzel      8220  0.0  0.0   1564   204 pts/3    S+   14:15   0:00 ./process_t 20
lzel      8221  0.0  0.0   1564   204 pts/3    S+   14:15   0:00 ./process_t 20
lzel      8222  0.0  0.0   1564   204 pts/3    S+   14:15   0:00 ./process_t 20
lzel      8223  0.0  0.0   1564   204 pts/3    S+   14:15   0:00 ./process_t 20
lzel      8224  0.0  0.0   1564   204 pts/3    S+   14:15   0:00 ./process_t 20
lzel      8225  0.0  0.0   1564   204 pts/3    S+   14:15   0:00 ./process_t 20
lzel      8226  0.0  0.0   1564   204 pts/3    S+   14:15   0:00 ./process_t 20
lzel      8227  0.0  0.0   1564   204 pts/3    S+   14:15   0:00 ./process_t 20
lzel      8230  0.0  0.1   3220   776 pts/2    S+   14:16   0:00 grep process
lzel@lzel-desktop:~/os/src$
除grep process外,共产生11个进程,其中一个父进程,10个子进程。
lzel@lzel-desktop:~/os/src$ ps aux|grep process
lzel      8217  0.0  0.0   1568   376 pts/3    S+   14:15   0:00 ./process_t 20
lzel      8218  0.0  0.0      0     0 pts/3    Z+   14:15   0:00
[process_t]  
lzel      8219  0.0  0.0      0     0 pts/3    Z+   14:15   0:00
[process_t]  
lzel      8220  0.0  0.0      0     0 pts/3    Z+   14:15   0:00
[process_t]  
lzel      8221  0.0  0.0      0     0 pts/3    Z+   14:15   0:00
[process_t]  
lzel      8222  0.0  0.0   1564   204 pts/3    S+   14:15   0:00 ./process_t 20
lzel      8223  0.0  0.0   1564   204 pts/3    S+   14:15   0:00 ./process_t 20
lzel      8224  0.0  0.0   1564   204 pts/3    S+   14:15   0:00 ./process_t 20
lzel      8225  0.0  0.0   1564   204 pts/3    S+   14:15   0:00 ./process_t 20
lzel      8226  0.0  0.0   1564   204 pts/3    S+   14:15   0:00 ./process_t 20
lzel      8227  0.0  0.0   1564   204 pts/3    S+   14:15   0:00 ./process_t 20
lzel      8250  0.0  0.1   3220   780 pts/2    R+   14:17   0:00 grep process
lzel@lzel-desktop:~/os/src$
杀死0、1、2、3后的情况,Z+是僵尸进程的标志 ,defunct死亡
lzel@lzel-desktop:~/os/src$ ps aux|grep process
lzel      8252  0.0  0.1   3220   776 pts/2    R+   14:17   0:00 grep process
lzel@lzel-desktop:~/os/src$
用q杀死所有子进程之后。父进程此时也已经退出。

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/73569/showart_1287005.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP