Main content:
1. Command-line arguments under Main function
2. Access to environmental variables;
3. Where the environment variables are stored in the validation process
4. Status of Process operation
1. Command line Parametersin the study of C language, we have a general understanding of the use of command line parameters;today we are under the Linux system, to see the use of these parameters;
we all know that the main function has parameters:format of main function: int main (
intargc,char * argv[],char* env)where the parameter env represents the environment variable of the process ... the first two indicate thatThe number of commands represented by ARGCargv[] Represents a collection of commands (strings)Let's check it out!!! we can check it out in the next code!!
#include <stdio.h> #include <stdlib.h>int main (int argc, char * Argv[],char *env[]) { int i = 0; C3/>for (i =0; i < argc;++i) { printf ("%s", Argv[i]); } printf ("\ n"); return 0;}
get results under Linux!!!
2. Access to environment variables
How do you get an environment variable for a process?? ,Here I would like you to introduce three ways:1), use the third command-line parameter envGet the method:command Line arguments The env type is stored inside the pointer array, and the environment variable ends with NULLcode to show it!
#include <stdio.h> #include <stdlib.h>int main (int argc, char * Argv[],char *env[]) { int i = 0; for (i= 0; env[i];++i) { printf ("%s\n", Env[i]); } printf ("\ n"); return 0;}
2), use global variable environ
The global variable environ defined in the C library points to the environment variable table, and Environ is not contained in any header file, so it is used with an extern declaration.
#include <stdio.h>int main () {extern char **environ;int i=0;for (; environ[i]!=null;i++) {printf ("%s\n", environ[i ]);} return 0;}
3), get function using environment variable
the Environ pointer above and the command line parameter env can see all the environment variable strings, but not easy enough,If you give name to find its corresponding value in the environment variable table, you can use the GETENV function.
function getenv (environment variable name) The return value of the getenv is a pointer to value, or null if not found;
#include <stdio.h> #include <stdlib.h>int main () {//Method Three: Use function getenv (environment variable name) char * name = getenv (" PATH "); printf ("%s\n", name); return 0;}
where environment variables are stored in the processfor an incoming, the system allocates the memory of the process is roughly the same as several points????
Let's check it out!!! we all know that the local variables defined for the code are defined in the stack segment ,
so we just have to decide if the address of the environment variable is the high or low address of the local variable.
Code Implementation
#include <stdio.h> #include <stdlib.h>int main () {//view environment variable in memory key storage location //main area: code snippet, static zone (initialize data, Uninitialized data), heap, stack, unknown region int m = 0; printf ("&m =%p\n", &m); printf ("&path =%p \ n", &*env[0]); return 0;}
The final result:
The address of the local variable m in the result is located at the low address of the environment variable path, so the conclusion is correct ...
3. Process status
for memory, there is certainly not only one process at a time, but so many processes are certainly not in the running state;so A status identifier is included in the PCB for each process
for the process 、、、 there are mainly the following states:1, R (running)The running state 、、、 indicates that the current process is running in the queue
2, S (sleeping)hibernate 、、、 Process Execution code for hibernation
3, D (disk sleep)a special kind of dormant state;;;
4, T (Stopped)stop state, process now nothing needs to be done
5, T (tracing stop)This state is not explained in the temporary
6, X (dead)dead state, indicating that the process is being reclaimed by the parent process
7, Z (zombie)Zombie State,,, represents the state of the process that has an exception--------process is recycled within this time period
differences between state S (sleeping), D (disk sleep), T (stopped)S-state: A shallow sleep, the code of sleep that the process executes at this time, which can be awakened by the system;
D state: Deep sleep, the process into the deep sleep state, can only wake themselves, the process generally in the I/O, the state will change to a D state, the process in this state will not be killed by any signal;
t Status: Stop state, the process in that State does nothing;
transition diagram for process state
4, the special process of analysiswe hear a lot of special processes, and here's what I'm going to tell you about two special processes.
1. Orphan processthe so-called orphan process is that the parent process is over, and the process of child process adoption by process # 1th
"Process Demo"Let's actually demonstrate this process:here is the test code
The result of the final operation:
as a result, when the parent process is finished, the PID of the parent process of the child process becomes the No. 1th process.
2. Zombie ProcessThis kind of process is, that is, the child process has ended, but the parent process is still running without end, the child process cannot be recycled, so this time the child process is a zombie process.
"Process Demo"
implementation results:
the current sub-process becomes a zombie state;
Analysis of process state and environment variables