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;

    }

  • 相关阅读:
    Expression Blend实例中文教程(13)
    Expression Blend实例中文教程(12)
    Expression Blend实例中文教程(11)
    【转】Python3中urllib详细使用方法(header,代理,超时,认证,异常处理)
    cookie
    【转】cookielib模块
    代理的步骤
    urllib.parse.urlencode
    urllib.request.Request
    【转】python3 urllib.request 网络请求操作
  • 原文地址:https://www.cnblogs.com/dongzhiquan/p/4752607.html
Copyright © 2011-2022 走看看