Linux stat调用超时

raz*_*zeh 7 linux

有没有办法让Linux统计系统调用超时?

我正在使用分布式文件系统,理论上我的所有文件系统调用都应该及时回答,实际上它们不是.经过一段固定的时间后,我宁愿有超时和错误代码而不是继续挂起.

我已经尝试在另一个线程中生成请求,但是这与gdb有一些不良的交互,并且是表达我真正想要的一种非常迂回的方式:超时.

And*_*nle 4

假设您使用的是 C,并且可以安全地设置SIGALARM处理程序,则可以使用与此类似的代码,只是使用不同的库调用: Can statvfs block on certain network devices? 那这个案子要怎么处理呢?

几乎剪切并粘贴代码并更改statvfsstat

#include <sigaction.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>

// alarm handler doesn't need to do anything
// other than simply exist
static void alarm_handler( int sig )
{
    return;
}

 .
 .
 .

// stat() with a timeout measured in seconds
// will return -1 with errno set to EINTR should
// it time out
int statvfs_try( const char *path, struct stat *s, unsigned int seconds )
{
    struct sigaction newact;
    struct sigaction oldact;

    // make sure they're entirely clear (yes I'm paranoid...)
    memset( &newact, 0, sizeof( newact ) );
    memset( &oldact, 0, sizeof( oldact) );

    sigemptyset( &newact.sa_mask );

    // note that does not have SA_RESTART set, so
    // stat() should be interrupted on a signal
    // (hopefully your libc doesn't restart it...)
    newact.sa_flags = 0;
    newact.sa_handler = alarm_handler;
    sigaction( SIGALRM, &newact, &oldact );

    alarm( seconds );

    // clear errno
    errno = 0;
    int rc = stat( path, s );

    // save the errno value as alarm() and sigaction() might change it
    int save_errno = errno;

    // clear any alarm and reset the signal handler
    alarm( 0 );
    sigaction( SIGALRM, &oldact, NULL );

    errno = saved_errno;
    return( rc );
}
Run Code Online (Sandbox Code Playgroud)