Garrick_W
Garrick_W

Reputation: 23

Why does using write() to write date to a disk block the SIGALRM signal until the write finishes?

#include <stdio.h>
#include <signal.h>
#include <fcntl.h>
#include <stdlib.h>

#define BUFSIZE 500*1024*1024
char buf[BUFSIZE];

static void sig_alrm(int signo) {
    printf("%d\n",time(NULL));
    printf("caught SIGALRM\n");
    fprintf(stderr, "in sig_alrm\n");
}


int main()
{
    int fd;
    fd=open("a.txt",O_WRONLY);
    signal(SIGALRM,sig_alrm);
    printf("%d\n",time(NULL));
    alarm(1);
    write(fd,buf,BUFSIZE);
}

output:

1414899972
1414899976
caught SIGALRM
in sig_alrm

It seems that the SIGALRM was caught about 3 seconds after the call to alarm(1). Why isn't the signal caught right away and the system call restarted?

Upvotes: 2

Views: 89

Answers (1)

Barmar
Barmar

Reputation: 781779

Some system calls don't allow interruptions. Writing to a local disk file is one of these. Writing to a file on an NFS server depends on whether it's mounted with the intr option (and I think this may only allow certain types of interrupts, such as Ctl-C).

Upvotes: 1

Related Questions