// start_routine for thread A void* threadA_routine (void *args) { // local variable char buf[BUFFER_SIZE]; int rc; ThreadData_t * this_data = (ThreadData_t *)args; for (;;) { // block until pipe write semaphore reached sem_wait(this_data->pipeWrite_sem); printf("......................................\n"); // read a line from data.txt // if eof reached, then thread A B C will stop rc = read_line(this_data->fp0, buf); if ((rc == 0) && (eof_reached == 1)) terminateAll(); // create a pipe pipe_create(this_data->pipefd); // write the line to pipe pipe_in(this_data->pipefd, buf); printf("thread A completed\n"); // post the pipe read signal sem_post(this_data->pipeRead_sem); } }
static void ps_update(void) { if(ps_list) { for(size_t i = 0; i < ps_n; i++) free(ps_list[i]); free(ps_list); } static const char *ps_cmd = "ps -e -o pid,ppid,uid,gid,state,nice,tty,command"; ps_list = pipe_in(ps_cmd, &ps_n, 1); }
void pipe_in(t_list *list, t_command *c, int input, int receive_from) { t_in *in; if (receive_from > -1) pipe_from_file(input, receive_from); if (list == NULL) return ; in = (t_in*)list->content; if (c->fd_in[in->id] && !c->fd_in[CLOSED]) write_in(in, input); pipe_in(list->next, c, input, -1); }
reader::reader(clpr_proc_db & db){ uint64_t global_label=0; while(1){ ifstream pipe_in(CLPR_INPUT_PIPE); //FIXME //struct stat st; //fstat(pipe_in, &st); //if (!(S_ISFIFO(st.st_mode))) // std::invalid_argument("bad or non-existant fifo"); string in; //input: //pidstat -d -u -h -l -r -w -U -v | grep -v root string host_info="?"; string index; while (getline(pipe_in,in)){ istringstream iss(in); vector<string> tokens{istream_iterator<string>{iss}, istream_iterator<string>{}}; //not a blank line nor a header if ((tokens.size()>0) && (tokens.size()!=23)){ if (tokens.size()<=20){ host_info=in; } if (tokens.size()>20){ index = get_uid_blob_label(tokens); uid_blob blob = db.get(tokens,index,global_label); blob.set(host_info,tokens); db.insert(blob,index); } //else { // host_info=in; //} } } } };
pid_t receipt_and_execute(t_shell *sh, t_command *c, int receipt_fd) { pid_t child; int pip[2]; pipe(pip); decompose_command(sh, c->line, c); child = my_fork(); if (child) { if (!is_default_in(c->fd_in, c->list_in, c)) dup2(pip[0], 0); do_execute_and_distrib(sh, c, pip); return (child); } else { close(pip[0]); if (!is_default_in(c->fd_in, c->list_in, c)) pipe_in(c->list_in, c, pip[1], receipt_fd); close(pip[1]); return (quit(sh, 0)); } }