送给大家一句话: 人一切的痛苦,本质上都是对自己无能的愤怒。而自律,恰恰是解决人生痛苦的根本途径。—— 王小波 今天我们继续学习Linux的进程,上两篇文章我们认识了什么是进程,如何创建进程,进程状态。今天我们主要讲解 进程优先级和环境变量。
学习优先级需要了解:
int prio = ??
,优先级的本质是一个数字,数字越小,优先级越高。因为CPU的资源是有限的,不得不排队(就像食堂打饭一样),系统大部分情况进程是比较多的,但是硬件只有一套。
操作系统关于的调度好优先级的原则:分时操作系统,基本的公平。如果进程因为长时间不被调度,就造成的饥饿问题。 我们来简单查看一下进程的优先级:
1 #include<stdio.h>
2 #include<unistd.h>
3
4 int main()
5 {
6 while(1){
7 printf("I am a process , pid: %d\n",getpid());
8 sleep(1);
9 }
10 }
11
这里介绍一下一个新指令: ps -l
运行是运行了,但是没有我们现在的进程,这是因为ps -l
默认是显示当前终端的进程,我们使用ps -al
就可以查看全部的了。
其中:
通过上图,可以看到我们进程(pid : 6000)的优先级是80。
接下来我们来看如何查看修改优先级
用top命令更改已存在进程的nice:
进入top后按 r
输入进程PID –> 输入nice值,就可以修改nice值。来进行一下尝试:
可以看到,我们想要改到100,但是最终结果是99,这是为什么???
nice值不能随意调整,而是有范围的 [-20 , 19)共四十个数字(即四十 个 梯度)
我们通过ps-al查看的进程信息中,nice值就是最大值 19。
每次调整完优先级都是从80开始的,通过 新的优先级 = 优先级(80) + nice值
进行计算。
注意:作为普通用户不能频繁调整优先级
想要多次调整需要root身份。
同时调整优先级也可以使用nice
和 renice
命令,具体使用方法可以用那个男人 man
来进行查看(以renice 为例):
RENICE(1) User Commands RENICE(1)
NAME
renice - alter priority of running processes
SYNOPSIS
renice [-n] priority [-gpu] identifier...
DESCRIPTION
renice alters the scheduling priority of one or more running processes. The
first argument is the priority value to be used. The other arguments are
interpreted as process IDs (by default), process group IDs, user IDs, or user
names. renice'ing a process group causes all processes in the process group
to have their scheduling priority altered. renice'ing a user causes all pro‐
cesses owned by the user to have their scheduling priority altered.
OPTIONS
-n, --priority priority
Specify the scheduling priority to be used for the process, process
group, or user. Use of the option -n or --priority is optional, but
when used it must be the first argument.
-g, --pgrp pgid...
Force the succeeding arguments to be interpreted as process group IDs.
-u, --user name_or_uid...
Force the succeeding arguments to be interpreted as usernames or UIDs.
-p, --pid pid...
Force the succeeding arguments to be interpreted as process IDs (the
default).
-h, --help
Display a help text.
-V, --version
Display version information.
也就是使用 renice -n ? -p ?
就可以完成修改优先级。
命令行参数其实很简单,我们已经用过许多次了。类似 rm -f
grep -v
等等都是命令行参数。那么这本质到底是什么呢???
我们先从main函数讲起,我们都知道main函数有参数int main( int argc, char* argv[ ])
,(但实际我们经常不带,今天不管这个问题)
下面我们来看具体是什么作用:
1 #include<stdio.h>
2 #include<unistd.h>
3
W> 4 int main(int argc , char* argv[])//这里的报错先忽视
5 {
6 for(int i = 0; i < argc ;i++ )
7 {
8 printf("argv[%d]-> %s\n",i,argv[i]);
9 }
10 return 0;
11 }
12
来看运行效果:
实际上main函数的参数就是命令行的参数,也就是命令行字符串。前面的./myprocess
是程序的路径和名称,后面的-a -b -c -d
就是该进程匹配的选项。
那为什么要这么干呢???又是谁干的呢???
来看下面一段代码:
1 #include<stdio.h>
2 #include<unistd.h>
3 #include<string.h>
4
5 int main(int argc , char* argv[])
6 {
7 if(argc != 2)
8 {
9 printf("Usage:%s -[a,b,c,d]\n",argv[0]);
10 return 1;
11 }
12
13 if(strcmp(argv[1],"-a") == 0)
14 {
15 printf("this is function1\n");
16 }
17
18 else if(strcmp(argv[1],"-b") == 0)
19 {
20 printf("this is function2\n");
21 }
22
23 else if(strcmp(argv[1],"-c") == 0)
24 {
25 printf("this is function3\n");
26 }
27
28 else if(strcmp(argv[1],"-d") == 0)
29 {
30 printf("this is function1\n");
31 }
32 else
33 {
34 printf("no this function!!!\n");
35 }
36 return 0;
37 }
38
这样可以区分命令行输入了哪些信息,并执行相应功能。来看效果:
为什么要有命令行参数???
本质:命令行参数的本质是交给我们不同程序的不同选项,用来定制不同功能,一般命令里会带许多选项
这样通过不同选项就执行程序的不同功能。这是不是有点像我们经常使用的哪些命令呢!?ll
ls
rm -f
等等
那这个工作时是谁来干的呢???
我们进行一个小小的测试:
1 #include<stdio.h>
2 #include<unistd.h>
3 #include<string.h>
4
5 int g_val = 10000;
6
W> 7 int main(int argc , char* argv[])
8 {
9 printf("I am father process,pid : %d ,ppid: %d ,g_val: %d\n",getpid(),getppid(),g_val);
10 sleep(5);
11
12 pid_t id = fork();
13 if(id == 0)
14 {
15 //child
16 while(1)
17 {
18 printf("I am child process,pid: %d,ppid: %d,g_val: %d\n",getpid(),getppid(),g_val);
19 sleep(1);
20 }
21 }
22 else
23 {
24 //father
25
26 printf("I am father process,pid: %d,ppid: %d,g_val: %d\n",getpid(),getppid(),g_val);
27 sleep(1);
28 }
29
30 return 0;
31 }
来看效果:
可以看到子进程和父进程都成功使用了g_val,即父进程的数据默认可以被子进程看到并访问!!!
而其中的PID:10069是谁呢???
命令行中的程序都会变成进程,其实都是bash 的子进程!!!
所以那这个工作时是谁来干的呢???
都是bash进行的,也就是main函数的参数是bash处理的
系统中的很多配置,在我们登录Linux就已经被加载到bash进程中了。 来看一个(搜索路径的环境变量):
bash在执行命令的时候,需要先找到命令,因为未来要加载!!!
同样只要我们把我们写可执行程序拷贝到对应路径就可以不用再写./
就可以执行。
当然这样太粗暴了,我们可以使用PATH = 路径
这样就改变我们的环境变量了:
但是现在我们好多指令都不能正常使用了???啊???怎么办???原来的路径不一致,寻找不到了大部分指令。 这时候重启即可。
这样就恢复了,但是到底怎样才能把我们的程序路径加入进PATH呢???
应该使用PATH=$PATH : 路径
即可。
这样就成功加入了!!!
注意 最开始的环境变量不是在内存中,而是在系统的对应的配置文件中
那配置文件在哪里呢???就在家目录的几个文件中
环境变量都是大写的英文字母,使用echo $名字
即可
使用env
就可以查看所有的环境变量:
这些都是环境变量!!!我们来认识其中几个:
等等都是环境变量
当然自己也可以定义环境变量。
使用export就可以进行:export THIS_IS_MY_ENV = hellobit
这样就创建一个环境变量!!!
如果不加export 就会创建本地变量,与环境变量不同(需要对通信,多线程有一定了解才能理解)。
1 #include<stdio.h>
2 #include<unistd.h>
3 #include<string.h>
4
5 int g_val = 10000;
6
7 int main()
8 {
9 extern char** environ;
10 for(int i = 0;environ[i];i++)
11 {
12 printf("env[%d]->%s\n",i,environ[i]);
13 }
14 return 0;
15 }
这样我们就通过C语言程序成功获取了环境变量。 然后因为我们的程序是一个进程,是bash的子进程,所以环境变量可以被子进程获取!!! (环境变量在BASH中)
首先数据储存在磁盘中,运行时会加载到内存中,也就把环境变量存入内存中的bash/shell. bash进程在启动的时候,默认会给子进程形成两张表:
环境变量具有系统级的全局属性,因为会被子进程继承下去!!! esport , echo 等是内建命令,由bash执行,80%命令是bash创建的子进程实行