int NdbInfoScanOperation::execute()
{
  DBUG_ENTER("NdbInfoScanOperation::execute");
  DBUG_PRINT("info", ("name: '%s', id: %d",
             m_table->getName(), m_table->getTableId()));

  if (m_state != Prepared)
    DBUG_RETURN(NdbInfo::ERR_WrongState);

  assert(m_cursor.size() == 0);
  m_state = MoreData;

  m_signal_sender->lock();

  if (!find_next_node())
  {
    m_signal_sender->unlock();
    DBUG_RETURN(NdbInfo::ERR_ClusterFailure);
  }

  int ret = sendDBINFO_SCANREQ();
  m_signal_sender->unlock();

  DBUG_RETURN(ret);
}
/*The main body of the scheduling algorithm*/
int algorithm () {
    int i, coreid;
    int des_core;
    for (ticks = 0; task_not_empty(); ticks++) {
        for (coreid = 0; coreid < NCORE; coreid++) {
            if (core[coreid].idle == 0 && core[coreid].remain > 0)
                core[coreid].remain--;
            if (core[coreid].remain == 0 && core[coreid].idle == 0) {
                core[coreid].idle = 1;
                core[coreid].last_node = core[coreid].curr_node;
                core[coreid].curr_node = -1;
                /*如果之前有任务执行,则将之前任务发出的有向边去掉*/
                /*If there was some tasks executing on the core, then eliminate the directed edge 
                 * from the previous task (vertex).*/
                if (core[coreid].last_node != -1) {
                    for (i = 0; i < NUM + 1; i++) {
                        if (dag_new[core[coreid].last_node][i] == 1) {
                            dag_dynamic [core[coreid].last_node][i] = 0; 
                            testnode[i].id--;
                        } 
                    }
                } 
            }
        }

        need_schedule = 0;
        need_schedule = find_next_node ();
        if (need_schedule) {
            des_core = -1;
            while ( check_free_core () && exist_pri (3)) {
                task = pick_pri (3);
                des_core = -1;
                des_core = search_core_satisfy (task);
                if (des_core > -1)  schedule_to (task, des_core);
                else  schedule (task);
            }
            while ( check_free_core () && exist_pri (2) ) {
                task = pick_pri (2);
                schedule (task);
            }
            while ( check_free_core () && exist_pri (1) ) {
                task = pick_pri (1);
                des_core = -1;
                des_core = search_core_satisfy (task);
                if (des_core > -1)  schedule_to (task, des_core);
                else  schedule (task);
            }
            while ( check_free_core () && exist_pri (0) ) {
                task = pick_pri (0);
                schedule (task);
            }	
        }
    }
    int large = 0;
    return ticks;
}
Exemple #3
0
 virtual Cost search() {
   Q = Queue();
   MC.clear();
   init();
   
   while ( ! Q.empty() ) {
     Node node = Q.top();
     Q.pop();
     
     if ( is_goal(node) )
       return node.first;
     
     find_next_node(node);
   }
   
   return get_none_cost();
 }
Exemple #4
0
/***************************entry point*******************************/
plc_t parse_ld_program(const char * name, 
                       const char lines[][MAXSTR], 
                       plc_t p) {
    int rv = PLC_OK;
    if(p == NULL){
    
        return NULL;
    }    
    unsigned int len = program_length(lines, MAXBUF); 
    ld_line_t * program = construct_program(lines, len);
    
    int node = 0; 
    while(rv >= PLC_OK 
       && node >= 0) {
        rv = horizontal_parse(len, program);
        if(rv >= PLC_OK){
        
            node = find_next_node(program, node, len);
        }
        if(node >= 0){
        
            rv = vertical_parse(node, len, program);
        }
    }
    if(rv < PLC_OK){  
    
        p->status = rv;
    } else {
    
        p = generate_code(len, name, program, p);
        
        char dump[MAXBUF];
        memset(dump, 0, MAXBUF);
        dump_rung(p->rungs[0], dump);
        plc_log(dump);
    }
    destroy_program(len, program);
    return p;
}
node_t *delete_node(node_t *root, node_t *target)
{
	if (target == NULL)
		return root;

	if (target->left && target->right) {
		node_t *successor = find_next_node(target);
		target->value = successor->value;
		target = successor;
	}
	node_t *l = target->left;
	node_t *r = target->right;
	node_t *p = target->parent;

	if (!l && !r) {
		if (p == NULL) {
			free(target);
			return NULL;
		}
		if (target == p->left)
			p->left = NULL;
		else
			p->right = NULL;
		free(target);
	} else if (!l || !r) {
		node_t *tmp = (l == NULL) ? r : l;
		if (p == NULL) {
			free(target);
			return tmp;
		}
		if (target == p->left)
			p->left = tmp;
		else
			p->right = tmp;
		free(target);
	}
	return root;
}
Exemple #6
0
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);
}
Exemple #7
0
int main(int argc, char* argv[]){
	int p,i,n,ab;
	h = (int) strtol(argv[1], (char **)NULL, 10);
	h=h+1;
	char *m = malloc(sizeof(argv[2]));
	strcpy(m,argv[2]);
	s = (int) strtol(argv[3], (char **)NULL, 10);
	d = (int) strtol(argv[4], (char **)NULL, 10);
	//s=7;d=8;h=4;
	getMessagePath();
	printf("Final Path -\t");
	for(i=0;i<path_size;i++){
		printf("%d\t",path[i]);
	}
	int pipe_count = (int) (pow(2,h) -1);
	int fd[pipe_count][2];
	for(i=0;i<pipe_count;i++){
		fd[i][0]= -1;
		fd[i][1] = -1;
	}
	pid_t root_pid = getpid();
	printf("\n");
	char readbuffer[100];
	char dest_buffer[100];
	pid_t t1=-1,t2=-1;
	//printf("root is %d, t1 is %d, t2 is %d\n",getpid(),t1,t2);
	int parent_index = 0;
	int nbytes;
	pipe(fd[0]);
	for(i=1;i<=(h-1);i++){
		t1= fork();
		if(t1!=0){
			t2 = fork();
		}
		if(t1==0 && t2!=0){ // first child is invoked
			parent_index = (parent_index*2)+1;
			pipe(fd[parent_index]);
			
		}
		if(t2==0 && t1!=0){ // second child is invoked
			parent_index = (parent_index*2)+2;
			pipe(fd[parent_index]);
		}
		if(t1!=0 && t2!=0){

			break;
		}
		t1=-1;t2=-1;
	}
	//printf("PIPES of %d - %d & %d\n",parent_index,fd[parent_index][0], fd[parent_index][1]);fflush(stdout);
	while(nodeInPath(parent_index) == 1){
		//printf("entering while %d\n",parent_index);fflush(stdout);
		nbytes = 0;
		if(parent_index == s){
			int next_node = find_next_node(s);
			if(next_node < parent_index){ //if next node is parent then write to parent
				write(fd[next_node][1], m, (strlen(m)+1));
				printf("Transmitting from source at %d\n",s);fflush(stdout);
				close(fd[next_node][1]);
			}else{ // else write to self
				write(fd[parent_index][1], m, (strlen(m)+1));
				printf("Transmitting from source at %d\n",s);fflush(stdout);
				close(fd[parent_index][1]);
			}
			exit(0);
		}else if(parent_index != d){
			int next_node = find_next_node(parent_index);
			int prev_node = find_prev_node(parent_index);
			//printf("Preparing to forward at %d\n",parent_index);fflush(stdout);
			if(prev_node < parent_index && next_node > parent_index){//prev is parent and next is child
				nbytes = read(fd[prev_node][0], dest_buffer, 100);
				close(fd[prev_node][0]);
				write(fd[parent_index][1], dest_buffer, sizeof(dest_buffer));
				printf("Forwarding at %d\n",parent_index);fflush(stdout);
				close(fd[parent_index][1]);
				//printf("Writing --%d bytes-- to %d and Exiting %d\n",nbytes,next_node,parent_index);fflush(stdout);

			}else if(prev_node > parent_index && next_node < parent_index){//prev is child and next is parent
				nbytes = read(fd[parent_index][0], dest_buffer, 100);
				close(fd[parent_index][0]);
				write(fd[next_node][1], dest_buffer, sizeof(dest_buffer));
				printf("Forwarding at %d\n",parent_index);fflush(stdout);
				close(fd[next_node][1]);
				//printf("Writing --%d bytes-- to %d and Exiting %d\n",nbytes,next_node,parent_index);fflush(stdout);
			}else if(prev_node > parent_index && next_node > parent_index){
				nbytes = read(fd[parent_index][0], dest_buffer, 100);
				printf("Forwarding at %d\n",parent_index);fflush(stdout);
				write(fd[parent_index][1], dest_buffer, sizeof(dest_buffer));
			}
			exit(0);
			
		}else if(parent_index == d){
			int prev_node = find_prev_node(parent_index);
			if(prev_node < parent_index){ // if prev node is parent
				nbytes = read(fd[prev_node][0], dest_buffer, sizeof(dest_buffer));
				close(fd[prev_node][0]);
			}else{
				nbytes = read(fd[parent_index][0], dest_buffer, sizeof(dest_buffer));
				close(fd[parent_index][0]);
			}
			printf("Transmission complete at %d with %s\n",parent_index,dest_buffer);fflush(stdout);
			exit(0);
			
		}
	}
	wait(NULL);
	wait(NULL);
	//printf("Exiting %d\n",parent_index);
}
Exemple #8
0
void avl_delete_node(AVL *tree, AVLNODE *node)
{     /* delete specified node from AVL tree */
      AVLNODE *f, *p, *q, *r, *s, *x, *y;
      short int flag;
      p = node;
      /* if both subtrees of the specified node are non-empty, the node
         should be interchanged with the next one, at least one subtree
         of which is always empty */
      if (p->left == NULL || p->right == NULL) goto skip;
      f = p->up; q = p->left;
      r = find_next_node(tree, p); s = r->right;
      if (p->right == r)
      {  if (f == NULL)
            tree->root = r;
         else
            if (p->flag == 0) f->left = r; else f->right = r;
         r->rank = p->rank; r->up = f;
         r->flag = p->flag; r->bal = p->bal;
         r->left = q; r->right = p;
         q->up = r;
         p->rank = 1; p->up = r; p->flag = 1;
         p->bal = (short int)(s == NULL ? 0 : +1);
         p->left = NULL; p->right = s;
         if (s != NULL) s->up = p;
      }
      else
      {  x = p->right; y = r->up;
         if (f == NULL)
            tree->root = r;
         else
            if (p->flag == 0) f->left = r; else f->right = r;
         r->rank = p->rank; r->up = f;
         r->flag = p->flag; r->bal = p->bal;
         r->left = q; r->right = x;
         q->up = r; x->up = r; y->left = p;
         p->rank = 1; p->up = y; p->flag = 0;
         p->bal = (short int)(s == NULL ? 0 : +1);
         p->left = NULL; p->right = s;
         if (s != NULL) s->up = p;
      }
skip: /* now the specified node [p] has at least one empty subtree;
         go upstairs to the root and adjust the rank field of all nodes
         affected by deletion */
      q = p; f = q->up;
      while (f != NULL)
      {  if (q->flag == 0) f->rank--;
         q = f; f = q->up;
      }
      /* delete the specified node from the tree */
      f = p->up; flag = p->flag;
      q = p->left != NULL ? p->left : p->right;
      if (f == NULL)
         tree->root = q;
      else
         if (flag == 0) f->left = q; else f->right = q;
      if (q != NULL) q->up = f, q->flag = flag;
      tree->size--;
      /* go upstairs to the root and correct all subtrees affected by
         deletion */
      while (f != NULL)
      {  if (flag == 0)
         {  /* the height of the left subtree of [f] is decreased */
            if (f->bal == 0)
            {  f->bal = +1;
               break;
            }
            if (f->bal < 0)
               f->bal = 0;
            else
            {  f = rotate_subtree(tree, f);
               if (f->bal < 0) break;
            }
            flag = f->flag; f = f->up;
         }
         else
         {  /* the height of the right subtree of [f] is decreased */
            if (f->bal == 0)
            {  f->bal = -1;
               break;
            }
            if (f->bal > 0)
               f->bal = 0;
            else
            {  f = rotate_subtree(tree, f);
               if (f->bal > 0) break;
            }
            flag = f->flag; f = f->up;
         }
      }
      /* if the root has been reached, the height of the entire tree is
         decreased */
      if (f == NULL) tree->height--;
      /* returns the deleted node to the memory pool */
      dmp_free_atom(tree->pool, p, sizeof(AVLNODE));
      return;
}
int NdbInfoScanOperation::receive(void)
{
  DBUG_ENTER("NdbInfoScanOperation::receive");
  while (true)
  {
    const SimpleSignal* sig = m_signal_sender->waitFor();
    if (!sig)
      DBUG_RETURN(-1);
    //sig->print();

    int sig_number = sig->readSignalNumber();
    switch (sig_number) {

    case GSN_DBINFO_TRANSID_AI:
    {
      if (execDBINFO_TRANSID_AI(sig))
        continue;  // Wait for next signal

      if (m_rows_received < m_rows_confirmed)
        DBUG_RETURN(1); // Row available

      // All rows in this batch recieved
      assert(m_rows_received == m_rows_confirmed);

      if (m_cursor.size() == 0 && !find_next_node())
      {
        DBUG_PRINT("info", ("No cursor -> EOF"));
        m_state = End;
        DBUG_RETURN(1); // Row available(will get End on next 'nextResult')
      }

      // Cursor is still set, fetch more rows
      assert(m_state == MoreData);
      int err = sendDBINFO_SCANREQ();
      if (err != 0)
      {
        DBUG_PRINT("error", ("Failed to request more data"));
        assert(m_state == Error);
        // Return error immediately
        DBUG_RETURN(err);
      }

      DBUG_RETURN(1); // Row available
      break;
    }

    case GSN_DBINFO_SCANCONF:
    {
      if (execDBINFO_SCANCONF(sig))
        continue; // Wait for next signal

      if (m_rows_received < m_rows_confirmed)
        continue;  // Continue waiting(for late TRANSID_AI signals)

      // All rows in this batch recieved
      assert(m_rows_received == m_rows_confirmed);

      if (m_cursor.size() == 0 && !find_next_node())
      {
        DBUG_PRINT("info", ("No cursor -> EOF"));
        m_state = End;
        DBUG_RETURN(0); // No more rows
      }

      // Cursor is still set, fetch more rows
      assert(m_state == MoreData);
      int err = sendDBINFO_SCANREQ();
      if (err != 0)
      {
        DBUG_PRINT("error", ("Failed to request more data"));
        assert(m_state == Error);
        DBUG_RETURN(err);
      }

      continue;
    }

    case GSN_DBINFO_SCANREF:
    {
      int error;
      if (execDBINFO_SCANREF(sig, error))
        continue; // Wait for next signal
      assert(m_state == Error);
      DBUG_RETURN(error);
      break;
    }

    case GSN_NODE_FAILREP:
    {
      const NodeFailRep * const rep =
        CAST_CONSTPTR(NodeFailRep, sig->getDataPtr());
      if (NdbNodeBitmask::get(rep->theNodes, m_node_id))
      {
        DBUG_PRINT("info", ("Node %d where scan was runnig failed", m_node_id));
        m_state = Error;
        DBUG_RETURN(NdbInfo::ERR_ClusterFailure);
      }
      break;
    }

    case GSN_NF_COMPLETEREP:
      // Already handled in NODE_FAILREP
      break;

    case GSN_SUB_GCP_COMPLETE_REP:
    case GSN_API_REGCONF:
    case GSN_TAKE_OVERTCCONF:
    case GSN_CONNECT_REP:
      // ignore
      break;

    default:
      DBUG_PRINT("error", ("Got unexpected signal: %d", sig_number));
      assert(false);
      break;
    }
  }
  assert(false); // Should never come here
  DBUG_RETURN(-1);
}
Exemple #10
0
/* Returns the number of added collation keys */
static MVMint64 collation_push_cp (MVMThreadContext *tc, collation_stack *stack, MVMCodepointIter *ci, int *cp_maybe, int cp_num, char *name) {
    MVMint64 rtrn = 0;
    MVMCodepoint cps[10];
    MVMint64 num_cps_processed = 0;
    int query = -1;
    int cp_num_orig = cp_num;
    /* If supplied -1 that means we need to grab it from the codepoint iterator. Otherwise
     * the value we were passed is the codepoint we should process */
    if (cp_num == 0) {
        cps[0] = MVM_string_ci_get_codepoint(tc, ci);
        cp_num = 1;
    }
    else {
        MVMint32 i;
        for (i = 0; i < cp_num; i++) {
            cps[i] = cp_maybe[i];
        }
    }
    query = get_main_node(tc, cps[0], 0, starter_main_nodes_elems);
    if (query != -1) {
        DEBUG_PRINT_SUB_NODE(main_nodes[query]);
        /* If there are no sub_node_elems that means we don't need to look at
         * the next codepoint, we are already at the correct node
         * If there's no more codepoints in the iterator we also are done here */
        if (main_nodes[query].sub_node_elems < 1 || (cp_num < 2 && !MVM_string_ci_has_more(tc, ci))) {
            collation_add_keys_from_node(tc, NULL, stack, ci, name, cps[0],  &main_nodes[query]);
            num_cps_processed++;
        }
        /* Otherwise we need to check the next codepoint(s) (0 < sub_node_elems) */
        else {
            MVMint64 last_good_i      =  0,
                     last_good_result = -1;
            MVMint64 i, result = query;
            DEBUG_PRINT_SUB_NODE(main_nodes[query]);
            for (i = 0; result != -1 && MVM_string_ci_has_more(tc, ci) && i < 9;) {
                i++;
                /* Only grab a codepoint if it doesn't already exist in the array */
                if (cp_num <= i) {
                    cps[i] = MVM_string_ci_get_codepoint(tc, ci);
                    cp_num++;
                }
                result = find_next_node(tc, main_nodes[result], cps[i]);
                /* If we got something other than -1 and it has collation elements
                 * store the value so we know how far is valid */
                if (result != -1 && main_nodes[result].collation_key_elems != 0) {
                    last_good_i      = i;
                    last_good_result = result;
                }
                if (result != -1)
                    DEBUG_PRINT_SUB_NODE(main_nodes[result]);
            }
            /* If there is no last_good_result we should return a value from main_nodes */
            DEBUG_PRINT_SUB_NODE( (last_good_result == -1 ? main_nodes[query] : main_nodes[last_good_result]) );
            /* If the terminal_subnode can't be processed then that means it will push the starter codepoint ( cp[0] )'s value onto
             * the stack, and we must set last_good_i to 0 since it didn't work out */
            if (!collation_add_keys_from_node(tc, (last_good_result == -1 ? NULL : &main_nodes[last_good_result]), stack, ci, name, cps[0], &main_nodes[query])) {
                /* If we get 0 from collation_add_keys_from_node then we only processed
                 * a single codepoint so set last_good_i to 0 */
                last_good_i = 0;
            }
            num_cps_processed = last_good_i + 1;
        }
    }
    else {
        /* Push the first codepoint onto the stack */
        rtrn = collation_push_MVM_values(tc, cps[0], stack, ci, name);
        num_cps_processed = 1;
    }
    /* If there are any more codepoints remaining call collation_push_cp on the remaining */
    if (num_cps_processed < cp_num) {
        return num_cps_processed + collation_push_cp(tc, stack, ci, cps + num_cps_processed, cp_num - num_cps_processed, name);
    }
    return num_cps_processed;
}
Exemple #11
0
int main(int argc, char* argv[]){
	int p,i,n,ab;
	h = (int) strtol(argv[1], (char **)NULL, 10);
	h=h+1;
	char *m = malloc(sizeof(argv[2]));
	strcpy(m,argv[2]);
	s_tree = (int) strtol(argv[3], (char **)NULL, 10);
	s = (int) strtol(argv[4], (char **)NULL, 10);
	d_tree = (int) strtol(argv[5], (char **)NULL, 10);
	d = (int) strtol(argv[6], (char **)NULL, 10);
	//s_tree = 1;d_tree = 0;
	//s=7;d=14;h=4;
	getMessagePath();
	//create a shared segment
	int segment_id; 
	char *shared_memory; 
	const int size = 4096; 
	//printf("Size of Shared Memory - %d\n"+strlen(shared_memory));
	printf("Final Path -\t");
	for(i=0;i<path_size;i++){
		printf("%d\t",path[i]);
	}
	//printf("\nIS in PAth - %d\n",nodeInForestPath(6,1));
	int pipe_count = (int) (pow(2,h+1) -2);
	int fd[pipe_count][2];
	for(i=0;i<pipe_count;i++){
		fd[i][0]= -1;
		fd[i][1] = -1;
	}
	pid_t root_pid = getpid();
	printf("\n");
	char readbuffer[100];
	char dest_buffer[100];
	pid_t t1=-1,t2=-1;
	//printf("root is %d, t1 is %d, t2 is %d\n",getpid(),t1,t2);
	int parent_index = -10;
	int tree_index = 0;
	int nbytes;
	for(i=1;i<=h;i++){
		t1= fork();
		if(t1!=0){
			t2 = fork();
		}
		if(t1==0 && t2!=0){ // first child is invoked
			if(parent_index == -10){
				parent_index = 0;
			}else{
				parent_index = (parent_index*2)+1;
				//pipe(fd[parent_index]);
			}
			pipe(fd[(tree_index*(pipe_count/2))+parent_index]);
			
		}
		if(t2==0 && t1!=0){ // second child is invoked
			if(parent_index == -10){
				parent_index = 0;
				tree_index = 1;
			}else{
				parent_index = (parent_index*2)+2;
			}
			pipe(fd[(tree_index*(pipe_count/2))+parent_index]);
		}
		if(t1!=0 && t2!=0){

			break;
		}
		t1=-1;t2=-1;
	}
	
	//printf("PIPES of %d in %d tree - %d & %d\n",parent_index,tree_index,fd[(tree_index*(pipe_count/2))+parent_index][0], fd[(tree_index*(pipe_count/2))+parent_index][1]);fflush(stdout);
	while(nodeInForestPath(parent_index, tree_index) == 1){
		
		/*
		*ignore process with index -10
		*
		*/
		if(parent_index != -10){
			//printf("Prev Node for %d from Tree %d in Path - %d\n",parent_index,tree_index,find_prev_node(parent_index,tree_index));fflush(stdout);
			nbytes = 0;
			if(parent_index == s && tree_index == s_tree){
				int next_node = find_next_node(s, s_tree);
				if( next_node == -10){
					//write to shared memory
					printf("Writing to SHared Memory from Source \n");fflush(stdout);
					segment_id = shmget(KEY, size, S_IRUSR|S_IWUSR|IPC_CREAT);
					shared_memory=(char *)shmat(segment_id, NULL,0); 
					sprintf(shared_memory, m);
					shmdt(shared_memory); 
					break;
				}else if(next_node != -1){
					if(next_node < parent_index){ //if next node is parent then write to parent
						write(fd[((pipe_count/2)*tree_index)+next_node][1], m, (strlen(m)+1));
						printf("Transmitting from source at %d of Tree %d \n",s,tree_index);fflush(stdout);
						close(fd[((pipe_count/2)*tree_index)+next_node][1]);
					}else{ // else write to self
						write(fd[((pipe_count/2)*tree_index)+parent_index][1], m, (strlen(m)+1));
						printf("Transmitting from source at %d of Tree %d\n",s,tree_index);fflush(stdout);
						close(fd[((pipe_count/2)*tree_index)+parent_index][1]);
					}
					break;
				}
				
			}else if(parent_index != d){
				int next_node = find_next_node(parent_index,tree_index);
				int prev_node = find_prev_node(parent_index,tree_index);
				if(prev_node == -10){
					//read from shared mem and write to self
					segment_id = shmget(KEY, size, S_IRUSR|S_IWUSR|IPC_CREAT);
					shared_memory=(char *)shmat(segment_id, NULL,0);
					while(strlen(shared_memory) <= 0){
						sleep(1);
						shmdt(shared_memory);
						shared_memory=(char *)shmat(segment_id, NULL,0);
					}
					write(fd[((pipe_count/2)*tree_index)+parent_index][1], shared_memory, strlen(shared_memory));
					printf("Forwarding at Root %d of Tree %d Through Shared Memory\n",parent_index,tree_index);fflush(stdout);
					//shmdt(shared_memory);
					close(fd[((pipe_count/2)*tree_index)+parent_index][1]);
					shmdt(shared_memory);
					break;

				}
				if(next_node == -10){
					//read from self and write to shared mem
					nbytes = read(fd[((pipe_count/2)*tree_index)+parent_index][0], dest_buffer, sizeof(dest_buffer));
					close(fd[((pipe_count/2)*tree_index)+parent_index][0]);
					segment_id = shmget(KEY, size, S_IRUSR|S_IWUSR|IPC_CREAT);
					shared_memory=(char *)shmat(segment_id, NULL,0);
					sprintf(shared_memory, dest_buffer); 
					printf("Forwarding at Root %d of Tree %d Through Shared Memory\n",parent_index,tree_index);fflush(stdout);
					shmdt(shared_memory); 
					break;
				}
				if(prev_node != -10 && next_node != -10){
					//printf("Preparing to forward at %d\n",parent_index);fflush(stdout);
					if(prev_node < parent_index && next_node > parent_index){//prev is parent and next is child
						nbytes = read(fd[((pipe_count/2)*tree_index)+prev_node][0], dest_buffer, sizeof(dest_buffer));
						close(fd[((pipe_count/2)*tree_index)+prev_node][0]);
						write(fd[((pipe_count/2)*tree_index)+parent_index][1], dest_buffer, sizeof(dest_buffer));
						printf("Forwarding at %d of Tree %d\n",parent_index,tree_index);fflush(stdout);
						close(fd[((pipe_count/2)*tree_index)+parent_index][1]);
						break;
						//printf("Writing --%d bytes-- to %d and Exiting %d\n",nbytes,next_node,parent_index);fflush(stdout);

					}else if(prev_node > parent_index && next_node < parent_index){//prev is child and next is parent
						nbytes = read(fd[((pipe_count/2)*tree_index)+parent_index][0], dest_buffer, sizeof(dest_buffer));
						close(fd[((pipe_count/2)*tree_index)+parent_index][0]);
						write(fd[((pipe_count/2)*tree_index)+next_node][1], dest_buffer, sizeof(dest_buffer));
						printf("Forwarding at %d of Tree %d\n",parent_index,tree_index);fflush(stdout);
						close(fd[((pipe_count/2)*tree_index)+next_node][1]);
						break;
						//printf("Writing --%d bytes-- to %d and Exiting %d\n",nbytes,next_node,parent_index);fflush(stdout);
					}else if(prev_node > parent_index && next_node > parent_index){
						nbytes = read(fd[parent_index][0], dest_buffer, sizeof(dest_buffer));
						printf("Forwarding at %d of Tree %d\n",parent_index,tree_index);fflush(stdout);
						write(fd[parent_index][1], dest_buffer, sizeof(dest_buffer));
						break;
					}
					
				}
				
				
			}else if(parent_index == d && tree_index == d_tree){
				int prev_node = find_prev_node(parent_index, tree_index);
				if(prev_node == -10){
					//read from shared memory
					segment_id = shmget(KEY, size, S_IRUSR|S_IWUSR|IPC_CREAT);
					shared_memory=(char *)shmat(segment_id, NULL,0);
					while(strlen(shared_memory) <= 0){
						sleep(1);
						shmdt(shared_memory);
						shared_memory=(char *)shmat(segment_id, NULL,0);
					}
					printf("Transmission complete at %d of Tree through ShM %d with %s\n",parent_index,tree_index,shared_memory);fflush(stdout);
					shmdt(shared_memory); 
					shmctl(segment_id,IPC_RMID,NULL); 
					break;

				}else if(prev_node != -1){
					if(prev_node < parent_index){ // if prev node is parent
						nbytes = read(fd[((pipe_count/2)*tree_index)+prev_node][0], dest_buffer, sizeof(dest_buffer));
						close(fd[((pipe_count/2)*tree_index)+prev_node][0]);
					}else{
						nbytes = read(fd[((pipe_count/2)*tree_index)+parent_index][0], dest_buffer, sizeof(dest_buffer));
						close(fd[((pipe_count/2)*tree_index)+parent_index][0]);
					}
					printf("Transmission complete at %d of Tree %d with %s\n",parent_index,tree_index,dest_buffer);fflush(stdout);
					break;
				}
			}
		}else{
			break;
		}
		
	}
	wait(NULL);
	wait(NULL);
	//printf("Exiting %d on %d with PID - %d\n",parent_index, tree_index,getpid());
}