A process concept
1. Process generation
Operation runs a program that produces at least one process
2.Linux Create Process
struct TASK_STRUCT
{
Process Number (PID)
Parent process Number (PPID)
Run address space
A set of register values
Process status
....
};
Question: What is the difference between a process and a program?
Answer: The program is a compiled executable file, and the process is created by the operating system in order to execute a program, the process is a program execution process
Two process-related commands
1.ps-ef
View the PID and ppid of a process
2.ps aux
View process status R: Run State S: interruptible wait State D: non-interruptible wait state T: Stop State Z: Zombie State (PID resource not released)
3.kill
<1>kill-l viewing signals in the system
<2> send signal to the specified process
Kill-Signal PID
For example:
Kill-sigstop 5207
4.killall
Signaling processes of the same name
Killall-Signal Process name
5. View the maximum number of processes allowed by the system
Cat/proc/sys/kernel/pid_max-32768
Three Create child processes
pid_t fork (void);
Features: Creating child processes
return value:
The PID of the child process is successfully returned to the parent process, returns 0 to the child process, and the failure returns-1
Think: How do I create a subprocess?
Answer: Copy parent process
Zombie Process: The process ends, but the parent process does not dispose of it
Orphan process: The father process is over, the child process will become an orphan process, the orphan process will be automatically adopted by the INIT process
Question: Why do I create a child process?
Answer: The purpose of creating a child process is to have the child process perform a separate task
Issue: Child and parent processes will not affect each other
Answer: No, the address space of the parent-child process is independent
Practice:
Create two child processes, child process 1 output "Hello word" and then end,
Child Process 2 makes while (1), the parent process makes while (1)
The four parent process recycles the zombie child process
pid_t Wait (int *status);
function: Wait for zombie child process
Parameters:
@status to get the state of the zombie child process
return value:
Successfully returned zombie subprocess PID, failed to return 1 (No child process)
Common usage: Wait (NULL);
pid_t waitpid (pid_t pid, int *status, int options);
Parameters:
@pid-1: Wait for any one child process to exit the PID of the subprocess: Wait for the specified PID child process
@status get the status of the child process exit
@options 0: Blocking mode call Wnohang: Non-blocking method invocation
return value:
Successful return of the zombie subprocess PID, failed to return 1, if no child process is in a zombie state, and non-blocking mode call, return immediately, return value is 0
Waitpid ( -1,null,0); = = Wait (NULL);
Waitpid (pid,null,0);
Waitpid ( -1,null,wnohang);//Common usage
Waitpid (Pid,null,wnohang);
Five end a process
Return: Use return in the main function to end the process
_exit (): Used to end a process
Exit (): Used to end a process, flush the cache
Exit (exit_success) success
Exit (Exit_failure) failed
For example:
FD = open (...);
if (FD < 0) {
fprintf (stderr, "...");
Exit (Exit_failure);
}
Six run another program in one process (start another process)
exec function Family:
Executes a program in a process that replaces all resources (code snippets, data segments, ...) of the original process, leaving only the PID of the original process
Process for bash Execution program:
Bash---> < fork---> Child processes >----> Call the EXEC function family----> Execute our executable program
int execl (const char *path, const char *arg, ...);
L:list
int execl (executable path, executable file name, pass parameter 1, pass parameter 2,..., NULL);
For example:
Executes the/bin/ls, passing the parameter is-l
Execl ("/bin/ls", "ls", "-l", NULL);
P:path
int EXECLP (const char *file, const char *arg, ...);
For example:
Executes the/bin/ls, passing the parameter is-l
EXECLP ("ls", "ls", "-l", NULL);
V:vetcor (parameters are stored in the pointer array)
int execv (const char *path, char *const argv[]);
For example:
Executes the/bin/ls, passing the parameter is-l
Char *arg[] = {"ls", "-L", NULL};
EXECV ("/bin/ls", Arg);
int EXECVP (const char *file, char *const argv[]);
int execle (const char *path, const char *arg, ..., char * const envp[]);
Linux System Programming Summary (1)