void get_data(char *path) { DIR *rep; t_dirent *fr; t_file *dir; char *tmp; rep = opendir(path); dir = lst_new(path, path, 2); if (rep != NULL) { while ((fr = readdir(rep)) != NULL) { tmp = ft_strdup(fr->d_name); lst_push(lst_new(tmp, path, 0), &dir); free(tmp); } closedir(rep); } else { ft_error(path); return ; } while (dir->next->next != NULL) dir = dir->next; data_proc(dir); }
int main(int argc, char **argv) { l_lst *l_a; l_lst *l_b; int i; i = 1; l_a = lst_new(); l_b = lst_new(); if (argc < 2) error(); if (argc == 2) { write (1, "\n", 1); return (0); } while (i < argc) { l_a = lst_add_end(l_a, ft_atoi(argv[i])); i++; } l_a = l_sort(l_a, l_b); write(1, "\n", 1); return (0); }
t_app *app_new(int argc,char **argv) { t_app *app = (t_app *)mem_malloc(sizeof(t_app)); app->argc=argc; app->argv=argv; set_name(app->name,"minuit"); app->main_func = app_default_func; app->x_func = NULL; app->off_screen=0; app->frame=0; app->timer=0; app->timer_add = 1; app->timer_add_low = .1; app->timer_count=0; app->loop=APP_LOOP; app->buffer=APP_BUFFER; app->with_glut=APP_WITH_GLUT; app->debug_keyboard=0; app->load_file = 0; app->loaded_file = 0; app->client = 0; app->slave = 0; app->osc_server = 0; app->osc_client = 0; app->osc_port = 0; bzero( app->path_home, _PATH_); bzero( app->path_current, _PATH_); bzero( app->filename, _NAME_LONG_); bzero( app->path_file, _PATH_); app->mouse=mouse_new(); app->window=window_new(app->with_glut); app->keyboard=keyboard_new(); app->clock=clock_new( "clock"); app->video_frames = lst_new("video"); app->video_frames_swap = lst_new("video swap"); app->video_offset = 0; app->video_build = 1; app->video_limit = APP_VIDEO_LIMIT; clock_init(app->clock); app->quit = 0; return app; }
t_engine *engine_new(const char *name) { t_engine *engine = (t_engine *)mem_malloc(sizeof(t_engine)); id_init(&engine->id, name); engine->processes=lst_new("lst"); engine->garbage = lst_new("lst"); engine->with_global_limit=ENGINE_WITH_GLOBAL_LIMIT; engine->global_limit = 0; engine->global_freq=ENGINE_GLOBAL_FREQ; engine->process_count=0; engine->process_id = 0; return engine; }
int build_steps(t_ps *ps, int op_id) { t_lst *A2; A2 = lst_last(ps->ops, 0); if ((op_id == SA && A2->i == SA) || (op_id == SB && A2->i == SB) || (op_id == PA && A2->i == PB) || (op_id == PB && A2->i == PA) || (op_id == RA && A2->i == RRA) || (op_id == RRA && A2->i == RA) || (op_id == RB && A2->i == RRB) || (op_id == RRB && A2->i == RB) || (op_id == RRR && A2->i == RR) || (op_id == RR && A2->i == RRR) || (op_id == SS && A2->i == SS)) A2->i = 0; else if ((op_id == SA && A2->i == SB) || (op_id == SB && A2->i == SA)) A2->i = SS; else if ((op_id == RA && A2->i == RB) || (op_id == RB && A2->i == RA)) A2->i = RR; else if ((op_id == RRA && A2->i == RRB) || (op_id == RRB && A2->i == RRA)) A2->i = RRR; else if (A2->i == 0) A2->i = op_id; else if (A2->next) A2->next->i = op_id; else A2->next = lst_new(op_id); return (op_id); }
int main() { char* argumentos[MAX_ARGS + 1]; char buffer[BUFFER_SIZE]; list = lst_new(); int pid; pthread_t tid; pthread_mutex_init(&mutex, NULL); // Criar tarefa para monitorizar os filhos if(pthread_create(&tid, 0, tarefa_monitor,NULL) != 0) { perror("Erro no pthread_create: "); exit(EXIT_FAILURE); } while(1){ if(readLineArguments(argumentos,MAX_ARGS,buffer,BUFFER_SIZE) <= 0){ continue; } if(strcmp(argumentos[0],"exit")==0) { // exit - esperar pela tarefa monitora e imprimir as informacoes dos filhos pthread_mutex_lock(&mutex); exit_ative = true; pthread_mutex_unlock(&mutex); pthread_join(tid,NULL); lst_print(list); pthread_mutex_destroy(&mutex); exit(EXIT_SUCCESS); } else { pid = fork(); time_t starttime = time(NULL); if (pid < 0) { perror("Erro no fork"); continue; } if (pid == 0) { // Codigo do filho execv(argumentos[0], argumentos); perror("Erro no execv:"); exit(EXIT_FAILURE); } // Codigo do pai pthread_mutex_lock(&mutex); nfilhos++; insert_new_process(list,pid,starttime); pthread_mutex_unlock(&mutex); } } return 0; }
int main(int argc, char **argv) { t_lst *a; a = NULL; if (argc < 2) error(); while (argc > 1) lst_pushfront(&a, lst_new(ft_atoi(argv[--argc]))); a = sort(a); write(1, "\b\n", 2); lst_del(a); return (0); }
t_engine *engine_new(const char *name) { t_engine *engine = (t_engine *)malloc(sizeof(t_engine)); engine->id=0; engine->id_chunk=0; set_name(engine->name,name); engine->processes=lst_new("lst"); engine->with_global_limit=ENGINE_WITH_GLOBAL_LIMIT; engine->global_freq=ENGINE_GLOBAL_FREQ; engine->tot_processes=0; return engine; }
t_list *shortest_route(t_room *room) { t_room *tmp; int weight; t_list *route; t_list *name; tmp = room; route = NULL; while (tmp && tmp->end == 0) tmp = tmp->next; if (tmp->weight == 0) ft_error(7); weight = tmp->weight; route = lst_add(route, lst_new(tmp->name)); while (weight-- > 1) { name = tmp->links; tmp = room; tmp = find_next_node(weight, room, name); route = lst_add(route, lst_new(tmp->name)); } return (route); }
t_lst *block_leaves_get( t_block *block, int dir) { t_lst *branch = lst_new( "branch"); t_lst *bricks = block->bricks; t_link *l; t_block *target; t_brick *brick; for( l = bricks->first; l; l = l->next) { brick = l->data; target = get_block_connected( brick, dir); if( target) lst_add( branch, target, target->id.name); } return branch; }
int main(int argc, char *argv[]) { list_t *pilha; while(1){ /* initialize the stack */ pilha = lst_new(); /* integration and data processing */ leOperacao(pilha); /*free memory of the stack*/ lst_destroy(pilha); } return 0; }
/*=================================================== = Função que inicializa todas as = = variáveis e estruturas = ===================================================*/ void initialize_variables(){ /** * Criar listas dos processos filhos e dos terminais */ list = lst_new(); lista_terminais = terminais_new(); /** * Criar tarefa para monitorizar os filhos */ if(pthread_create(&tid, 0, tarefa_monitora,NULL) != 0) { perror("Erro no pthread_create: "); exit(EXIT_FAILURE); } /** * Inicializar mutex e variáveis de condição */ if(pthread_mutex_init(&mutex, NULL) < 0){ perror("Error no pthread_mutex_init: "); exit(EXIT_FAILURE); } if(pthread_cond_init(&podeCriarFilhos,NULL) < 0){ perror("Erro no pthread_cond_init do podeCriarFilhos: "); exit(EXIT_FAILURE); } if(pthread_cond_init(&podeMonitorizar,NULL) < 0){ perror("Erro no pthread_cond_init do podeMonitorizar: "); exit(EXIT_FAILURE); } // Garantir que não existe nenhuma ligação com este nome if(unlink(inputpipe) < 0){ if(errno != ENOENT) { perror("Erro no unlink no initialize_variables: "); exit(EXIT_FAILURE); } } // Criar pipe que recebe comandos dos terminais if(mkfifo(inputpipe, READWRITE_ONLY) < 0){ perror("Erro no mkfifo: "); exit(EXIT_FAILURE); } }
t_term *term_new(void) { t_term *term = (t_term *)malloc(sizeof(t_term)); term->tot_line=CTX_TERM_TOT_LINE; term->line=0; term->line_length=CTX_TERM_LINE_LENGTH; term->offset=0; term->cursor=NULL; term->lines=lst_new("lst"); term->is_init=0; term->loc_x=0; term->loc_y=0; term->draw=term_draw; term->init=term_init; return term; };
// copy a list t_lst *lst_copy(t_lst *lst) { if(lst->first) { t_lst *copy=lst_new(lst->name); t_link *link; for(link=lst->first;link;link=link->next) { void *data = link->data; lst_add(copy,data,link->name); } return copy; } else { return NULL; } }
int main(int argc, char **argv) { int x; int i; i = 1; x = 0; if (argc == 1) error(argv[0]); ard = lst_new(); ard = makelst(ard, argc, argv, 1); ft_select(ard); i = 0; while (i < (int)ard->len) { if (ard->start->bol == 2 || ard->start->bol == 3) printr(ard->start->str, x++, ft_celement(ard)); ard->start = ard->start->next; i++; } return (0); }
int main() { int inputPipe; char buffer[BUFFER_LEN]; char mktemp_filename[PATH_MAX]; char mktemp_dir[PATH_MAX]; int ret; int terminalPid; TerminalList * termlist = lst_new(); pthread_t threadMonitor; char *args[N_ARGS]; if (signal(SIGINT, handleSIGINT)) { fprintf(stderr, "Could not set sigint handler!\n"); exit(EXIT_FAILURE); } if (pthread_mutex_init(&mutexRunningProcesses, NULL)) { fprintf(stderr, "Could not create runningProcesses mutex\n"); exit(EXIT_FAILURE); } if (pthread_cond_init(&condRunningProcesses, NULL)) { fprintf(stderr, "Could not create runningProcesses cond\n"); exit(EXIT_FAILURE); } if (pthread_cond_init(&condFreeSlots, NULL)) { fprintf(stderr, "Could not create FreeSlots cond\n"); exit(EXIT_FAILURE); } strcpy(mktemp_dir, MKTEMP_TEMPLATE); TESTTRUE(mkdtemp(mktemp_dir)!=NULL, "Erro na criação do diretorio temporário (" _AT_ ")\n"); strncpy(mktemp_filename, mktemp_dir, PATH_MAX); strncpy(mktemp_filename+strlen(mktemp_filename), "/" INPUT_FILE, PATH_MAX-strlen(mktemp_filename)); fprintf(stdin, "Ficheiro de input: '%s'\n", mktemp_filename); if (mkfifo(mktemp_filename, 0660) <0) { fprintf(stderr, "Could not create fifo %s\n", mktemp_filename); exit(EXIT_FAILURE); } printf("A abrir o pipe %s para leitura...\n", mktemp_filename); if ((inputPipe = open(mktemp_filename, O_RDONLY)) < 0) { fprintf(stderr, "Could not create fifo " INPUT_FILE "\n"); exit(EXIT_FAILURE); } printf("A abrir o pipe %s para escrita...\n", mktemp_filename); if ((outPipe = open(mktemp_filename, O_WRONLY|O_NONBLOCK)) < 0) { fprintf(stderr, "Erro ao abrir o ficheiro de output" INPUT_FILE "\n"); exit(EXIT_FAILURE); } initProcessList(); if(pthread_create(&threadMonitor, 0,processMonitor, NULL)!= 0) { printf("Erro na criação da tarefa\n"); exit(EXIT_FAILURE); } while(1) { if (exitCalled) break; if (readFromPipe(inputPipe, buffer, &terminalPid, termlist)!=0) continue; printf("Comando: %s\n", buffer); ret = readLineArguments(args, N_ARGS, buffer, BUFFER_LEN); if (!ret) continue; processesWaitingToRun++; newProcess(args, terminalPid); } //Mata todos os processos de terminal lst_destroy(termlist); C_SIGNAL(&condRunningProcesses); pthread_join(threadMonitor, NULL); //The following function is called after all threads have joined, therefore there aren't used any mutexes exitParShell(); endProcessList(); pthread_mutex_destroy(&mutexRunningProcesses); pthread_cond_destroy(&condRunningProcesses); pthread_cond_destroy(&condFreeSlots); close(inputPipe); //aqui nao faz sentido testar o return destas funcoes close(outPipe); //aqui nao faz sentido testar o return destas funcoes unlink(INPUT_FILE); //aqui nao faz sentido testar o return destas funcoes return EXIT_SUCCESS; }
void __cls_plug_flow_operator_for(t_plug_mode mode,t_plug *plug,t_plug *plug_src) { t_context *C =ctx_get(); t_plug *plug_in = plug->src; t_brick *brick = plug->brick; // get bricks t_block *block = brick->block; t_brick *brick_indice = block_brick_get(block,"indice"); t_brick *brick_vector = block_brick_get(block,"vector"); t_plug *plug_indice = &brick_indice->plug_intern; t_plug *plug_vector = &brick_vector->plug_intern; t_plug *plug_vector_in = &brick_vector->plug_in; int *data_indice = plug_indice->data; t_vector *vector = plug_vector->data; // IN if(mode == mode_in) { // reset vector vector->data = NULL; // if for connected if(plug_in->is_connected ) { // get src t_plug *src_plug = plug_get_src(plug); t_data_type src_type = src_plug->data_type; t_vlst *vlst=NULL; switch(src_type) { // + VLST case dt_vlst: // SET VLST vlst = src_plug->data; // SET vector if(vlst) { vector->data = vlst->data; vector->type = vlst->data_type; vector->length = vlst->length; } break; default: plug_warning(plug,src_plug); break; } // SET VECTOR if(plug_vector_in->is_connected) { if(vlst) { // set vector, open for fisrt loop //if(!plug->is_eval) if(!for_init) //if(1==2) { if(C->ui->show_step) term_log(":FOR loop (INIT) %d",brick->counter); plug->is_eval = 1; for_init = 1; // get pointer float *ptr = vlst->data; // set pointer vector->data = ptr; // set indice *data_indice=0; t_plug *plug_vector_src = plug_vector_in->src; t_brick *brick_vector_src = plug_vector_src->brick; brick_vector_src->cls->trigger(brick_vector_src); t_lst *BRICKS = ctx_links_lst_get(); lst_add(BRICKS,brick,"for"); } else { if(brick->counter < vlst->count) { if(C->ui->show_step) term_log(":FOR loop %d",brick->counter); // get pointer float *ptr = vlst->data; // set pointer vector->data = ptr + (vlst->length * brick->counter); // set indice *data_indice=brick->counter; // get branch (all bricks) t_lst *lst=lst_new("lst"); block_branch_get(0,lst,block); // reset states ctx_links_reset(C,lst); // add to loop t_lst *BRICKS = ctx_links_lst_get(); t_link *l; t_brick *b; for(l=lst->first;l;l=l->next) { b = l->data; lst_add(BRICKS,l->data,b->name); } // free lst_free(lst); // counter ++ brick->counter++; } else { //XXX vector->data = NULL; // reset counter brick->counter = 0; for_init=0; } } } } else { *data_indice = 0; } } } }
void mode_init(t_mode *mode) { mode->modules=lst_new("lst"); }
int main(int argc, char **argv) { pid_t child_pid; /* child process id */ pthread_t monitor_thread; /* monitor thread */ char *args[CMD_ARGS_MAX+1], buffer_cmd[LINE_COMMAND_MAX]; /* cmd related */ char buffer_log[LINE_LOGFILE_MAX]; /* log related */ if((g_lst_children = lst_new()) == NULL) { if(fprintf(stderr, "lst_new: couldn't create list\n") < 0) handle_error("fprintf"); exit(EXIT_FAILURE); } g_log_file = f_open(PATH_LOGFILE_STR, "a+"); while(!feof(g_log_file)) { f_gets(buffer_log, LINE_LOGFILE_MAX, g_log_file); /* NULL or iteracao # */ if(feof(g_log_file)) break; if(sscanf(buffer_log, "%*s %d", &g_iterations) != 1) handle_error("sscanf"); f_gets(buffer_log, LINE_LOGFILE_MAX, g_log_file); /* PID: # time: # s */ f_gets(buffer_log, LINE_LOGFILE_MAX, g_log_file); /* total time: # s */ if(sscanf(buffer_log, "%*[^0-9] %d", &g_total_time) != 1) handle_error("sscanf"); } ++g_iterations; cond_init(&g_child_cv); cond_init(&g_monitoring_cv); mutex_init(&g_mutex); pcreate(&monitor_thread, process_monitor, NULL); while(true) { int numargs = readLineArguments(args, CMD_ARGS_MAX + 1, buffer_cmd, LINE_COMMAND_MAX); if(args[0] == NULL) continue; if(numargs < 0 || (numargs > 0 && !strcmp(args[0], COMMAND_EXIT_STR))) { mutex_lock(&g_mutex); g_monitoring = false; cond_signal(&g_monitoring_cv); mutex_unlock(&g_mutex); pjoin(monitor_thread); lst_print(g_lst_children); destroySharedResources(); f_close(g_log_file); return EXIT_SUCCESS; } else { FILE *fp; /* To check file existance */ if ((fp = fopen(args[0], "r")) == NULL) perror(args[0]); else { f_close(fp); mutex_lock(&g_mutex); while(g_num_children == MAXPAR) cond_wait(&g_child_cv, &g_mutex); mutex_unlock(&g_mutex); if((child_pid = fork()) < 0) perror("fork"); else if(child_pid == 0) { execv(args[0],args); destroySharedResources(); handle_error("execv"); } else { mutex_lock(&g_mutex); if(insert_new_process(g_lst_children, child_pid, time(NULL)) != 0) { fprintf(stderr, "insert_new_process: failed to insert new process\n"); exit(EXIT_FAILURE); } ++g_num_children; cond_signal(&g_monitoring_cv); mutex_unlock(&g_mutex); } } } } }
int main(int argc, char **argv){ char buffer[BUFFER_SIZE]; int numArgs; char *arg_vector[VECTOR_SIZE]; time_t starttime; int child_pid; pthread_t monitor_thread; pthread_t writer_thread; lst = lst_new(); writing_queue = new_queue(); /* Initialize synchronization objects */ pthread_mutex_init_(&mutex, NULL); pthread_cond_init_(&write_cond, 0); pthread_cond_init_(&max_par, NULL); pthread_cond_init_(&new_child, NULL); if ((log_fd = fopen("./log.txt", "a+")) == NULL){ perror("[ERROR] opening log file"); exit(EXIT_FAILURE); } read_log(); /* assign total time and iteration values for this execution */ pthread_create_(&monitor_thread, NULL, (void *)&monitor, NULL); /* Create Monitor Thread */ pthread_create_(&writer_thread, NULL, (void *)&writer, NULL); /* Create Writer Thread */ while (1) { numArgs = readLineArguments(arg_vector, VECTOR_SIZE, buffer, BUFFER_SIZE); if (numArgs <= 0)continue; if (strcmp(arg_vector[0], EXIT_COMMAND) == 0 ) { /* signal exit command */ exit_command = 1; /* wait for monitor thread to end */ pthread_cond_signal_(&new_child); /* must signal to catch exit flag */ pthread_join_(monitor_thread, NULL); pthread_join_(writer_thread, NULL); /* print all the elements in the list */ lst_print(lst); /* terminate sync objects */ pthread_mutex_destroy_(&mutex); pthread_cond_destroy_(&write_cond); pthread_cond_destroy_(&max_par); pthread_cond_destroy_(&new_child); fclose(log_fd); lst_destroy(lst); exit(EXIT_SUCCESS); } /* while there are child process slots available launch new child process, * else wait here */ pthread_mutex_lock_(&mutex); while (child_count >= MAXPAR) pthread_cond_wait_(&max_par, &mutex); pthread_mutex_unlock_(&mutex); child_pid = fork(); if (child_pid < 0){ /* test for error in fork */ perror("[ERROR] forking process"); continue; } if (child_pid == 0){ /* execute on child */ if (execv(arg_vector[0], arg_vector) == -1) { perror("[ERROR] executing program."); exit(EXIT_FAILURE); } } else { /* execute on parent */ /* Main thread * * Child processes are inserted into the process list saving their pid * and their start time. * Child counter is incremented. * The condition variable waiting for a new child process is signaled. */ time(&starttime); /* get start time of child process */ pthread_mutex_lock_(&mutex); insert_new_process(lst, child_pid, starttime); child_count++; pthread_cond_signal_(&new_child); pthread_mutex_unlock_(&mutex); } } }
void _op_obj_import(void) { t_context *C=ctx_get(); char *filename=C->event->standby_string; if(filename) { // init list OBJECTS=lst_new("lst"); // parse file t_file *file = file_new(filename); free(C->event->standby_string); //C->event->standby_function=NULL; C->event->callback=NULL; file_read(file); file_read_lines(file); //parse words t_link *link; t_link *l; t_word *word; for(link=file->lines->first;link;link=link->next) { t_line *line = link->data; line_read_words(line); } // parse tokens int object_start; int line_object; int is_face; //int tot_object; int tot_vert; int tot_face; int tot_tri; int tot_quad; int tot_indice; char *object_name; //tot_object=0; tot_face=0; tot_quad=0; tot_tri=0; object_start=0; for(link=file->lines->first;link;link=link->next) { // LINE t_line *line = link->data; // RESET is_face=0; for(l=line->words->first;l;l=l->next) { // WORD word=l->data; if(word_equal(word,"o")) { if(object_start) { obj_add(object_name,tot_vert,tot_face,tot_quad,tot_tri); tot_vert=0; tot_face=0; tot_quad=0; tot_tri=0; free(object_name); } else { object_start=1; } tot_vert=0; line_object=1; } else if(line_object) { object_name=(char *)malloc(sizeof(char)*(strlen(word->data)+1)); strcpy(object_name,word->data); line_object=0; } else if(word_equal(word,"v")) { tot_vert++; } else if(word_equal(word,"f")) { tot_face++; is_face=1; tot_indice=0; } else if(is_face) { tot_indice++; } else if(word_equal(word,"usemtl")) { } else if(word_equal(word,"s")) { } } if(is_face) { if(tot_indice==4) { tot_quad++; } else { tot_tri++; } } } // add last object obj_add(object_name,tot_vert,tot_face,tot_quad,tot_tri); // vars int is_data=0; int indice_vertex=0; int indice_face=0; int cursor_tri=0; int cursor_quad=0; int global_cursor=0; int tmp_global_cursor=0; int face[4]; object_start=0; t_token_type token; t_link *link_object; t_obj *obj; for(link=file->lines->first;link;link=link->next) { // LINE t_line *line = link->data; // reset is_data=0; indice_face=0; for(l=line->words->first;l;l=l->next) { // WORD word=l->data; if(word_equal(word,"o")) { token=token_object; if(object_start) { if(link_object->next) link_object=link_object->next; obj=link_object->data; // global cursor global_cursor+=tmp_global_cursor; tmp_global_cursor=obj->tot_vert; } //first else { link_object=OBJECTS->first; obj=link_object->data; object_start=1; tmp_global_cursor=obj->tot_vert; } indice_vertex=0; indice_face=0; cursor_tri=0; cursor_quad=0; } else if(word_equal(word,"v")) { token=token_vertex; } else if(word_equal(word,"f")) { token=token_face; } else if(word_equal(word,"usemtl")) { token=token_material; } else if(word_equal(word,"s")) { token=token_unknown; } else { is_data=1; } if(is_data) { if(token==token_vertex) { obj->verts[indice_vertex]=atof(word->data); indice_vertex++; } else if(token==token_face) { face[indice_face]=atoi(word->data); indice_face++; } } } // store face indice if(token==token_face) { int i; if(indice_face==3) { for(i=0;i<3;i++) { obj->tris[cursor_tri]=face[i]-global_cursor-1; cursor_tri++; } } else { for(i=0;i<4;i++) { obj->quads[cursor_quad]=face[i]-global_cursor-1; cursor_quad++; } } } } // add objects to scene C->scene->store=1; for(link=OBJECTS->first;link;link=link->next) { t_obj *obj = link->data; // new mesh t_node *node_mesh=mesh_make( obj->name, //"me_obj", obj->tot_vert, obj->tot_face, obj->tot_quad, obj->tot_tri, obj->verts, obj->quads, obj->tris); // new object t_node *node_object=object_add("mesh",obj->name); // link t_object *object=node_object->data; object->cls->link(object,node_mesh); } C->scene->store=0; // free obj for(link=OBJECTS->first;link;link=link->next) { t_obj *obj = link->data; obj_free(obj); } } }
/*//////////////////////////////////////////// /////////////// MAIN FUNCTION //////////////// ////////////////////////////////////////////*/ int main() { char* arg[7]; char buffer[100], filename[100], fnamePipeStats[100]; int vectorSize = 7, pid, numargs, fd, fDescPIPE, fDescPIPE_S; pthread_t waitChild; pid_t pidChild; terminalLst* terminals = new_terminal_lst(); l = lst_new(); if (pthread_mutex_lock(&mutex) != 0) { perror(error_mutex_lock); } if (pthread_mutex_unlock(&mutex) != 0) { perror(error_mutex_unlock); } if (pthread_mutex_init(&mutex, NULL) != 0){ perror(error_mutex); } if (pthread_cond_init(&can_wait, NULL) != 0) { perror(error_cond_init_wait); } if (pthread_cond_init(&can_execute, NULL) != 0) { perror(error_cond_init_execute); } if (pthread_create(&waitChild, NULL, waitThread, NULL) != 0) { perror(error_thread_create); exit(EXIT_FAILURE); } if (signal(SIGINT, CTRLC) == SIG_ERR) { perror(error_signal); } unlink(PIPE); /*-------------------------------------------------*/ if (mkfifo(PIPE, 0777) != 0) { perror(error_mkfifo); exit(EXIT_FAILURE); } if ((fDescPIPE = open(PIPE, O_RDONLY)) == -1) { perror(error_pipe_open); exit(EXIT_FAILURE); } close(0); dup2(fDescPIPE, 0); while (1) { numargs = readLineArguments(arg, vectorSize, buffer, 100); if(flag_signal) break; if (numargs <= 1) { continue; } if (pthread_mutex_lock(&mutex) != 0) { perror(error_mutex_lock); } while (numChildren == MAXPAR) { if (pthread_cond_wait(&can_execute, &mutex) != 0) { perror(error_cond_wait); } } if (pthread_mutex_unlock(&mutex) != 0) { perror(error_mutex_unlock); } else if (strcmp(arg[0], EXIT) != 0) { if (strcmp(arg[0], starter) == 0) { int pid_terminal = atoi(arg[1]); insert_terminal(terminals, pid_terminal); continue; } if (strcmp(arg[0], stats) == 0) { /*tratar o PID e meter na lista*/ sprintf(fnamePipeStats, "%s-%s", PIPE_S, arg[1]); while((fDescPIPE_S = open(fnamePipeStats, O_WRONLY)) < 0); if(fDescPIPE < 0) { perror(error_pipe_open); exit(EXIT_FAILURE); } if (pthread_mutex_lock(&mutex) != 0) { perror(error_mutex_lock); } sprintf (buffer, "Number of active children: %d\nTotal execution time to the moment: %d", numChildren, timeTotal); write(fDescPIPE_S, buffer, strlen(buffer)); if (pthread_mutex_unlock(&mutex) != 0) { perror(error_mutex_unlock); } close(fDescPIPE_S); continue; } if (strcmp(arg[0], EXITGLOBAL) == 0) { /* SONFOILULDNILDEFNC*/ printf("1\n"); CTRLC(SIGINT); } else { pid = fork(); if (pid < 0) { perror(error_fork); } if (pthread_mutex_lock(&mutex) != 0) { perror(error_mutex_lock); } insert_new_process(l, pid, time(NULL)); numChildren++; if (pthread_cond_signal(&can_wait) != 0) { perror(error_cond_wait); } if (pthread_mutex_unlock(&mutex) != 0) { perror(error_mutex_unlock); } if (pid == 0) { pidChild = getpid(); sprintf(filename, "par-shell-%d.txt", pidChild); fd = open(filename, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR); close(1); dup(fd); close(fd); if (execv(arg[0], arg) < 0) { perror(error_execv); exit(EXIT_FAILURE); } } else { sprintf(filename, "par-shell-%d.txt", pid); } } } else { break; } } isExit = true; if (pthread_cond_signal(&can_wait) != 0) { perror(error_cond_wait); } if (pthread_mutex_unlock(&mutex) != 0) { perror(error_mutex_unlock); } if (pthread_join(waitChild, NULL) != 0) { perror(error_thread_join); } lst_print(l); lst_destroy(l); while(remove_terminal(terminals)!= -1){ } if (pthread_cond_destroy(&can_wait) != 0) { perror(error_cond_destroy_wait); } if (pthread_cond_destroy(&can_execute) != 0) { perror(error_cond_destroy_execute); } exit(EXIT_SUCCESS); return 0; }