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;

    }

  • 相关阅读:
    能够免费做商业站点的CMS讨论
    ntoskrnl.exe损坏或丢失的解决方式
    QT 仓库管理系统 开放源代码
    Disposable microfluidic devices: fabrication, function, and application Gina S. Fiorini and Daniel T
    DllImport中的EntryPoint
    IOS Table中Cell的重用reuse机制分析
    双slave的server_uuid同样问题
    怎样使用SetTimer MFC 够具体
    2013 成都邀请赛
    设计模式六大原则(2):里氏替换原则
  • 原文地址:https://www.cnblogs.com/dongzhiquan/p/4752607.html
Copyright © 2011-2022 走看看