
jdk中的timeout属性:貌似SO_TIMEOUT是连接超时时间,并不是读数据的超时时间,unix中好像本来是没有SO_TIMEOUT这样的TCP值选项的。
HttpURLConnection是基于HTTP协议的,其底层通过socket通信实现。如果不设置(timeout),在网络异常的情况下,可能会导致程序僵死而不继续往下执行。
如果是Win2000、WinXP系统:
系统变量->新建->变量名:JAVA_HOME 变量值:JDK安装目录。
系统变量->新建->变量名:CLASSPATH 变量值:.%JAVA_HOME%\lib。
系统变量->编辑->变量名:Path 在变量值的最前面加上:%JAVA_HOME%\bin。
c.如果是Vista、Win7、Win8系统,使用鼠标右击“计算机”->属性->左侧高级系统设置->高级->环境变量。
502badgateway要先找到nginx配置的路径。然后找到nginx所在的error日志文件来查看具体原因。
如果是客户端浏览器配置的问题,以360浏览器为例,出现502BadGateway可能是设置了代代理导致的。
取消浏览器代理之后,刷新一下就可以访问了。
502BadGateway是一种报错提示,这一错误并不意味着上游服务器已关闭(无响应网关/代理),而是上游服务器和网关/代理不同意的协议交换数据。
鉴于互联网协议是相当清楚的,它往往意味着一个或两个机器已不正确或不完全编程。
timerfd是Linux为用户程序提供的一个定时器接口。这个接口基于文件描述符,通过文件描述符的可读事件进行超时通知,所以能够被用于select/poll的应用场景。
timerfd是linux内核2.6.25版本中加入的借口。
timerfd、eventfd、signalfd配合epoll使用,可以构造出一个零轮询的程序,但程序没有处理的事件时,程序是被阻塞的。这样的话在某些移动设备上程序更省电。
clock_gettime函数可以获取系统时钟,精确到纳秒。需要在编译时指定库:-lrt。可以获取两种类型事件:
CLOCK_REALTIME:相对时间,从1970.1.1到目前的时间。更改系统时间会更改获取的值。也就是,它以系统时间为坐标。
CLOCK_MONOTONIC:与CLOCK_REALTIME相反,它是以绝对时间为准,获取的时间为系统重启到现在的时间,更改系统时间对齐没有影响。
timerfd_create:
生成一个定时器对象,返回与之关联的文件描述符。接收两个入参,一个是clockid,填写
CLOCK_REALTIME或者CLOCK_MONOTONIC,参数意义同上。第二个可以传递控制标志:TFD_NONBLOCK(非阻
塞),TFD_CLOEXEC(同O_CLOEXEC)
注:timerfd的进度要比usleep要高。
timerfd_settime:能够启动和停止定时器;可以设置第二个参数:flags,0表示是相对定时器,TFD_TIMER_ABSTIME表示是绝对定时器。
第三个参数设置超时时间,如果为0则表示停止定时器。定时器设置超时方法:
1、设置超时时间是需要调用
clock_gettime
获取当前时间,如果是绝对定时器,那么需要获取
CLOCK_REALTIME,在加上要超时的时间。如果是相对定时器,要获取
CLOCK_MONOTONIC时间。
2、数据结构:
struct timespec {
time_t tv_sec /* Seconds */
long tv_nsec /* Nanoseconds */
}
struct itimerspec {
struct timespec it_interval /* Interval for periodic timer */
struct timespec it_value/* Initial expiration */
}
it_value是首次超时时间,需要填写从
clock_gettime获取的时间,并加上要超时的时间。
it_interval是后续周期性超时时间,是多少时间就填写多少。
注意一个容易犯错的地方:tv_nsec加上去后一定要判断是否超出1000000000(如果超过要秒加一),否则会设置失败。
it_interval不为0则表示是周期性定时器。
it_value和
it_interval都为0表示停止定时器。
注: timerfd_create第一个参数和
clock_gettime的第一个参数都是
CLOCK_REALTIME或者
CLOCK_MONOTONIC,
timerfd_settime的第二个参数为0(相对定时器)或者TFD_TIMER_ABSTIME,三者的关系:
1、如果
timerfd_settime设置为
TFD_TIMER_ABSTIME(决定时间),则后面的时间必须用
clock_gettime来获取,获取时设置
CLOCK_REALTIME还是
CLOCK_MONOTONIC取决于
timerfd_create设置的值。
2、如果
timerfd_settime设置为
0(相对定时器),则后面的时间必须用相对时间,就是:
new_value.
it_value
.tv_nsec = 500000000
new_value.
it_value
.tv_sec = 3
new_value.
it_interval
.tv_sec = 0
new_value.
it_interval
.tv_nsec = 10000000
read函数可以读timerfd,读的内容为uint_64,表示超时次数。
看一段代码例子:
#include <sys/timerfd.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h> /* Definition of uint64_t */
#define handle_error(msg) \
do { perror(msg)exit(EXIT_FAILURE)} while (0)
void printTime()
{
struct timeval tv
gettimeofday(&tv, NULL)
printf("printTime: current time:%ld.%ld ", tv.tv_sec, tv.tv_usec)
}
int main(int argc, char *argv[])
{
struct timespec now
if (clock_gettime(CLOCK_REALTIME, &now) == -1)
handle_error("clock_gettime")
struct itimerspec new_value
new_value.it_value.tv_sec = now.tv_sec + atoi(argv[1])
new_value.it_value.tv_nsec = now.tv_nsec
new_value.it_interval.tv_sec = atoi(argv[2])
new_value.it_interval.tv_nsec = 0
int fd = timerfd_create(CLOCK_REALTIME, 0)
if (fd == -1)
handle_error("timerfd_create")
if (timerfd_settime(fd, TFD_TIMER_ABSTIME, &new_value, NULL) == -1)
handle_error("timerfd_settime")
printTime()
printf("timer started\n")
for (uint64_t tot_exp = 0tot_exp <atoi(argv[3]))
{
uint64_t exp
ssize_t s = read(fd, &exp, sizeof(uint64_t))
if (s != sizeof(uint64_t))
handle_error("read")
tot_exp += exp
printTime()
printf("read: %llutotal=%llu\n",exp, tot_exp)
}
exit(EXIT_SUCCESS)
}
root@node1:/home/c_test/unix_test# ./timerfd 20 3 4
printTime: current time:1396594376.746760 timer started
printTime: current time:1396594396.747705 read: 1total=1
printTime: current time:1396594399.747667 read: 1total=2
printTime: current time:1396594402.747728 read: 1total=3
printTime: current time:1396594405.746874 read: 1total=4
第一个参数为第一次定时器到期间隔,第二个参数为定时器的间隔,第三个参数为定时器多少次则退出。
timerfd简单的性能测试:
申请1000个定时器,超时间定位1s,每秒超时一次,发现cpu占用率在3.0G的cpu上大概为1%,10000个定时器的话再7%左右,而且不会出
现同时超时两个的情况,如果有printf到前台,则一般会出现定时器超时多次(3-5)才回调。
PS:linux内核新添加的API timerfd、signalfd、eventfd都有异曲同工之妙,都可以将本来复杂的处理转化思维变得简单。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)