磁盘满的情况下write调用会阻塞业务线程--实测

通过下面发现磁盘满的情况下write调用阻塞业务线程执行
testwrite.cpp (1.37KB)发送成功


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <map>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
using namespace std;

int handle;

pthread_mutex_t mutex_x= PTHREAD_MUTEX_INITIALIZER;

void *Write(void *arg)
{
   
    /*
    Createafilenamed"TEST.$$$"inthecurrentdirectoryandwrite
    astringtoit.If"TEST.$$$"alreadyexists,itwillbeoverwritten.
    */
    char string[40];
    int length,res;
    strcpy(string,"Hello,world! ");
    length=strlen(string);
   
    int i = 0;
    while(true)
    {
        pthread_mutex_lock(&mutex_x);
        bool bRet = (write(handle,string,length)!=length);
        pthread_mutex_unlock(&mutex_x);
       
        usleep(10000);
       
        if(!(i % 100))
        {
            printf("Errorwritingtothefile.tid:%u count:%d time:%d ", pthread_self(), i / 100,time(0));
        }
        i++;
    }
}

int main()

{
    if( (handle = open("TEST.txt",O_WRONLY|O_CREAT|O_TRUNC, S_IREAD|S_IWRITE) ) == -1)
    {
        printf("Erroropeningfile. ");
        exit(1);
    }
   
    int err;
    pthread_t ntid;
    for(int i = 0; i < 32; ++i)
    {
        err = pthread_create(&ntid, NULL, Write, NULL);
        if (err != 0)
            printf("can't create thread: %s ", strerror(err));
    }
   
    pthread_join(ntid,NULL);
    close(handle);
 return 0;

}

原文地址:https://www.cnblogs.com/dongzhiquan/p/4752607.html