Operating system and computer network serial questions, how many questions can you stick to?


foreword

As a JAVA development programmer, you can't just stare at JAVA and databases in the process of learning, but also take into account basic computer courses such as computer networks and operating systems. This article summarizes the computer network and operating system questions frequently asked in interviews for your reference and study.


1. Operating system

What is the difference between a thread and a process?

1. The process is the unit of resource allocation, and the thread is the basic unit of independent scheduling and dispatching.
2. A process can contain multiple threads, a thread can only belong to one process, and a thread cannot run independently of the process;
3. As a basic unit with resources, a process has only one essential element that can ensure independent operation. Resources (thread control block TCB, program counter, a set of registers and stacks)
4. The resources consumed by the OS for creating, canceling, and scheduling processes are significantly greater than the overhead of threads
5. The independence between processes is higher than that of threads, because threads share The address space and resources in the process (the process can be regarded as a class, and the thread is the students in it. The school allocates 500 yuan of funds to the class, which is equivalent to the allocated system resources, and these students share these funds.)


What are the methods of process communication?

1. Pipes: If you have learned Linux commands, you must be familiar with the vertical bar " | ". The vertical bar " | " in the above command line is a pipe, and its function is to use the output of the previous command ( ps auxf ) as the input of the next command ( grep mysql ). From this function description, It can be seen that the pipeline transmits data in one direction. If we want to communicate with each other, we need to create two pipelines.

For example: ps -ef | grep process name: to view the process number of the specified name, first output all pipeline commands to grep name filter to filter out the specified process information.

Features: The data written by a process is cached in the kernel, and when another process reads data, it is naturally obtained from the kernel. The pipeline is a low-efficiency communication method and is not suitable for frequent data exchange between processes. Of course, its advantage is of course simplicity, and at the same time we can easily know that the data in the pipeline has been read by another process.

2. Message queue: As mentioned earlier, the communication method of pipeline is inefficient, so pipeline is not suitable for frequent exchange of data between processes. For this problem, the communication mode of the message queue can be solved. For example, if process A wants to send a message to process B, process A can return the data normally after putting the data in the corresponding message queue, and process B can read the data when needed. In the same way, it is the same for the B process to send a message to the A process.

Features: In the message model, the communication between two processes is just like sending an email. You send one, I will reply, so you can communicate frequently. However, there are two deficiencies in the communication method of e-mail. One is that the communication is not timely, and the other is that the attachments are also limited in size. This is also the point of insufficient communication in message queues.

3. Shared memory: In the process of reading and writing message queues, there will be a message copying process between user mode and kernel mode. The shared memory method solves this problem very well. The mechanism of shared memory is to take out a piece of virtual address space and map it to the same physical memory. In this way, the things written by this process can be seen by another process immediately, and there is no need to copy and copy, and pass them around, which greatly improves the speed of inter-process communication.

Features: The shared memory communication method is used, which brings new problems, that is, if multiple processes modify the same shared memory at the same time, it is likely to conflict. For example, if two processes write an address at the same time, the process that writes first will find that the content has been overwritten by others.

4. Semaphore: In order to prevent data confusion caused by multi-process competition for shared resources, a protection mechanism is required so that shared resources can only be accessed by one process at any time. As it happens, the semaphore implements this protection mechanism.

A semaphore is actually an integer counter, which is mainly used to achieve mutual exclusion and synchronization between processes, not to cache data communicated between processes. The semaphore represents the number of resources, and there are two atomic operations for controlling the semaphore:

1. One is the P operation , which subtracts 1 from the semaphore. If the semaphore < 0 after subtraction, it means that the resource is occupied and the process needs to block and wait; after the subtraction, if the semaphore >= 0, It indicates that there are still resources available, and the process can continue to execute normally.

2. The other is the V operation . This operation will add 1 to the semaphore. If the semaphore <= 0 after the addition, it means that there is a currently blocked process, so the process will wake up and run; after the addition If the semaphore is > 0, it means that there is currently no blocking process;

Features: The P operation is used before entering the shared resource, and the V operation is used after leaving the shared resource. These two operations must appear in pairs

5. Signal: The inter-process communication mentioned above is in the normal state. For the working mode under abnormal conditions, it is necessary to notify the process by means of a signal.

For example: if the process is running in the background, you can send a signal to the process through the kill command, but you need to know the PID number of the running process, for example: kill -9 1050, which means to send the SIGKILL signal to the process whose PID is 1050 , which is used to immediately terminate the process


What are the common commands in Linux?

View Files

cd … to go to the root directory or go back to the previous directory.

ls to view files in a directory

ls -a show all file directories (including hidden files)

ls -l displays file and directory details

pwd: show the current directory

mkdir: create directory

touch : create file

rm: delete files

rm -rf: means that no matter how many levels of directories there are, they will be deleted at the same time

View processes and pipes

ps -aux to view all processes

ps -ef : Display all process information in full format, including parent process Pid, ​​creator, creation time, process ID
kill -9 [PID] #-9 means to force the process to stop immediately

For example: ps -ef | grep process name: to view the process number of the specified name, first output all pipeline commands to grep name filter to filter out the specified process information. Check whether the MySQL service is currently started ps -ef | grep mysql

ps -ef|grep xxx.jar to view the process number of the currently running jar package program

top: If you want to dynamically display process information, you can use the top command, and you can also see the cpu utilization

vim compiler

vim filename

i enter edit mode

Esc to exit edit mode

q! If you have modified the file and do not want to save it, use ! to force quit without saving the file.

:wq save and leave

unzip command

tar-zxvf decompress gz file

tar-czvf compressed file

Linux view file line number command

1. tail -n - digital file name means to view the last few lines of the file

2. tail -n + number file name means to view a certain line of the file to the last line

3. head -n digital file name means to view the contents of the first few lines of the file


What is deadlock?

Four conditions for deadlock:

1. A resource can only be used by one thread at a time (a fork can only be used by one philosopher)

2. When a thread blocks and waits for a resource, it does not release the resource already occupied (even if the second fork cannot be obtained, the philosopher will not give up the already obtained fork)

3. The resources that a thread has obtained cannot be taken away by a strong surname before it is used up (the fork cannot be taken away from other philosophers)

4. Several threads form a head-to-tail loop waiting for a resource relationship (each philosopher is waiting for the next person to release the fork, forming a ring)

5. These are the 4 conditions that must be met to cause deadlock! If you want to avoid deadlock, you only need to dissatisfaction with one of these conditions. However, the first three conditions are the conditions to be met as locks, so to avoid deadlocks, the fourth condition needs to be broken, and there is no cyclic waiting for locks. The most common method to break this condition is the orderly allocation of resources.

Prevent deadlock :

As a programmer, during development:

1. Pay attention to the locking order to ensure that each thread locks in the same order

2. Pay attention to the locking time limit, you can set a timeout time for the set

3. Pay attention to deadlock checking, which is a preventive mechanism to ensure that deadlocks are discovered and resolved in the first place


What is the difference between IO, BIO, AIO and NIO?

Synchronous blocking (blocking-IO) referred to as BIO: go to a restaurant to eat, just wait in line there, wait for an hour, it is your turn, and then start eating, this is BIO, the whole process is synchronous blocking. A typical example is data=socket.read(), if the kernel data is not ready, the Socket thread will always block in read() waiting for the kernel data to be ready.

Synchronous non-blocking (non-blocking-IO) referred to as NIO: going to a restaurant for a meal, it takes a long time to wait, so you can go to the mall, and every time you visit,

I ran back to see if I had waited. So in the end she not only shopping, but also eating. For example, the non-blocking IO model and the signal-driven IO model are NIO.

Asynchronous-non-blocking-IO (AIO) for short If you wait, the store manager will call you. The asynchronous IO model is AIO.

Java NIO (Non Blocking IO) is a new IO API introduced since Java 1.4, which can replace the standard Java IO API. NIO supports buffer-oriented, channel-based IO operations. NIO will read and write files in a more efficient way.

The core parts of Java NIO consist of :

1.Channels: Channel, which can be translated into "channel". The Stream (stream) in Channel and IO is almost the same level. It's just that Stream is one-way, such as InputStream, OutputStream; and Channel is two-way, which can be used for both read and write operations. Because Channels are full-duplex, they map the underlying operating system's APIs better than Streams.

2.Buffers: Buffers in Java NIO are used to interact with NIO channels. Data is read from the channel into the buffer, and written from the buffer to the channel. A buffer is essentially a block of memory to which data can be written and then read from it. This piece of memory is wrapped as a NIO Buffer object and provides a set of methods for easy access to this piece of memory.

3.Selectors : generally known as selectors. It is one of the core components of Java NIO and is used to check whether the state of one or more NIO Channels is readable and writable. In this way, a single thread can manage multiple channels, that is, multiple network links can be managed.

The advantage of using Selector is that it uses fewer threads to process the channel, avoiding the overhead of thread context switching compared to using multiple threads.


2. Computer network

Common HTTP Status Codes

1XX——Indicates prompt information

2XX - the request was processed successfully

  1. 200: The response header has body data
  2. 204: no body data
  3. 206: body is a partial resource

3XX - means redirection, the resource location has changed

  1. 301: Permanent redirect
  2. 302: Temporary redirect
  3. 303: Cache redirect

4XX - Indicates a client error

  1. 400 Bad Request: The server cannot understand the request sent by the client, and there may be syntax errors in the request message.
  2. 403 Forbidden : Access to that resource is not allowed. This status code indicates that access to the requested resource was denied by the server . (permissions, unauthorized IPs, etc.)
  3. 404 Not Found: The requested resource is not available on the server, maybe the path is wrong, etc.

5XX - An error occurred on the server itself

  1. 500 Internal Server Error: It looks like an internal resource has failed. This status code indicates that an error occurred on the server side while executing the request. It is also possible that there is a bug in the web application or some temporary failure.
  2. 503 Service Unavailable: This status code indicates that the server is temporarily overloaded or is down for maintenance and cannot process requests now.

The difference between HTTP1 and 2

HTTP2 is an upgrade to HTTP1.1. It optimizes the performance of HTTP1.1, mainly from the following two aspects: header compression and queue header blocking.

  1. HTTP1.1 mainly compresses the body, but the header is not compressed. HTTP2 compresses the header, reducing the transmission time.
  2. HTTP1.1 uses the TCP protocol, and in order to save resources, a long connection is used, which introduces the problem of head-of-line blocking. HTTP2 introduced streams and frames to solve head-of-line blocking at the HTTP level.

The difference between get and post

1. Semantic difference:

We usually define Get as "reading" a resource, such as reading all student information, and using POST for "creating a resource, such as adding a student record.

2. In terms of security:

We often hear that GET is not as secure as POST, because POST uses body to transmit data, while GET uses url transmission, which is easier to see. But from an attack point of view, neither GET nor POST is secure enough, because HTTP itself is a clear text protocol . In order to avoid data theft during transmission, end-to-end encryption from the client to the server must be performed. The common practice in the industry is https .

3. Cache:

Because GET is a read and idempotent does not modify the server state, the data of the GET request can be cached, and this cache can be done on the browser itself (to completely avoid the browser sending requests). **And POST is used to "create a resource", this action is not idempotent and modifies the server state, so it cannot be cached. **For example: If the POST request is cached by the browser, if there is an order request, you can directly return to the locally cached "order successful interface" instead of sending a request to the server, without actually placing an order on the server , which can cause problems.

4. Request parameters

Important: From the perspective of the protocol itself, there is no restriction that GET must not have no body, and POST must not put parameters on the URL. Therefore, the format can be used more freely.

Disadvantages: Of course, too much freedom also brings another kind of trouble. Developers have to discuss every time whether to put parameters in the path of the url or in the body, which is too inefficient.

Solution: So there are some serial interface specifications/styles. One of the most famous is REST. REST makes full use of GET, POST, PUT, and DELETE, and stipulates that these four interfaces obtain, create, modify, and delete "resources" respectively. REST best practices also recommend using json format in the request body. In this way, you can understand what the interface means just by looking at the HTTP method, and the parsing format has also been unified.

5. Packets

In general, GET produces one TCP packet; POST produces two TCP packets.

For GET requests, the browser will send the header and data together, and the server responds with 200 (returning data); for POST, the browser sends the header first, the server responds with 100 continue, the browser sends the data, and the server responds with 200 ok (return data).

For example: that is to say, GET only needs one car trip to deliver the goods, while POST has to make two trips, the first trip, go and say hello to the server "Hey, I'm going to deliver a batch of goods later, You open the door to meet me," before turning back to deliver the goods.


TCP acknowledgment and retransmission mechanism

One of the ways that TCP achieves reliable transmission is through sequence numbers and acknowledgments.

Acknowledgment response: In TCP, when the data from the sender reaches the receiver, the receiver host will return an acknowledgment message, indicating that the message has been received
insert image description here

In a complex network, normal data transmission may not be possible as shown in the above figure, and data may be lost during the transmission process. Therefore, TCP will use the retransmission mechanism to solve the problem of packet loss.

Common retransmission mechanism

There are four kinds:

  • Timeout retransmission : One of the ways of the retransmission mechanism is to set a timer when sending data . When the specified time is exceeded , the data will be retransmitted RTOwithout receiving the confirmation response message from the other party , that is, timeout retransmissionACK
  • Fast retransmission : Therefore, fast retransmission works by retransmitting the lost segment before the timer expires when three identical ACK packets are received.
  • SACK : SACK (Selective Acknowledgment Selective Acknowledgment). It will add SACKsomething to the [options] field of the TCP header, and it can send the cached map to the sender , so that the sender can know which data has been sent and which data has not been received. Knowing this information, to retransmit only the lost data
  • D-SACK: Duplicate SACK, also known as D-SACK, mainly uses SACK to tell [sender] which data has been received repeatedly .

TCP flow control and congestion control

flow control

If the sender sends the data too fast, the receiver may not receive it in time, which will result in data loss. The so-called flow control is to let the sender's sending rate not be too fast, and let the receiver have time to receive. Using the sliding window mechanism, it is very convenient to implement flow control on the sender on the TCP connection.

Let A send data to B. When the connection is established, B tells A: "My receive window is rwnd = 400" (rwnd here means receiver window). Therefore, the sender's send window cannot exceed the value of the receive window given by the receiver.

congestion control

Congestion control is to allow the network to bear the existing network load. It is a global process involving all hosts, all routers, and all factors related to reducing network transmission performance.

Flow control often refers to the control of point-to-point traffic, that is, the receiver controls the sender, and what it needs to do is to suppress the rate at which the sender sends data so that the receiver can receive it in time. If the sender sends data too fast and the receiver cannot receive it in time, data may be lost.

Several congestion control methods:

Slow-start, congestion avoidance, fast retransmit, and fast recovery.

Slow start algorithm: When a host starts to send data, if a large amount of data bytes are injected into the network immediately, it may cause network congestion, because it is not clear about the load of the network. Therefore, a better method is to probe first, that is, gradually increase the sending window from small to large, that is, gradually increase the value of the congestion window from small to large.

Congestion avoidance algorithm: Let the congestion window cwnd increase slowly, that is, increase the sender's congestion window cwnd by 1 after each round-trip time RTT, instead of doubling it. In this way, the congestion window cwnd grows slowly according to a linear law, which is much slower than the growth rate of the congestion window of the slow start algorithm.

Fast retransmission requires the receiver to send a duplicate acknowledgment immediately after receiving an out-of-sequence segment (in order to let the sender know early that a segment has not reached the other party) instead of waiting until it sends data by piggybacking the acknowledgment. The fast retransmission algorithm stipulates that as long as the sender receives three repeated acknowledgments in a row, it should immediately retransmit the segment that has not been received by the other party, without continuing to wait for the set retransmission timer to expire.

The "fast recovery" algorithm is added after the above "fast retransmission" algorithm. When three duplicate ACKs are received, TCP does not enter the congestion avoidance stage at the end, but the fast recovery stage. The fast retransmission and fast recovery algorithms are generally use simultaneously.


Summarize

This article mainly introduces some common problems of computer networks and operating systems, such as the difference between threads and processes, and the common commands of Linux operating systems are high-frequency interview questions. This article has a lot of content, and more needs to be remembered.


Supongo que te gusta

Origin blog.csdn.net/qq_52173163/article/details/126951059
Recomendado
Clasificación