磁盘满的情况下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!\n");
    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\n", 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.\n");
        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\n", strerror(err));
    }
   
    pthread_join(ntid,NULL);
    close(handle);
 return 0;

}

posted @ 2015-08-23 15:41  dzqabc  阅读(125)  评论(0编辑  收藏  举报