y_nnn_manager: trying to handle screen and socket
This commit is contained in:
@@ -2,7 +2,12 @@
|
||||
#ifndef Y_NETWORK_NEURAL_NETWORK__MANAGER__H_C
|
||||
#define Y_NETWORK_NEURAL_NETWORK__MANAGER__H_C
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "y_net_neur_net/y_nnn_screen_manager.h"
|
||||
|
||||
#include "y_socket_t/y_socket_t.h"
|
||||
|
||||
void y_nnn_manager_handle_input(char * buf, int len_buf, void *arg);
|
||||
|
||||
#endif /* Y_NETWORK_NEURAL_NETWORK__MANAGER__H_C */
|
||||
|
||||
@@ -12,18 +12,61 @@
|
||||
|
||||
#include <ctype.h>
|
||||
#include <dirent.h>
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
#include "list_t/list_t.h"
|
||||
|
||||
GENERATE_LIST_ALL(pid_t)
|
||||
|
||||
#define SIZE_LOCAL_BUF 300
|
||||
|
||||
#define GOTO_TOP_LEFT "\x1B[0;0f"
|
||||
#define LEN_GOTO_TOP_LEFT 6
|
||||
|
||||
//#include "y_socket_t/y_list_var_tool.h"
|
||||
|
||||
pid_t max_pidof_to_list_pid_t(char *target, struct main_list_pid_t *m_pid_t);
|
||||
pid_t pidof(char *target, struct main_list_pid_t *m_pid_t);
|
||||
|
||||
int sprintbashpid(pid_t pid, char *content, size_t size_content);
|
||||
//int sprintbashpid(pid_t pid, char *content, size_t size_content);
|
||||
|
||||
int _xy_goto(char *str, int x, int y);
|
||||
|
||||
struct arg_bash{
|
||||
pid_t old_bash_pid;
|
||||
pid_t new_bash_pid;
|
||||
pid_t current_bash_pid;
|
||||
|
||||
int fd_old_bash_pid;
|
||||
int fd_new_bash_pid;
|
||||
int fd_current_bash_pid;
|
||||
|
||||
pthread_mutex_t *mut_bash_var;
|
||||
pthread_cond_t *cond_bash_var;
|
||||
int go_on;
|
||||
};
|
||||
|
||||
struct arg_bash *create_arg_bash();
|
||||
void free_arg_bash(struct arg_bash *arg);
|
||||
|
||||
//int open_duplicate_bash_most_recent();
|
||||
|
||||
//int open_duplicate_bash(pid_t pid);
|
||||
|
||||
//void close_duplicate_bash(int fd);
|
||||
|
||||
int write_duplicate_bash(int fd, char *content, size_t size_content);
|
||||
|
||||
|
||||
void* run_newbash(void *arg);
|
||||
void* wait_newbash(void *arg);
|
||||
void* wait_sleep_newbash(void* argg);
|
||||
void kill_all_bash(struct arg_bash *arg);
|
||||
int check_go_on_bash(struct arg_bash *arg);
|
||||
void set_go_on_bash(struct arg_bash *arg, int go_on);
|
||||
|
||||
/* pthread call of 3 previous functions */
|
||||
void* launch_new_bash(void *arg);
|
||||
void* launch_wait_bash(void *arg);
|
||||
void* launch_sleep_wait_bash(void *arg);
|
||||
|
||||
#endif /* Y_NETWORK_NEURAL_NETWORK__SCREEN_MANAGER__H_C */
|
||||
|
||||
@@ -1,3 +1,24 @@
|
||||
/* file: y_network_neural_network/src/y_net_neur_net/y_nnn_manager.c */
|
||||
|
||||
#include "y_net_neur_net/y_nnn_manager.h"
|
||||
|
||||
void y_nnn_manager_handle_input(char * buf, int len_buf, void *arg){
|
||||
//struct arg_bash *bash_arg=(struct arg_bash*)arg;
|
||||
|
||||
if(arg && (len_buf>0)){
|
||||
|
||||
printf("debug: %s #%d (%d)\n",buf,len_buf,((struct arg_bash*)arg)->old_bash_pid);
|
||||
if(strncmp(buf,"newbash",7)==0){
|
||||
run_newbash(arg);
|
||||
}else if(strncmp(buf,"waitbash",8)==0){
|
||||
launch_sleep_wait_bash(arg);
|
||||
}else if(strncmp(buf,"killbash",8)==0){
|
||||
kill_all_bash(arg);
|
||||
}else{
|
||||
printf("debug: %s is not handle\n",buf);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/* file: y_network_neural_network/src/y_net_neur_net/y_nnn_screenmanager.c */
|
||||
#include "y_net_neur_net/y_nnn_screen_manager.h"
|
||||
|
||||
#define SIZE_LOCAL_BUF 300
|
||||
//#define SIZE_LOCAL_BUF 300
|
||||
|
||||
GEN_LIST_ALL(pid_t)
|
||||
|
||||
|
||||
/* return max pidof target, and if m_pid_t is not NULL, put into it all pids */
|
||||
pid_t max_pidof_to_list_pid_t(char *target, struct main_list_pid_t *m_pid_t){
|
||||
pid_t pidof(char *target, struct main_list_pid_t *m_pid_t){
|
||||
pid_t maxpid=0;
|
||||
struct dirent *entry;
|
||||
DIR *proc = opendir("/proc"); /* */
|
||||
@@ -69,3 +69,274 @@ int sprintbashpid(pid_t pid, char *content, size_t size_content){
|
||||
}
|
||||
|
||||
}
|
||||
int open_duplicate_bash_most_recent(){
|
||||
pid_t pid=pidof("bash", NULL);
|
||||
char duplicate_stdout_path[128];
|
||||
sprintf(duplicate_stdout_path,"/proc/%d/fd/1",pid);
|
||||
return open(duplicate_stdout_path, O_WRONLY);
|
||||
}
|
||||
|
||||
int open_duplicate_bash(pid_t pid){
|
||||
char duplicate_stdout_path[128];
|
||||
sprintf(duplicate_stdout_path,"/proc/%d/fd/1",pid);
|
||||
return open(duplicate_stdout_path, O_WRONLY);
|
||||
}
|
||||
|
||||
void close_duplicate_bash(int fd){
|
||||
if(fd>0)
|
||||
close(fd);
|
||||
}
|
||||
|
||||
int write_duplicate_bash(int fd, char *content, size_t size_content){
|
||||
if(fd>0){
|
||||
return write(fd, content, size_content);
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
|
||||
struct arg_bash *create_arg_bash(){
|
||||
struct arg_bash *b_arg=malloc(sizeof(struct arg_bash));
|
||||
b_arg->old_bash_pid = pidof("bash", NULL);
|
||||
b_arg->new_bash_pid = b_arg->old_bash_pid;
|
||||
b_arg->current_bash_pid= b_arg->old_bash_pid;
|
||||
b_arg->fd_old_bash_pid = -1;
|
||||
b_arg->fd_new_bash_pid= -1;
|
||||
b_arg->fd_current_bash_pid= -1;
|
||||
|
||||
b_arg->mut_bash_var = malloc(sizeof(pthread_mutex_t));
|
||||
b_arg->cond_bash_var = malloc(sizeof(pthread_cond_t));
|
||||
pthread_mutex_init(b_arg->mut_bash_var, NULL);
|
||||
pthread_cond_init(b_arg->cond_bash_var,NULL);
|
||||
|
||||
b_arg->go_on=1;
|
||||
|
||||
return b_arg;
|
||||
}
|
||||
|
||||
/* free and kill bash */
|
||||
void free_arg_bash(struct arg_bash *arg){
|
||||
pthread_mutex_destroy(arg->mut_bash_var);
|
||||
free(arg->mut_bash_var);
|
||||
pthread_cond_destroy(arg->cond_bash_var);
|
||||
free(arg->cond_bash_var);
|
||||
|
||||
if(arg->fd_new_bash_pid > 0) {
|
||||
close(arg->fd_new_bash_pid);
|
||||
kill(arg->new_bash_pid, SIGKILL);
|
||||
}
|
||||
|
||||
if(arg->fd_current_bash_pid > 0 && arg->fd_current_bash_pid != arg->fd_new_bash_pid) {
|
||||
close(arg->fd_current_bash_pid);
|
||||
kill(arg->current_bash_pid, SIGKILL);
|
||||
}
|
||||
|
||||
free(arg);
|
||||
}
|
||||
|
||||
/* run new bash terminal graphic, can be called directly or in a thread */
|
||||
void* run_newbash(void* argg){
|
||||
struct arg_bash *arg=(struct arg_bash*)argg;
|
||||
//* fork exec better than system call!
|
||||
|
||||
if(arg->new_bash_pid == arg->old_bash_pid){
|
||||
pid_t pid=fork();
|
||||
if(pid<0){
|
||||
perror("fork");
|
||||
}
|
||||
else if(pid==0){/* child */
|
||||
char *cmdbash[]={"/usr/bin/gnome-terminal",NULL};
|
||||
execvp(cmdbash[0], cmdbash);
|
||||
}else{
|
||||
wait(NULL);
|
||||
|
||||
pthread_mutex_lock(arg->mut_bash_var);
|
||||
arg->new_bash_pid=pidof("bash",NULL);
|
||||
if(arg->new_bash_pid > arg->old_bash_pid){
|
||||
arg->fd_new_bash_pid = open_duplicate_bash(arg->new_bash_pid);
|
||||
printf("runnewbash: ready\n");
|
||||
}
|
||||
pthread_mutex_unlock(arg->mut_bash_var);
|
||||
pthread_cond_signal(arg->cond_bash_var);
|
||||
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void kill_all_bash(struct arg_bash *arg){
|
||||
pthread_mutex_lock(arg->mut_bash_var);
|
||||
if(arg->fd_new_bash_pid > 0) {
|
||||
close(arg->fd_new_bash_pid);
|
||||
kill(arg->new_bash_pid, SIGKILL);
|
||||
arg->new_bash_pid = arg->old_bash_pid;
|
||||
arg->fd_new_bash_pid = -1;
|
||||
}
|
||||
|
||||
if(arg->fd_current_bash_pid > 0 && arg->fd_current_bash_pid != arg->fd_new_bash_pid) {
|
||||
close(arg->fd_current_bash_pid);
|
||||
kill(arg->current_bash_pid, SIGKILL);
|
||||
arg->current_bash_pid = arg->old_bash_pid;
|
||||
arg->fd_current_bash_pid=-1;
|
||||
}
|
||||
pthread_mutex_unlock(arg->mut_bash_var);
|
||||
}
|
||||
|
||||
int check_go_on_bash(struct arg_bash *arg){
|
||||
int ret;
|
||||
pthread_mutex_lock(arg->mut_bash_var);
|
||||
ret = arg->go_on;
|
||||
pthread_mutex_unlock(arg->mut_bash_var);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
void set_go_on_bash(struct arg_bash *arg, int go_on){
|
||||
pthread_mutex_lock(arg->mut_bash_var);
|
||||
arg->go_on = go_on;
|
||||
pthread_mutex_unlock(arg->mut_bash_var);
|
||||
}
|
||||
|
||||
/* call run_newbash in a thread */
|
||||
void* launch_new_bash(void *b_arg){
|
||||
struct arg_bash *arg = (struct arg_bash*)b_arg; // create_arg_bash();
|
||||
|
||||
//run_newbash(arg);
|
||||
pthread_t new_bash_thread;
|
||||
|
||||
pthread_create(&new_bash_thread, NULL, run_newbash, (void*)arg);
|
||||
/*
|
||||
pthread_mutex_lock(arg->mut_bash_var);
|
||||
while(arg->fd_new_bash_pid == -1){
|
||||
printf("debug: wait new bash!\n");
|
||||
pthread_cond_wait(arg->cond_bash_var, arg->mut_bash_var);
|
||||
}
|
||||
pthread_mutex_unlock(arg->mut_bash_var);
|
||||
*/
|
||||
pthread_join(new_bash_thread, NULL);
|
||||
|
||||
//free_arg_bash(arg);
|
||||
return NULL;
|
||||
}
|
||||
/* to be call in thread or directly! */
|
||||
void* wait_sleep_newbash(void* argg){
|
||||
struct arg_bash *arg=(struct arg_bash*)argg;
|
||||
//* fork exec better than system call!
|
||||
|
||||
char *msg="Please, open new terminal or new ssh to this machine!, then touch enter in this terminal.";
|
||||
int len_msg=strlen(msg);
|
||||
char pad[len_msg+1];
|
||||
memset(pad,' ',len_msg);
|
||||
while(
|
||||
(arg->current_bash_pid == arg->old_bash_pid) ||
|
||||
(arg->current_bash_pid == arg->new_bash_pid)
|
||||
){
|
||||
write(1,"\r",1);
|
||||
write(1,pad,len_msg);
|
||||
usleep(400000);
|
||||
write(1,"\r",1);
|
||||
write(1,msg,len_msg);
|
||||
usleep(400000);
|
||||
arg->current_bash_pid = pidof("bash", NULL);
|
||||
}
|
||||
//char *msg_o="open fd, ";
|
||||
//int len_msg_o=strlen(msg_o);
|
||||
pthread_mutex_lock(arg->mut_bash_var);
|
||||
//if(arg->current_bash_pid != arg->new_bash_pid){
|
||||
while(arg->fd_current_bash_pid == -1){
|
||||
//write(1,msg_o,len_msg_o);
|
||||
arg->fd_current_bash_pid = open_duplicate_bash(arg->current_bash_pid);
|
||||
}
|
||||
//}
|
||||
pthread_mutex_unlock(arg->mut_bash_var);
|
||||
pthread_cond_signal(arg->cond_bash_var);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/* call wait_newbash in a thread */
|
||||
void* launch_sleep_wait_bash(void *b_arg){
|
||||
struct arg_bash *arg = (struct arg_bash*)b_arg; // create_arg_bash();
|
||||
//wait_newbash(arg);
|
||||
|
||||
pthread_t wait_bash_thread;
|
||||
|
||||
pthread_create(&wait_bash_thread, NULL, wait_sleep_newbash, (void*)arg);
|
||||
/*
|
||||
pthread_mutex_lock(arg->mut_bash_var);
|
||||
while(arg->fd_current_bash_pid == -1){
|
||||
printf("debug: wait new bash!\n");
|
||||
pthread_cond_wait(arg->cond_bash_var, arg->mut_bash_var);
|
||||
}
|
||||
pthread_mutex_unlock(arg->mut_bash_var);
|
||||
*/
|
||||
pthread_join(wait_bash_thread, NULL);
|
||||
|
||||
//free_arg_bash(arg);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/* to be call in thread or directly! */
|
||||
void* wait_newbash(void* argg){
|
||||
struct arg_bash *arg=(struct arg_bash*)argg;
|
||||
//* fork exec better than system call!
|
||||
|
||||
char *msg="Please, open new terminal or new ssh to this machine!, then touch enter in this terminal.\n";
|
||||
int len_msg=strlen(msg);
|
||||
while(arg->current_bash_pid == arg->old_bash_pid){
|
||||
write(1,msg,len_msg);
|
||||
getchar();
|
||||
arg->current_bash_pid = pidof("bash", NULL);
|
||||
}
|
||||
while(arg->current_bash_pid == arg->new_bash_pid){
|
||||
write(1,msg,len_msg);
|
||||
getchar();
|
||||
arg->current_bash_pid = pidof("bash", NULL);
|
||||
}
|
||||
|
||||
//char *msg_o="open fd, ";
|
||||
//int len_msg_o=strlen(msg_o);
|
||||
pthread_mutex_lock(arg->mut_bash_var);
|
||||
while(arg->fd_current_bash_pid == -1){
|
||||
//write(1,msg_o,len_msg_o);
|
||||
arg->fd_current_bash_pid = open_duplicate_bash(arg->current_bash_pid);
|
||||
}
|
||||
|
||||
|
||||
pthread_mutex_unlock(arg->mut_bash_var);
|
||||
pthread_cond_signal(arg->cond_bash_var);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
int _xy_goto(char *str, int x, int y){
|
||||
return sprintf(str,"%c[%d;%df", 0x1B, y, x);
|
||||
}
|
||||
|
||||
/* call wait_newbash in a thread */
|
||||
void* launch_wait_bash(void *b_arg){
|
||||
struct arg_bash *arg = (struct arg_bash*)b_arg; // create_arg_bash();
|
||||
//wait_newbash(arg);
|
||||
|
||||
pthread_t wait_bash_thread;
|
||||
|
||||
pthread_create(&wait_bash_thread, NULL, wait_newbash, (void*)arg);
|
||||
/*
|
||||
pthread_mutex_lock(arg->mut_bash_var);
|
||||
while(arg->fd_current_bash_pid == -1){
|
||||
printf("debug: wait new bash!\n");
|
||||
pthread_cond_wait(arg->cond_bash_var, arg->mut_bash_var);
|
||||
}
|
||||
pthread_mutex_unlock(arg->mut_bash_var);
|
||||
*/
|
||||
pthread_join(wait_bash_thread, NULL);
|
||||
|
||||
//free_arg_bash(arg);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -16,10 +16,229 @@
|
||||
#include "y_net_neur_net/y_nnn_manager.h"
|
||||
#include "y_net_neur_net/y_nnn_screen_manager.h"
|
||||
|
||||
|
||||
TEST(pidof_bash){
|
||||
|
||||
struct arg_bash *arg= create_arg_bash();
|
||||
launch_new_bash((void*)arg);
|
||||
|
||||
char buf[256];
|
||||
int len_buf = sprintf(buf," main Hello, voici le terminal avec new_bash_pid=%d et getpid=%d, old_bash_pid=%d\n\n",arg->new_bash_pid, getpid(), arg->new_bash_pid);
|
||||
|
||||
char str[80];
|
||||
int len_str=_xy_goto(str,0,0);
|
||||
char index_str[8];
|
||||
int len_index_str;
|
||||
for(int i=0;i<200;++i){
|
||||
write(1,":",1);
|
||||
write(arg->fd_new_bash_pid, str, len_str);
|
||||
len_index_str=sprintf(index_str, "%d", i);
|
||||
write(arg->fd_new_bash_pid, index_str, len_index_str);
|
||||
write(arg->fd_new_bash_pid, buf, len_buf);
|
||||
usleep(40000);
|
||||
}
|
||||
write(1,"\n",1);
|
||||
|
||||
free_arg_bash(arg);
|
||||
|
||||
}
|
||||
|
||||
TEST(wait_bash){
|
||||
struct arg_bash *arg= create_arg_bash();
|
||||
launch_wait_bash((void*)arg);
|
||||
|
||||
char buf[256];
|
||||
int len_buf = sprintf(buf," main Hello, voici le terminal avec new_bash_pid=%d et getpid=%d, old_bash_pid=%d\n\n",arg->new_bash_pid, getpid(), arg->new_bash_pid);
|
||||
|
||||
char str[80];
|
||||
int len_str=_xy_goto(str,0,0);
|
||||
char index_str[8];
|
||||
int len_index_str;
|
||||
for(int i=0;i<200;++i){
|
||||
write(1,":",1);
|
||||
write(arg->fd_current_bash_pid, str, len_str);
|
||||
len_index_str=sprintf(index_str, "%d", i);
|
||||
write(arg->fd_current_bash_pid, index_str, len_index_str);
|
||||
write(arg->fd_current_bash_pid, buf, len_buf);
|
||||
usleep(40000);
|
||||
}
|
||||
write(1,"\n",1);
|
||||
|
||||
free_arg_bash(arg);
|
||||
|
||||
}
|
||||
|
||||
|
||||
TEST(new_and_wait_bash){
|
||||
struct arg_bash *arg= create_arg_bash();
|
||||
run_newbash((void*)arg);
|
||||
pthread_t th;
|
||||
pthread_create(&th, NULL, wait_newbash, ((void*)arg));
|
||||
|
||||
char buf[256];
|
||||
int len_buf = sprintf(buf," main Hello, voici le terminal avec new_bash_pid=%d et getpid=%d, old_bash_pid=%d\n\n",arg->new_bash_pid, getpid(), arg->new_bash_pid);
|
||||
|
||||
// char str[80];
|
||||
// int len_str=_xy_goto(str,0,0);
|
||||
char index_str[18];
|
||||
int len_index_str;
|
||||
for(int i=0;i<700;++i){
|
||||
if(i%70)write(1,":",1);
|
||||
else write(1,"\r",1);
|
||||
len_index_str=sprintf(index_str, "%d ", i);
|
||||
if(arg->fd_new_bash_pid>0){
|
||||
write(arg->fd_new_bash_pid, GOTO_TOP_LEFT, LEN_GOTO_TOP_LEFT);
|
||||
write(arg->fd_new_bash_pid, index_str, len_index_str);
|
||||
write(arg->fd_new_bash_pid, buf, len_buf);
|
||||
}
|
||||
if(arg->fd_current_bash_pid>0){
|
||||
write(arg->fd_current_bash_pid, GOTO_TOP_LEFT, LEN_GOTO_TOP_LEFT);
|
||||
write(arg->fd_current_bash_pid, index_str, len_index_str);
|
||||
write(arg->fd_current_bash_pid, buf, len_buf);
|
||||
|
||||
}
|
||||
usleep(40000);
|
||||
}
|
||||
write(1,"\n",1);
|
||||
|
||||
pthread_join(th, NULL);
|
||||
|
||||
free_arg_bash(arg);
|
||||
|
||||
}
|
||||
|
||||
|
||||
TEST(sleep_wait_bash){
|
||||
struct arg_bash *arg= create_arg_bash();
|
||||
launch_sleep_wait_bash((void*)arg);
|
||||
|
||||
char buf[256];
|
||||
int len_buf = sprintf(buf," main Hello, voici le terminal avec new_bash_pid=%d et getpid=%d, old_bash_pid=%d\n\n",arg->new_bash_pid, getpid(), arg->new_bash_pid);
|
||||
|
||||
char str[80];
|
||||
int len_str=_xy_goto(str,0,0);
|
||||
char index_str[8];
|
||||
int len_index_str;
|
||||
for(int i=0;i<200;++i){
|
||||
write(1,":",1);
|
||||
write(arg->fd_current_bash_pid, str, len_str);
|
||||
len_index_str=sprintf(index_str, "%d", i);
|
||||
write(arg->fd_current_bash_pid, index_str, len_index_str);
|
||||
write(arg->fd_current_bash_pid, buf, len_buf);
|
||||
usleep(40000);
|
||||
}
|
||||
write(1,"\n",1);
|
||||
|
||||
free_arg_bash(arg);
|
||||
|
||||
}
|
||||
|
||||
TEST(new_or_wait_bash){
|
||||
struct arg_bash *arg= create_arg_bash();
|
||||
pthread_t th;
|
||||
pthread_create(&th, NULL, wait_sleep_newbash, ((void*)arg));
|
||||
usleep(5000000);
|
||||
run_newbash((void*)arg);
|
||||
|
||||
char buf[256];
|
||||
int len_buf = sprintf(buf," main Hello, voici le terminal avec new_bash_pid=%d et getpid=%d, old_bash_pid=%d\n\n",arg->new_bash_pid, getpid(), arg->new_bash_pid);
|
||||
|
||||
// char str[80];
|
||||
// int len_str=_xy_goto(str,0,0);
|
||||
char index_str[18];
|
||||
int len_index_str;
|
||||
for(int i=0;i<700;++i){
|
||||
if(i%70)write(1,":",1);
|
||||
else write(1,"\r",1);
|
||||
len_index_str=sprintf(index_str, "%d ", i);
|
||||
if(arg->fd_new_bash_pid>0){
|
||||
write(arg->fd_new_bash_pid, GOTO_TOP_LEFT, LEN_GOTO_TOP_LEFT);
|
||||
write(arg->fd_new_bash_pid, index_str, len_index_str);
|
||||
write(arg->fd_new_bash_pid, buf, len_buf);
|
||||
}
|
||||
if(arg->fd_current_bash_pid>0){
|
||||
write(arg->fd_current_bash_pid, GOTO_TOP_LEFT, LEN_GOTO_TOP_LEFT);
|
||||
write(arg->fd_current_bash_pid, index_str, len_index_str);
|
||||
write(arg->fd_current_bash_pid, buf, len_buf);
|
||||
|
||||
}
|
||||
usleep(40000);
|
||||
}
|
||||
write(1,"\n",1);
|
||||
|
||||
pthread_join(th, NULL);
|
||||
|
||||
free_arg_bash(arg);
|
||||
|
||||
}
|
||||
|
||||
|
||||
void *func_bash(void* b_arg){
|
||||
struct arg_bash *bash_arg = (struct arg_bash*)b_arg;
|
||||
|
||||
while(check_go_on_bash(bash_arg)){
|
||||
|
||||
pthread_mutex_lock(bash_arg->mut_bash_var);
|
||||
while(
|
||||
(bash_arg->fd_new_bash_pid == -1) &&
|
||||
(bash_arg->fd_current_bash_pid == -1)
|
||||
){
|
||||
printf("debug: wait new bash!\n");
|
||||
pthread_cond_wait(bash_arg->cond_bash_var, bash_arg->mut_bash_var);
|
||||
}
|
||||
pthread_mutex_unlock(bash_arg->mut_bash_var);
|
||||
char buf[256];
|
||||
int len_buf = sprintf(buf," main Hello, voici le terminal avec new_bash_pid=%d et getpid=%d, old_bash_pid=%d\n\n",bash_arg->new_bash_pid, getpid(), bash_arg->new_bash_pid);
|
||||
|
||||
|
||||
char index_str[18];
|
||||
int len_index_str;
|
||||
for(int i=0;i<200;++i){
|
||||
//if(i%70)write(1,":",1);
|
||||
//else write(1,"\r",1);
|
||||
len_index_str=sprintf(index_str, "%d ", i);
|
||||
if(bash_arg->fd_new_bash_pid>0){
|
||||
write(bash_arg->fd_new_bash_pid, GOTO_TOP_LEFT, LEN_GOTO_TOP_LEFT);
|
||||
write(bash_arg->fd_new_bash_pid, index_str, len_index_str);
|
||||
write(bash_arg->fd_new_bash_pid, buf, len_buf);
|
||||
}
|
||||
if(bash_arg->fd_current_bash_pid>0){
|
||||
write(bash_arg->fd_current_bash_pid, GOTO_TOP_LEFT, LEN_GOTO_TOP_LEFT);
|
||||
write(bash_arg->fd_current_bash_pid, index_str, len_index_str);
|
||||
write(bash_arg->fd_current_bash_pid, buf, len_buf);
|
||||
|
||||
}
|
||||
usleep(40000);
|
||||
}
|
||||
//write(1,"\n",1);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
TEST(try_y_socket_manager){
|
||||
struct arg_bash *bash_arg= create_arg_bash();
|
||||
struct arg_var_ * var = create_arg_var_(y_nnn_manager_handle_input, bash_arg);
|
||||
struct y_socket_t *argS=y_socket_create("1600", 2, 3, var);
|
||||
|
||||
pthread_t pollTh;
|
||||
pthread_create(&pollTh, NULL, y_socket_poll_fds, (void*)argS);
|
||||
|
||||
pthread_t bashprinth;
|
||||
pthread_create(&bashprinth, NULL, func_bash, (void*)bash_arg);
|
||||
|
||||
wait_var_set_up_value_not_equal(var, 0);
|
||||
|
||||
|
||||
pthread_join(pollTh, NULL);
|
||||
|
||||
set_go_on_bash(bash_arg, 0);
|
||||
pthread_join(bashprinth, NULL);
|
||||
|
||||
y_socket_free(argS);
|
||||
free_arg_var_(var);
|
||||
free_arg_bash(bash_arg);
|
||||
|
||||
}
|
||||
|
||||
int main(int argc, char **argv){
|
||||
|
||||
|
||||
Reference in New Issue
Block a user