The program cannot directly access the data (hardware: The kernel provides syscall, syscall method can not be called directly (protected mode), so there is a soft interrupt, interrupt function in the CPU, THE CPU will according to the interrupt number to the kernel to find the corresponding callback…
Program testing
The following is a simple example to explain, the following code is a test socket simple method, should be better to understand (in fact, through multithreading to solve the problem of multiple connections), the following Java file in Linux execution see
public class TestSocket {
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(8090);
System.out.println("step1:new ServerSocket(80)");
while (true){
Socket client = server.accept();
System.out.println("step2:client\t"+client.getPort());
new Thread(() ->{
try {
InputStream in = client.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
while (true){ System.out.println(reader.readLine()); }}catch(IOException e) { e.printStackTrace(); } }).start(); }}}Copy the code
Program to grab
Use the following command to find out if the above program execution system call to the kernel
strace -ff -o ./ooxx java TestSocket
Copy the code
After execution, you can see that the first sentence is printed, indicating that the program has executed and blocked
Enter this folder through another window and find a bunch of files generated
In fact, these files are threads, so which file is the main thread? You can find it by the keyword in the code
Grep 'step1'./*Copy the code
Write (1, “step1:new ServerSocket(80)”, 26) = 26″
You can open this file and find out where this operation is in the file (we’ll see what that means later).
Process thread analysis
Threads can also be viewed through JPS
In Linux, everything is a file. You can see what’s in the 162 process. You can go to the /proc/process ID directory in the Linux root directory and see a bunch of things
First look at the task file, open the above will find whether these Numbers and sex. * is the same, yes actually this thread in the process of the JDK, know you can view your application after the number of threads
There’s also a directory fd, and you can see some data in there and there’s something called a file descriptor, any program has IO, 0, 1, 2 are the three basic IO
- 0 standard input
- 1 Standard Output
- 2 Error Output
These streams are objects in Java and files in the operating system, and are represented numerically, similar to variables in Java
- 3 and 4 are libraries specific to Java programs
- 5, 6 is actually our program started after the server will listen to 8090, the two divisions are ipv4, ipv6
Run the netstat -natp command. Note that state is in the LISTEN state. Only the server is in the LISTEN state. Clients can connect to the server through this port number
System call Analysis
If you connect with the NC command, you can see that there is one more record in the Java program and one more socket in the file descriptor than before
nc localhost 8090
Copy the code
The network connection
File descriptor
Then at the beginning of the folder where the program is located, find the main thread, open after the search first connected port number 49482; You can see
Accept (6, {sa_family=AF_INET, sin_port=htons(49482), sin_addr=inet_addr(“127.0.0.1”)}, [16]) = 7
Recall that the previous method was server.accept(), and the kernel also has accept, so you can understand that some Java methods are wrapper around the kernel; In the files of the main thread, you can see the generated file descriptors and the specific system calls that occur during program execution
You can also use the man command to see what the various commands do; I’ll stop there, and I’ll talk a little bit more about NIO