zoukankan      html  css  js  c++  java
  • 磁盘满的情况下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;

    }

  • 相关阅读:
    [网站设计]网站设计的流程
    教你几招如何看透一个人
    难得迷茫
    java 日期 加减 运算
    第01章 SQL Server数据库基础 读后感
    [网站设计]如何设计一个成功的网站
    [网站设计] 素材网罗
    转载个人毕业5年职业感想
    SWTDesigner
    [存档]asp.net夜话之十一:web.config详解收藏
  • 原文地址:https://www.cnblogs.com/dongzhiquan/p/4752607.html
Copyright © 2011-2022 走看看