y_nnn: debug pthread leak valgrind by adding pthread_t attibute in arg_bash to follow thread created

This commit is contained in:
2025-11-19 22:50:33 +01:00
parent ba0a95a5b1
commit 5aa5b2e395
5 changed files with 76 additions and 73 deletions
@@ -53,6 +53,8 @@ struct arg_bash{
int fd_current_bash_pid; int fd_current_bash_pid;
pthread_t *thread_launch; pthread_t *thread_launch;
pthread_t *thread_run_newbash;
pthread_t *thread_run_waitbash;
pthread_mutex_t *mut_bash_var; pthread_mutex_t *mut_bash_var;
pthread_cond_t *cond_bash_var; pthread_cond_t *cond_bash_var;
@@ -43,14 +43,17 @@ void y_nnn_manager_handle_input(char * buf, int len_buf, void *arg){
kill_all_bash(bash_arg); kill_all_bash(bash_arg);
}else if(strncmp(buf,"startprintnewbash",17)==0){ }else if(strncmp(buf,"startprintnewbash",17)==0){
run_newbash(bash_arg); run_newbash(bash_arg);
pthread_t thread_run; bash_arg->thread_run_newbash=malloc(sizeof(pthread_t));
pthread_create(&thread_run, NULL, runBashPrint, arg); pthread_create(bash_arg->thread_run_newbash, NULL, runBashPrint, arg);
//pthread_t thread_run;
//pthread_create(&thread_run, NULL, runBashPrint, arg);
//Sleep(2); //Sleep(2);
}else if(strncmp(buf,"startprintwaitbash",18)==0){ }else if(strncmp(buf,"startprintwaitbash",18)==0){
bash_arg->thread_run_waitbash=malloc(sizeof(pthread_t));
pthread_t thread_run; pthread_create(bash_arg->thread_run_waitbash, NULL, run_sleep_wait_bash_and_print, arg);
pthread_create(&thread_run, NULL, run_sleep_wait_bash_and_print, arg); //pthread_t thread_run;
//pthread_create(&thread_run, NULL, run_sleep_wait_bash_and_print, arg);
}else if(strncmp(buf,"stopprintbash",13)==0){ }else if(strncmp(buf,"stopprintbash",13)==0){
pthread_mutex_lock(bash_arg->mut_bash_var); pthread_mutex_lock(bash_arg->mut_bash_var);
@@ -106,11 +109,12 @@ void* runBashPrint(void *arg){
pthread_mutex_lock(&(car->mut_coord)); pthread_mutex_lock(&(car->mut_coord));
bash_print_vehicle_n_path(car, pprint->scale_x, pprint->scale_y,bash_arg); bash_print_vehicle_n_path(car, pprint->scale_x, pprint->scale_y,bash_arg);
pthread_mutex_unlock(&(car->mut_coord)); pthread_mutex_unlock(&(car->mut_coord));
//pthread_mutex_unlock(&(pprint->mut_printed)); //pthread_mutex_unlock(&(pprint->mut_printed));
////printf("%s ",pprint->string_space); ////printf("%s ",pprint->string_space);
len_buf=sprintf(buf,"%s ",pprint->string_space); len_buf=sprintf(buf,"%s ",pprint->string_space);
BASH_WRITE_IF_EXIST(bash_arg, buf, len_buf) BASH_WRITE_IF_EXIST(bash_arg, buf, len_buf)
#if 0
////printf("ep: %ld ",qlStatus->index_episode); ////printf("ep: %ld ",qlStatus->index_episode);
len_buf=sprintf(buf,"ep: %ld\n",qlStatus->index_episode); len_buf=sprintf(buf,"ep: %ld\n",qlStatus->index_episode);
BASH_WRITE_IF_EXIST(bash_arg, buf, len_buf) BASH_WRITE_IF_EXIST(bash_arg, buf, len_buf)
@@ -142,13 +146,15 @@ void* runBashPrint(void *arg){
////printf("[%ld] %s ", rlAgent->car->status->cumulative_reward, pprint->string_space); ////printf("[%ld] %s ", rlAgent->car->status->cumulative_reward, pprint->string_space);
len_buf=sprintf(buf,"[%ld] %s ", rlAgent->car->status->cumulative_reward, pprint->string_space); len_buf=sprintf(buf,"[%ld] %s ", rlAgent->car->status->cumulative_reward, pprint->string_space);
BASH_WRITE_IF_EXIST(bash_arg, buf, len_buf) BASH_WRITE_IF_EXIST(bash_arg, buf, len_buf)
#endif
} }
Sleep(pprint->delay->delay_between_games); Sleep(pprint->delay->delay_between_games);
++count_print; ++count_print;
if(count_print > 20){ if(count_print > 20){
count_print = 0; count_print = 0;
clear_screen(); ////clear_screen();
len_buf=sprintf(buf,"\e[2J\e[H");
BASH_WRITE_IF_EXIST(bash_arg, buf, len_buf)
} }
} }
printf("debug: end runBashPrint\n"); printf("debug: end runBashPrint\n");
@@ -111,6 +111,9 @@ struct arg_bash *create_arg_bash(){
b_arg->go_on=1; b_arg->go_on=1;
b_arg->thread_launch=NULL; b_arg->thread_launch=NULL;
b_arg->thread_run_newbash=NULL;
b_arg->thread_run_waitbash=NULL;
return b_arg; return b_arg;
} }
@@ -136,7 +139,21 @@ void free_arg_bash(struct arg_bash *arg){
free(arg->thread_launch); free(arg->thread_launch);
} }
if(arg->thread_launch){
pthread_join(*(arg->thread_launch),NULL);
free(arg->thread_launch);
}
if(arg->thread_run_newbash){
pthread_join(*(arg->thread_run_newbash),NULL);
free(arg->thread_run_newbash);
}
if(arg->thread_run_waitbash){
pthread_join(*(arg->thread_run_waitbash),NULL);
free(arg->thread_run_waitbash);
}
free(arg); free(arg);
} }
int new_bash_exist(struct arg_bash *bash_arg){ int new_bash_exist(struct arg_bash *bash_arg){
@@ -151,27 +168,31 @@ void* run_newbash(void* argg){
if(arg->new_bash_pid == arg->old_bash_pid){ if(arg->new_bash_pid == arg->old_bash_pid){
pid_t pid=fork(); pid_t pid=fork();
if(pid<0){ if(pid<0){
perror("fork"); ////perror("fork run_newbash");
fprintf(stderr, "fork run_newbash error\n");
} }
else if(pid==0){/* child */ else if(pid==0){/* child */
usleep(200000);
char *cmdbash[]={"/usr/bin/gnome-terminal",NULL}; char *cmdbash[]={"/usr/bin/gnome-terminal",NULL};
execvp(cmdbash[0], cmdbash); execvp(cmdbash[0], cmdbash);
//printf("debug: new terminal created\n");
arg->new_bash_pid = pidof("bash",NULL);
}else{ }else{
wait(NULL); wait(NULL);
usleep(20000); //usleep(200000);
pthread_mutex_lock(arg->mut_bash_var); pthread_mutex_lock(arg->mut_bash_var);
arg->new_bash_pid = pidof("bash",NULL);
arg->fd_new_bash_pid = open_duplicate_bash(arg->new_bash_pid);
while(arg->new_bash_pid == arg->old_bash_pid) while(arg->new_bash_pid == arg->old_bash_pid)
{ {
printf("debug: create new terminal: %d vs %d\n",arg->new_bash_pid,arg->old_bash_pid);
getchar();
arg->new_bash_pid = pidof("bash",NULL); arg->new_bash_pid = pidof("bash",NULL);
arg->fd_new_bash_pid = open_duplicate_bash(arg->new_bash_pid); printf("debug: create new terminal: %d vs %d: forkpid=%d\n",arg->new_bash_pid,arg->old_bash_pid,pid);
//usleep(2000); //getchar();
//arg->new_bash_pid = pidof("bash",NULL);
//arg->fd_new_bash_pid = open_duplicate_bash(arg->new_bash_pid);
usleep(2000000);
} }
arg->fd_new_bash_pid = open_duplicate_bash(arg->new_bash_pid);
pthread_mutex_unlock(arg->mut_bash_var); pthread_mutex_unlock(arg->mut_bash_var);
pthread_cond_signal(arg->cond_bash_var); pthread_cond_signal(arg->cond_bash_var);
@@ -198,6 +219,22 @@ void kill_all_bash(struct arg_bash *arg){
arg->fd_current_bash_pid=-1; arg->fd_current_bash_pid=-1;
} }
pthread_mutex_unlock(arg->mut_bash_var); pthread_mutex_unlock(arg->mut_bash_var);
if(arg->thread_run_newbash){
pthread_join(*(arg->thread_run_newbash),NULL);
free(arg->thread_run_newbash);
arg->thread_run_newbash=NULL;
}
if(arg->thread_run_waitbash){
pthread_join(*(arg->thread_run_waitbash),NULL);
free(arg->thread_run_waitbash);
arg->thread_run_waitbash=NULL;
}
if(arg->thread_launch){
pthread_join(*(arg->thread_launch),NULL);
free(arg->thread_launch);
arg->thread_launch=NULL;
}
} }
int check_go_on_bash(struct arg_bash *arg){ int check_go_on_bash(struct arg_bash *arg){
@@ -376,7 +413,10 @@ void bash_print_vehicle_n_path(struct vehicle *v, float scale_x, float scale_y,
int i=0; int i=0;
for(i=0; i<w.ws_col+1; ++i) pad[i]=' '; for(i=0; i<w.ws_col+1; ++i) pad[i]=' ';
pad[i]='\0'; pad[i]='\0';
for(i=0; i<w.ws_row / 2 ; ++i) printf("%s\n",pad);; for(int j=0; j<w.ws_row / 2 ; ++j) {
////printf("%s\n",pad);
BASH_WRITE_IF_EXIST(bash_arg, pad, i);
}
} }
//goto_xy(0,w.ws_row - lines); //goto_xy(0,w.ws_row - lines);
+8 -53
View File
@@ -302,57 +302,6 @@ TEST(first_learn_vehicle_50__9){
/*
copy_coordinate(path->lower_bound_block[4], (float[]){0,0});
copy_coordinate(path->upper_bound_block[4], (float[]){150,250});
copy_coordinate(path->lower_bound_block[3], (float[]){150,40});
copy_coordinate(path->upper_bound_block[3], (float[]){250,150});
copy_coordinate(path->lower_bound_block[2], (float[]){250,80});
copy_coordinate(path->upper_bound_block[2], (float[]){360,200});
copy_coordinate(path->lower_bound_block[1], (float[]){360,70});
copy_coordinate(path->upper_bound_block[1], (float[]){600,150});
copy_coordinate(path->lower_bound_block[0], (float[]){600,90});
copy_coordinate(path->upper_bound_block[0], (float[]){760,300});
copy_coordinate(path->lower_bound_block[6], (float[]){260,300});
copy_coordinate(path->upper_bound_block[6], (float[]){760,360});
copy_coordinate(path->lower_bound_block[5], (float[]){0,250});
copy_coordinate(path->upper_bound_block[5], (float[]){410,300});
copy_coordinate(path->lower_bound_block[0], (float[]){0,0});
copy_coordinate(path->upper_bound_block[0], (float[]){100,250});
copy_coordinate(path->lower_bound_block[1], (float[]){100,0});
copy_coordinate(path->upper_bound_block[1], (float[]){250,80});
copy_coordinate(path->lower_bound_block[2], (float[]){250,0});
copy_coordinate(path->upper_bound_block[2], (float[]){360,140});
copy_coordinate(path->lower_bound_block[3], (float[]){360,70});
copy_coordinate(path->upper_bound_block[3], (float[]){600,140});
copy_coordinate(path->lower_bound_block[4], (float[]){600,90});
copy_coordinate(path->upper_bound_block[4], (float[]){720,300});
copy_coordinate(path->lower_bound_block[5], (float[]){300,300});
copy_coordinate(path->upper_bound_block[5], (float[]){720,350});
copy_coordinate(path->lower_bound_block[6], (float[]){0,250});
copy_coordinate(path->upper_bound_block[6], (float[]){410,300});
copy_coordinate(path->lower_bound_block[0], (float[]){0,300});
copy_coordinate(path->upper_bound_block[0], (float[]){400,700});
copy_coordinate(path->lower_bound_block[1], (float[]){100,0});
copy_coordinate(path->upper_bound_block[1], (float[]){1000,300});
copy_coordinate(path->lower_bound_block[2], (float[]){1000,50});
copy_coordinate(path->upper_bound_block[2], (float[]){1400,500});
copy_coordinate(path->lower_bound_block[3], (float[]){1400,200});
copy_coordinate(path->upper_bound_block[3], (float[]){1800,700});
copy_coordinate(path->lower_bound_block[4], (float[]){1100,700});
copy_coordinate(path->upper_bound_block[4], (float[]){1700,1000});
copy_coordinate(path->lower_bound_block[5], (float[]){800,600});
copy_coordinate(path->upper_bound_block[5], (float[]){1100,975});
copy_coordinate(path->lower_bound_block[6], (float[]){100,700});
copy_coordinate(path->upper_bound_block[6], (float[]){800,975});
*/
#else #else
@@ -415,7 +364,8 @@ struct status_qlearning *qlstatus = create_status_qlearning ();
20/*long int nb_training_before_update_weight_in_target*/, 20/*long int nb_training_before_update_weight_in_target*/,
10000/*size_t number_episodes*/ 10000/*size_t number_episodes*/
); );
/* UPDATE_ATTRIBUTE_NEURONE_IN_ALL_LAYERS(TYPE_FLOAT, nnetworks->main_net, d_f_act , df ); /*
UPDATE_ATTRIBUTE_NEURONE_IN_ALL_LAYERS(TYPE_FLOAT, nnetworks->main_net, d_f_act , df );
UPDATE_ATTRIBUTE_NEURONE_IN_ALL_LAYERS(TYPE_FLOAT, nnetworks->main_net, f_act, f ); UPDATE_ATTRIBUTE_NEURONE_IN_ALL_LAYERS(TYPE_FLOAT, nnetworks->main_net, f_act, f );
UPDATE_ATTRIBUTE_NEURONE_IN_ALL_LAYERS(TYPE_FLOAT, nnetworks->target_net, d_f_act , df ); UPDATE_ATTRIBUTE_NEURONE_IN_ALL_LAYERS(TYPE_FLOAT, nnetworks->target_net, d_f_act , df );
UPDATE_ATTRIBUTE_NEURONE_IN_ALL_LAYERS(TYPE_FLOAT, nnetworks->target_net, f_act , f ); UPDATE_ATTRIBUTE_NEURONE_IN_ALL_LAYERS(TYPE_FLOAT, nnetworks->target_net, f_act , f );
@@ -433,6 +383,10 @@ struct status_qlearning *qlstatus = create_status_qlearning ();
qlparams/*struct qlearning_params *qlearnParams*/ qlparams/*struct qlearning_params *qlearnParams*/
); );
pthread_t thread_learn;
pthread_create(&thread_learn, NULL, learn_to_drive, (void*)rlAgent);
//learn_to_drive(rlAgent);
struct arg_bash *bash_arg= create_arg_bash(); struct arg_bash *bash_arg= create_arg_bash();
struct arg_run_qlearn_bprint *argQL_BP = create_arg_run_qlearn_bprint(bash_arg, rlAgent); struct arg_run_qlearn_bprint *argQL_BP = create_arg_run_qlearn_bprint(bash_arg, rlAgent);
@@ -440,17 +394,18 @@ struct status_qlearning *qlstatus = create_status_qlearning ();
struct arg_var_ * var = create_arg_var_(y_nnn_manager_handle_input, argQL_BP); struct arg_var_ * var = create_arg_var_(y_nnn_manager_handle_input, argQL_BP);
struct y_socket_t *argS = y_socket_create("1600", 2, 3, var); struct y_socket_t *argS = y_socket_create("1600", 2, 3, var);
pthread_t pollTh; pthread_t pollTh;
pthread_create(&pollTh, NULL, y_socket_poll_fds, (void*)argS); pthread_create(&pollTh, NULL, y_socket_poll_fds, (void*)argS);
learn_to_drive(rlAgent);
pthread_join(pollTh, NULL); pthread_join(pollTh, NULL);
pthread_join(thread_learn, NULL);
y_socket_free(argS); y_socket_free(argS);
free_arg_var_(var); free_arg_var_(var);
Binary file not shown.