C语言 关闭文件描述符后启动 SCSI 驱动器



我有一个程序,其中需要对未以任何其他方式安装或使用的驱动器进行降速。

我注意到在我关闭文件描述符后,驱动器会自动旋转。

我没有找到任何信息,为什么会这样,有什么办法可以禁用它吗?

这是一个自己测试的简短程序。任何帮助或指示将不胜感激

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <scsi/sg.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
    sg_io_hdr_t io_hdr;
    const unsigned char     stopcmdblk[6]        =     { 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00 };
    int fd = open(argv[1], O_RDWR);
    if (fd == -1) {
        perror("couldn't open device");
        exit(1);
    }
    memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
    io_hdr.interface_id = 'S';
    io_hdr.cmd_len = 6;
    io_hdr.mx_sb_len = 32;
    io_hdr.dxfer_direction = SG_DXFER_NONE;
    io_hdr.dxfer_len = 0;
    io_hdr.dxferp = NULL;
    io_hdr.cmdp = malloc(6);
    io_hdr.sbp = calloc(32, 1);
    memcpy(io_hdr.cmdp, stopcmdblk, 6);
    errno = 0;
    int ret = ioctl(fd, SG_IO, &io_hdr);
    if (ret < 0) { 
        perror("ioctl error");
        exit(1);
    }
    if ((io_hdr.info & SG_INFO_OK_MASK) != SG_INFO_OK) {
        printf("SCSI errn");
        exit(1);
    }
    printf("finished spindownn");
    sleep(30);
    printf("close file nown");
    close(fd);
    printf("file closedn");
    exit(0);
}

我猜系统想在文件关闭时将一些元数据写入磁盘。例如,文件大小 - 在每次write调用后更新文件大小似乎毫无意义。因此,它在close电话中更新。

我认为您需要sync系统调用。还有一个fsync变体仅适用于一个文件;但是,这与文件描述符无关;您要删除驱动器,因此应处理所有文件描述符。

最新更新