Master Linux Process Priorities and Environment Variables: A Step‑by‑Step Guide
This guide explains Linux process priority concepts, how to view and modify priorities with ps, top, renice and nice commands, and details environment variables, their common commands, and C APIs such as getenv, main arguments and environ, complete with practical code snippets and screenshots.
Process Priority in Linux
Process priority (priority) determines the order in which the CPU allocates resources to competing processes. A higher priority gives a process earlier execution rights, which can improve overall system performance in multitasking environments.
Key fields displayed by ps -al:
UID : user ID of the process owner
PID : process identifier
PPID : parent process identifier
PRI : raw priority value (lower numbers run first)
NI : nice value that adjusts the priority
The relationship between priority and nice is expressed by the formula: PRI(new) = PRI(old) + nice Most processes start with a default PRI of 80; adjusting the nice value changes the effective priority. The nice range is –20 to 19, giving 40 distinct levels.
To modify a running process’s priority interactively:
Run top, press r , enter the PID, then input the desired nice value (e.g., –20 for highest priority). Root privileges are required for negative nice values.
Command‑line tools for priority management: nice -n <em>value</em> <em>command</em>: start a new process with a specific nice value.
renice [-g <em>pgid</em>] [-u <em>user</em>] [-p <em>pid</em>] <em>priority</em>: change the priority of existing processes. Options allow adjusting an entire process group, a specific user’s processes, or a single PID. renice [-n] priority [-g] identifier After adjusting, you can verify the new priority with ps -al or top. The scheduler enforces the –20 to 19 range, preventing excessively high priorities that could starve other processes.
Environment Variables in Linux
Environment variables are key‑value pairs that define the operating environment for processes. They are global within a user session and inherited by child processes.
Common commands: env: list all environment variables. echo $VAR: display the value of a specific variable. export VAR=value: promote a shell variable to an environment variable (temporary for the current session). unset VAR: remove a variable. set: show both shell and environment variables.
Important variables:
PATH : directories searched for executable files. Adding a directory to PATH enables running programs without a leading ./.
HOME : the user’s home directory.
SHELL : the path to the current shell (e.g., /bin/bash).
Example of extending PATH temporarily: export PATH=$PATH:/your/new/path Note: export changes are lost after logout or reboot; permanent changes require editing files such as ~/.bashrc, ~/.bash_profile, or /etc/bashrc.
Accessing Environment Variables from C
The C standard library provides getenv() to retrieve a single variable’s value:
#include <stdio.h>
#include <stdlib.h>
int main() {
char *env = getenv("USER");
if (env == NULL) {
perror("getenv fail:");
}
printf("%s
", env);
return 0;
}Program output shows the value passed from the parent shell.
All environment variables can be accessed via the third argument of main: int main(int argc, char *argv[], char *envp[]) Iterating envp prints each variable, mimicking the env command:
#include <stdio.h>
int main(int argc, char *argv[], char *envp[]) {
for (int i = 0; envp[i] != NULL; ++i) {
printf("envp[%d] --> %s
", i, envp[i]);
}
return 0;
}The global variable environ offers another way to traverse the environment:
#include <stdio.h>
#include <unistd.h>
int main() {
extern char **environ;
for (int i = 0; environ[i] != NULL; ++i) {
printf("environ[%d] --> %s
", i, environ[i]);
}
return 0;
}These examples demonstrate that environment variables are stored in a memory‑resident table created for each logged‑in user, inherited from the parent bash process.
Configuration Files
Typical files that define a user’s environment: ~/.bashrc and ~/.bash_profile for regular users. /etc/bashrc for system‑wide defaults.
Editing these files allows permanent customization of variables such as PATH, HOME, and others.
Command‑Line Arguments in main
The parameters int argc and char *argv[] convey the number of arguments and their values to a program. argc counts the arguments excluding the terminating NULL. Example:
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("argc = %d
", argc);
for (int i = 0; argv[i] != NULL; ++i) {
printf("argv[%d] --> %s
", i, argv[i]);
}
return 0;
}Running this program with different options (e.g., ls -l -a) shows how C programs receive and act upon command‑line flags.
Overall, understanding process priorities, environment variables, and their interaction with C programs equips developers and system administrators to fine‑tune Linux performance and behavior.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
