小编Ten*_*hao的帖子

与timerfd的epoll

我想使用it_intervalof newValue来设置超时的间隔.
但在我的例子中,我只能打印timeout 一次.
发生了什么?我该如何设置间隔?

这是我的代码:

int main()
{
int efd =epoll_create(256);             
setnonblock(efd);
struct epoll_event ev,events[256];

int tfd;//timer fd

if((tfd= timerfd_create(CLOCK_MONOTONIC,TFD_NONBLOCK)) < 0)
  cout<<"timerfd create error"<<endl;

struct itimerspec newValue;
struct itimerspec oldValue;
bzero(&newValue,sizeof(newValue));  
bzero(&oldValue,sizeof(oldValue));
struct timespec ts;
ts.tv_sec = 5;
ts.tv_nsec = 0;

    //both interval and value have been set
    newValue.it_value = ts; 
    newValue.it_interval = ts;
    if( timerfd_settime(tfd,0,&newValue,&oldValue) <0)
    {
        cout<<"settime error"<<strerror(errno)<<endl;
    }   

    ev.data.fd = tfd;
    ev.events = EPOLLIN | EPOLLET;

    if( epoll_ctl(efd,EPOLL_CTL_ADD,tfd,&ev) < 0) …
Run Code Online (Sandbox Code Playgroud)

linux epoll timer

5
推荐指数
1
解决办法
9086
查看次数

sendfile不会复制文件内容

我创建文件1.txt 2.txt并写入一些内容1.txt.
然后我使用下面的代码,并希望将内容复制到2.txt.
但它不起作用.什么都没有2.txt.

你能解释我的错误吗?

int main()
{
    int fd1 = open("1.txt",O_RDWR);
    int fd2 = open("2.txt",O_RDWR);          
    struct stat stat_buf ;
    fstat(fd1,&stat_buf);
    ssize_t size = sendfile(fd1,fd2,0,stat_buf.st_size);
    cout<<"fd1 size:"<<stat_buf.st_size<<endl; //output 41
    cout<<strerror(errno)<<endl; //output success

    close(fd1);
    close(fd2);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ linux system-calls sendfile

5
推荐指数
1
解决办法
2197
查看次数

我怎么知道Linux系统调用是否是线程安全的?

linux中的某些功能由_r标记为“线程安全”(例如gmtime_r),但是大多数系统调用未标记,并且在联机帮助页中也未提及。所以我的问题是:我如何知道Linux syscall是否是线程安全的?谢谢!

c linux multithreading system-calls thread-safety

1
推荐指数
1
解决办法
2774
查看次数

标签 统计

linux ×3

system-calls ×2

c ×1

c++ ×1

epoll ×1

multithreading ×1

sendfile ×1

thread-safety ×1

timer ×1