The process of programming Linux systems
One, operating system responsibilities: management of resources, the different devices and different programs associated.
Second, the system call is the operating system provided to the user program of a special set of function interface, the user program through this set of interfaces to obtain the services provided by the system kernel, such as: Open files, close files, read and write files.
Third, POSIX (Portable Operation System Interface)
In Linux, the Application Programming Interface (API) adheres to the POSIX standard, which ensures that applications can be ported on multiple operating systems at the source level.
Four, the system call according to the function logic can be divided into: Process Control, interprocess communication, File system control, system control, memory management, network management, socket control, user management.
A negative return value is usually used to indicate an error, and a value of 0 indicates a successful perror print error message
V. Process
Program: Executable (Static), process: execution instance of the program (dynamic)
Status of the process: Create, schedule, perish
Process has its own environment and resources
Use the EXEC function to read the program from the kernel into memory so that it executes as a process.
Vi. the life cycle of the process
Create
Scheduling
Ready state: Execution conditions are in place, waiting for CPU time to be allocated
Execution state: Consuming CPU
Wait state: Some execution conditions are not available to continue execution
Die
Vii. PCB (Process control block)
The OS controls and manages the concurrent execution of the process based on the PCB, which opens up a memory space during the creation of a process to hold the PCB data structures associated with this process
The PCB records all the information needed to describe the progress of the process and to control the operation of the process.
PCB is the only sign of process existence, Linux PCB stored in the TASK_STRUCT structure, open/include/linux/sched.h can find the definition of task_struct
Viii. Process Control
Process number 0~32767 No. 0 dispatch process, number 1th init process
In addition to the scheduling process, all processes are created directly or indirectly by the INIT process.
PID process number Getpid () ppid Parent Process number Getppid () Pgid Process Group number Getpgid ()
IX. file Descriptor: Allocate 0-1023 minimum available descriptors in process units
X. Process state Transitions
CPU Scheduling algorithm: Priority, first-come first scheduling, short time first, time slice rotation
Umask Mask, masking file permissions
PS Command View current process
Xi. Creating a Process fork () vfork ()
1.fork () returns 0 in the subprocess, returns a value greater than 0 in the parent process, after calling fork (), the source program is copied a copy, two parts of the program only Fork () The return value is not the same, two copies of the program executed at the same time, the parent-child process physical address is different, the virtual address is the same.
Q: If a program executes three fork () at the same time, it creates several processes
Fork (); 1 (0)
Fork (); 2 (0) 3 (1)
Fork (); 4 (0) 5 (1) 6 (2) 7 (3)
First call, Process # No. 0 created process # 1th
Second call, process number NO. 0 creates process number 2nd, process 1th creates process number 3rd
Third call, process No. 0 created process 4th, process 1th created process 5th, process 2nd created process 6th, process 3rd has created process 7th
Altogether created 2 of the n-th minus one process
2.vfork () generally with exec, the child process uses the parent process address space to ensure that the child process runs first, calling the Exec or exit stepfather process to run again
The EXEC function family is a process substitution function that assigns the new address space to the original process, the process number is unchanged, and the new program runs from main
exec only fails to return, the code behind exec does not execute
But note that exec is a family of functions, with EXECLP,EXECVP,EXECLE,EXECVE, etc.
The child process can only be terminated with exit (), and return is the first address returned to the function execution
Atexit (void (*function) (void)) Register exit handler function
12. Input/Output buffers
System Call No buffering
Standard IO library function input and output as row buffer
Standard IO library operation file is fully buffered
13.
Zombie Process: The child process ends and the parent process does not call wait () or waitpid () to reclaim its resources
Orphan process: The parent process ends and the child process does not end
Daemon: Special orphan process, out-of-terminal running in the background
Wait ()/waitpid (): Waits for the child process to end and reclaim its resources
Use PS to view process, kill+pid or pkill+ process name end process
The process of programming Linux systems