From b3ddb9278645fc9df7025ca853809885ccc48f9d Mon Sep 17 00:00:00 2001 From: ubuntu201711081 <201711081@jbnu.ac.kr> Date: Tue, 8 Dec 2020 16:49:50 +0000 Subject: [PATCH] fix progress bar deadlock --- client.c | 3 +- display_bar.c | 33 +- display_bar.h | 2 + testdata/dlist.txt | 1000 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 1036 insertions(+), 2 deletions(-) create mode 100644 testdata/dlist.txt diff --git a/client.c b/client.c index 0c94da2..fd40e70 100644 --- a/client.c +++ b/client.c @@ -419,6 +419,7 @@ int main(int argc, const char *argv[]){ init_bench_data(); if(parse_arg(argc,argv) < 0) return EXIT_FAILURE; + ready_progress_bar(); err = getsockaddrbyname(AF_INET,SOCK_STREAM,0,cmd_args.server_name,(struct sockaddr *)&addr); if (err != 0){ @@ -480,6 +481,6 @@ int main(int argc, const char *argv[]){ fprintf(stdout,"operation: %lf us/op\n",avg / 1000.0); fprintf(stdout,"resolution: %ld sec %ld ns\n",bench.resolution.tv_sec,bench.resolution.tv_nsec); } - + quit_progress_bar(); return retval; } \ No newline at end of file diff --git a/display_bar.c b/display_bar.c index 1237c8d..36ba7ef 100644 --- a/display_bar.c +++ b/display_bar.c @@ -6,6 +6,9 @@ #include #include #include +#include +#include +#include #include "display_bar.h" #include "timerhelper.h" @@ -25,15 +28,34 @@ enum{ static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; static raw_progress_bar_t scrolled = 1; +static int tty_fd = STDIN_FILENO; +#ifdef DEBUG +static int deadlock = 0; +#endif void ready_progress_bar(){ fflush(stdout); + if(!isatty(STDIN_FILENO)){ + char * buf = ctermid(NULL); + tty_fd = open(buf,O_RDONLY); + } } void lock_scrolled(){ +#ifdef DEBUG + deadlock++; + if (deadlock > 1) + { + fprintf(stderr,"deadlock occur\n"); + exit(EXIT_FAILURE); + } +#endif pthread_mutex_lock(&mutex); } void unlock_scrolled(){ +#ifdef DEBUG + deadlock--; +#endif pthread_mutex_unlock(&mutex); } @@ -75,7 +97,10 @@ void RawDisplayProgressBar(raw_progress_bar_t bar,size_t offset,size_t total,con }//optimization. //if ioctl failed? what should i do... ioctl(STDIN_FILENO,TIOCGWINSZ,(char *)&wnd_size); - if (wnd_size.ws_row < pos) return; + if (wnd_size.ws_row < pos){ + unlock_scrolled(); + return; + } fprintf(stdout,"\x1b[%dA[%s]: %.2f%% %s bytes: %ld/%ld \x1b[%dB\r",pos,buf,cur_progress,filename,total,offset,pos); fflush(stdout); unlock_scrolled(); @@ -115,4 +140,10 @@ void DisplayProgressBar(progress_bar_t * bar,size_t offset,size_t total,const ch } bar->last_update = ts; RawDisplayProgressBar(bar->bar,offset,total,filename); +} + +void quit_progress_bar(){ + if(tty_fd != STDIN_FILENO){ + close(tty_fd); + } } \ No newline at end of file diff --git a/display_bar.h b/display_bar.h index 2909de9..7bdc170 100644 --- a/display_bar.h +++ b/display_bar.h @@ -13,6 +13,8 @@ void unlock_scrolled(); void ready_progress_bar(); +void quit_progress_bar(); + void add_scrolled_unlocked(unsigned int i); raw_progress_bar_t create_raw_progress_bar(); diff --git a/testdata/dlist.txt b/testdata/dlist.txt new file mode 100644 index 0000000..3be5636 --- /dev/null +++ b/testdata/dlist.txt @@ -0,0 +1,1000 @@ +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt +bootstrap.css +bootstrap.js +lorem.txt +react.js +test.txt \ No newline at end of file