Nagle's algorithm

Nagle算法旨在通过减少发送的小数据包数量来提高TCP/IP网络效率。该算法通过将多个小数据包合并为一个较大的数据包来发送,从而降低网络拥塞的可能性。然而,对于实时应用如网络游戏等,该算法可能导致延迟增加。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Nagle's algorithm

From Wikipedia, the free encyclopedia

Nagle's algorithm, named after John Nagle, is a means of improving the efficiency of TCP/IP networks by reducing the number of packets that need to be sent over the network.

Nagle's document, Congestion Control in IP/TCP Internetworks (RFC 896) describes what he called the 'small packet problem', where an application repeatedly emits data in small chunks, frequently only 1 byte in size. Since TCP packets have a 40 byte header (20 bytes for TCP, 20 bytes for IPv4), this results in a 41 byte packet for 1 byte of useful information, a huge overhead. This situation often occurs in Telnet sessions, where most keypresses generate a single byte of data that is transmitted immediately. Worse, over slow links, many such packets can be in transit at the same time, potentially leading to congestion collapse.

Nagle's algorithm works by combining a number of small outgoing messages, and sending them all at once. Specifically, as long as there is a sent packet for which the sender has received no acknowledgment, the sender should keep buffering its output until it has a full packet's worth of output, so that output can be sent all at once.

Contents

[hide]

[edit] Algorithm

if there is new data to send
  if the window size >= MSS and available data is >= MSS
    send complete MSS segment now
  else
    if there is unconfirmed data still in the pipe
      enqueue data in the buffer until an acknowledge is received
    else
      send data immediately
    end if
  end if
end if

where MSS = maximum segment size.

This algorithm interacts badly with TCP delayed acknowledgments, a feature introduced into TCP at roughly the same time in the early 1980s, but by a different group. With both algorithms enabled, applications that do two successive writes to a TCP connection, followed by a read that will not be fulfilled until after the data from the second write has reached the destination, experience a constant delay of up to 500 milliseconds, the "ACK delay". For this reason, TCP implementations usually provide applications with an interface to disable the Nagle algorithm. This is typically called the TCP_NODELAY option. It is, however, not recommended to disable this. A solution, recommended by Nagle himself, is to keep from sending small single writes and buffer up application writes then send (or with the help of writev() call).

"The user-level solution is to avoid write-write-read sequences on sockets. write-read-write-read is fine. write-write-write is fine. But write-write-read is a killer. So, if you can, buffer up your little writes to TCP and send them all at once. Using the standard UNIX I/O package and flushing write before each read usually works."[1]

Thus, you leave the Nagle algorithm on, but basically don't use it because you call flush on your writes.

The tinygram problem and silly window syndrome are sometimes confused. The tinygram problem occurs when the window is almost empty. Silly window syndrome occurs when the window is almost full.

[edit] Interactions with real-time systems

Applications that expect real time responses can react poorly with Nagle's algorithm. Applications such as networked multiplayer video games expect that actions in the game are sent immediately, while the algorithm purposefully delays transmission, increasing bandwidth at the expense of latency. For this reason applications with low-bandwidth time-sensitive transmissions typically use TCP_NODELAY to bypass the Nagle delay.[2]

转载于:https://www.cnblogs.com/xiayong123/archive/2011/08/27/3717236.html

### 使用MATLAB实现G-S算法进行图像或信号重建 #### G-S算法简介 Gerchberg-Saxton (G-S) 算法是一种用于相位恢复的经典迭代算法,在光学领域广泛应用。该算法通过交替约束幅度和相位来逐步逼近原始信号。 #### MATLAB代码示例 下面是一个简单的MATLAB程序,展示了如何利用G-S算法来进行一维信号的重建: ```matlab function [recovered_signal, error] = GS_algorithm(magnitude_spectrum, initial_guess, max_iterations) % 初始化变量 recovered_signal = initial_guess; N = length(received_signal); for iter = 1:max_iterations % 傅里叶变换到频域并施加幅度谱约束 freq_domain = fftshift(fft(ifftshift(recovered_signal))); constrained_freq = magnitude_spectrum .* exp(1j * angle(freq_domain)); % 反傅里叶变换成时域并保持初始猜测的相位分布 time_domain = real(ifftshift(ifft(fftshift(constrained_freq)))); recovered_signal = abs(initial_guess) .* exp(1j * angle(time_domain)); % 计算误差 error(iter) = norm(abs(freq_domain) - magnitude_spectrum); % 如果误差足够小则提前终止循环 if error(end) < 1e-6 break; end end end ``` 对于二维图像的情况,只需将上述的一维FFT函数替换为`fft2()` 和 `ifft2()`, 同样地也需要调整输入参数以适应矩阵形式的数据结构[^1]。 为了提高效率以及更好地控制边界条件,还可以考虑引入更多的优化措施,比如自适应步长调节机制或是加入额外的空间域平滑操作等高级特性[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值