sd卡umount时busy解决方法

一、背景

代码中需要格式化存储卡,因此需要先umount,但是往往会因为操作sd卡的一些描述符没有关闭导致不能umount成功。

二、解决方法

解决方法一:

直接kill那个进程

fuser -m /mountpath
kill -9 pid  #上一条命令显示的进程号

解决方法二:

通过消息队列传送还未关闭的文件描述符

fuser -m 挂载的绝对路径 #查看有哪些进程正在操作这个设备
ls -l /proc/进程id/fd   #查看哪些文件描述符打开的,同时看打开的哪些文件

注:超级用户、lsattr,chattr等都无法操作"/proc/进程/fd/描述符",所以目前就通过消息队列传信息给操作的线程来关闭它的套接字,直接使用close就可以,无需啥open,转化等操作。还在研究有没有其他方法,可以在外部(非操作的进程)只关闭描述符的方法,欢迎大佬们留言分享(《UNIX环境高级编程》第17章 高级进程间通信  有提到,2021/1/3改)

以下是做个测试,

#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/msg.h>
using namespace std;
struct MYSTR{
    long type;
    char buff[1024]={0};
};

int main()
{
    FILE* fp;
    const int pid = getpid();
    for(int k=0;k<10;k++)
        fp = fopen(string(string("/media/hi/8028-E678/")+to_string(k)).c_str(),"w+");
    if(fp != NULL){
        printf("%d\n",fileno(fp));
        system("ls /proc");
        cout <<"pid: "<<pid<<endl;
        cout << "----------------------------------------------------------"<<endl;
        system(string(string("ls /proc/"+to_string(pid)+string("/fd"))).c_str());
    }

    int msgid = msgget((key_t)1234,0666 | IPC_CREAT);
    if(msgid ==-1){
        cerr<< "msgid "<<endl;
        exit(0);
    }
    while(1){
        MYSTR data;
        if(msgrcv(msgid,(void*)&data,sizeof(MYSTR),0,0) == -1){
            cerr << "msgrcv"<<endl;
        }
        cout<<"mq data: "<<data.type<<endl;
        close(data.type);
        cout << "close: "<< data.type <<endl;
        system(string(string("ls /proc/"+to_string(pid)+string("/fd"))).c_str());
        cout <<endl;
    }
    return 0;
}
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <sys/msg.h>
using namespace std;

struct MYSTR{
    long type;
    char buff[1024]{0};
};
int main()
{
    int otherpid;//输入进程id
    cin >> otherpid;

    int msgid = msgget((key_t)1234,0666 | IPC_CREAT);
    if(msgid == -1){
        cerr << "msgid"<<endl;
        exit(0);
    }
    for(int k=3;k<13;k++){
        MYSTR data;
        data.type = k;
        if(msgsnd(msgid,(void*)&data,sizeof(MYSTR),0) == -1){
            cerr <<"msgsnd"<<endl;
        }
        else{
            cout <<"snd success "<< k <<endl;
        }
    }
    return 0;
}

运行结果:

方法二umount设备

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值