分类: LINUX
2008-10-12 16:26:50
(这没什么好说的,只有一用了才知道。)
找了一些比较好的网站以做参考:
Unix/Linux 命令速查表:
2008初学Linux推荐教程下载
初学者Linux电子书籍下载:
Linux命令大全(CHM版)
实验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
/* 在这里填写代码,建立child_proc_number个子进程
* 子进程要执行
* proc_number = i;
* do_something();
* 父进程把子进程的id保存到pid[i] */
if((child_pid=fork()) > 0) {
pid[i] = child_pid;
printf("the %d child process id is %d\n",i,pid[i]);
} 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;
三、编译及运行情况:
编译:
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
究竟启动了多少个进程。回到程序执行窗口,按"数字键+回车"尝试杀掉一两个进程,再到另一个窗口看进程状况。