1, Get ProcessID[1]
Each process has a unique positive number (not0) ProcessID(PID).
ExampleCode
# Include <unistd. h> # include <sys/types. h> PID t getpid (void); PID t getppid (void); returns: PID of either the caller or the parent
The getpid and getppid routines return an integer value of Type pid_t, which on Linux systems
Is defined in types. h as an int.
The process is always running, stopped, and terminated.
2, Creation and Termination
Sample Code
# Include <stdlib. h> void exit (INT status); this function does not return # include <unistd. h> # include <sys/types. h> PID t fork (void); returns: 0 to child, PID of child to parent,-1 on Error # include <sys/types. h> # include <sys/Wait. h> PID t waitpid (PID t PID, int * status, int options); returns: PID of child if OK, 0 (if wnohang) or-1 on errorthe members of the wait set are determined by the PID argument: If pid> 0, then the wait set is the singleton child process whose process ID is equal to PID. if pid =-1, then the wait set consists of all of the parent's child processes.
The newly created child process is almost, but not quite, identical to the parent. The child
Gets an identical (but separate) Copy of the parent'S userlevel virtual address space, including the text, data,
And BSS segments, heap, and user stack. The child also gets identical copies of any of the parent'S open file descriptors, which
Means the child can read and write any files that were open in the parent when it called fork.The most significant
Difference between the parent and the newly created child is that they have different
PIDs.
ForkA function is often confusing because it is called only once but returns twice: one is in the calling process (parent process) and the other is in the newly created child process.
For the example, see the original article.
3Reclaim sub-Processes
When a process is terminated, the kernel does not immediately clear it from the system; instead, it maintains a termination state until it is recycled by the parent process (Reaped). When a parent process recycles a terminated child process, the kernel passes the exit status of the child process to the parent process and then discards the terminated process. A terminated but not recycled process is called a botnet (Zombie).
If the parent process is not recycledInitProcess to recycle.
A process is calledWaitpidFunction to wait for its sub-process to terminate or pause.
4Process sleep
Sample Code
# Include <unistd. h> unsigned int sleep (unsigned int SECs); returns: seconds left to sleep // hold the process for a period of time # include <unistd. h> int pause (void); always returns-1 // puts the calling function to sleep until a signal is already ed by the process.
5, Load and runProgram
Sample Code
# Include <unistd. h> int execve (char * filename, char * argv [], char * envp); does not return if OK, returns-1 on errorint main (INT argc, char ** argv, char ** envp); int main (INT argc, char * argv [], char * envp []);
InExecveLoadedFilenameThen, start the code preparation stack and pass the control to the main function of the new program. The original function is shown in the code.
PassGetenv, setenv, unsetenvDevice environment variables.
6 A process is a special instance of a program in execution. The program always runs in the context of a process. fork the function runs the same program in the new sub-process, the new child process is a replica of the parent process ; execve the function loads and runs a new program in the context of the current process, it overwrites the address space of the current process, but does not create a new process. The new program still has the same PID , and inherits the call execve all file descriptors opened during the function.
Sample Code
/* $ Begin shellmain */# include "csapp. H "# define maxargs 128/* function prototypes */void eval (char * argument line); int parseline (char * Buf, char ** argv ); int builtin_command (char ** argv); int main () {Char Using line [maxline];/* command line */while (1) {/* read */printf (">"); fgets (linear line, maxline, stdin); If (feof (stdin) Exit (0 ); /* evaluate */eval (cmdline);}/* $ end shellmain * // * $ begin eval * // * E Val-evaluate a command line */void eval (char * argument line) {char * argv [maxargs];/* argv for execve () */Char Buf [maxline]; /* holds modified command line */int bg;/* shocould the job run in BG or fg? */Pid_t PID;/* process ID */strcpy (BUF, queue line); BG = parseline (BUF, argv); If (argv [0] = NULL) return; /* ignore empty lines */If (! Builtin_command (argv) {If (pid = fork () = 0) {/* child runs User Job */If (execve (argv [0], argv, environ) <0) {printf ("% s: Command not found. \ n ", argv [0]); exit (0) ;}}/* parent waits for foreground job to terminate */If (! BG) {int status; If (waitpid (PID, & status, 0) <0) unix_error ("waitfg: waitpid error ");} else printf ("% d % s", PID, cmdline);} return;}/* If first Arg is a builtin command, run it and return true */INT builtin_command (char ** argv) {If (! Strcmp (argv [0], "quit")/* Quit command */exit (0); If (! Strcmp (argv [0], "&")/* ignore Singleton & */return 1; return 0; /* not a builtin command */}/* $ end eval * // * $ begin parseline * // * parseline-parse the command line and build the argv array */INT parseline (char * Buf, char ** argv) {char * delim;/* points to first space delimiter */INT argc;/* Number of argS */int bg;/* Background job? */Buf [strlen (BUF)-1] = ''; /* replace trailing '\ N' with space */while (* Buf & (* Buf = '')/* ignore leading Spaces */Buf ++; /* build the argv list */argc = 0; while (delim = strchr (BUF, '') {argv [argc ++] = Buf; * delim = '\ 0'; Buf = delim + 1; while (* Buf & (* Buf = '')/* ignore Spaces */Buf ++ ;} argv [argc] = NULL; If (argc = 0)/* ignore blank line */return 1;/* shoshould the job run In the background? */If (BG = (* argv [argc-1] = '&'))! = 0) argv [-- argc] = NULL; return BG;}/* $ end parseline */
<Computer systems: a programmer's perspective>
Note:[1]Original book8.4This section describes in detail.