コード例 #1
0
ファイル: map_ai_hm.c プロジェクト: usrshare/ltest
int find_closest_on_hm_with_path(struct t_map* map, uint8_t sx, uint8_t sy, uint8_t* heatmap, uint8_t* viewarr, uint8_t* o_x, uint8_t* o_y) {

    uint16_t temp_patharr [ HEATMAP_SIZE ];
    memset(temp_patharr, 255, sizeof temp_patharr);

    temp_patharr [sy * MAP_WIDTH + sx] = 0;

    struct pqueue_t* pq = pq_create();

    pq_push(pq, (void*)((size_t)sy * MAP_WIDTH + sx + 1),0); // create a queue, set the source area to zero.

    //assumes the rest is already filled with 0xFFFF

    while (pq_size(pq)) { // while not empty

	int yx = (int)(size_t)(pq_pop(pq,NULL)) - 1;
	int x = (yx % MAP_WIDTH);
	int y = (yx / MAP_WIDTH);

	if (heatmap[yx]) { pq_destroy(pq); *o_y = y; *o_x = x; return 0; }

	int dir=0; //x and y now contain element with lowest priority

	for (dir=0; dir < MD_COUNT; dir++) { // for all neighbors

	    int nx = x + movediff[dir][0];
	    int ny = y + movediff[dir][1];

	    if (!vtile(nx,ny)) continue; //this should be a valid tile!

	    int cost = getcost(map,NULL,nx,ny,viewarr);
	    if (cost == -1) continue;

	    if (dir % 2) {
		cost = (cost * 3) / 2;
	    }

	    int alt = temp_patharr[y * MAP_WIDTH + x] + cost;
	    if (alt < temp_patharr[ny * MAP_WIDTH + nx]) {

		temp_patharr[ny * MAP_WIDTH + nx] = alt;

		pq_push (pq, (void*) (ny * MAP_WIDTH + nx + 1), alt);			
	    }

	}
    }

    pq_destroy(pq);

    *o_x = 255;
    *o_y = 255;

    return 0;	
}
コード例 #2
0
ファイル: test_pqueue.c プロジェクト: andylamp/c-primitives
/* our main stub */
int 
main(int argc, char **argv) {

  int i = 0, 
      p_tmp = 0;
  const char   *res = NULL, 
               *strdat[] = {
                     "T1", 
                     "T2", 
                     "T3", 
                     "T5", 
                     "T6",
                     "T3" };
  int strprio[] = { 2, 10, 3, 9, 9, 2 };
 

   pq_ptr pq = NULL;

   pq = pq_create(2*PQ_MIN_SZ);

  /* push all 5 tasks into q */
  for (i = 0; i < 6; i++)
    pq_push(pq, (char *)strdat[i], strprio[i], FALSE);
    
  pq_decrease_priority(pq, (char *)strdat[2], 0);
  /* pop them and print them */ 
  for(i = 0; i < 6; i++) {
     res = pq_pop(pq, &p_tmp);   
     printf("Element: %s with priority: %d\n", res, p_tmp);
   }
   /* clear the queue */
   pq_destroy(pq);

   return(EXIT_SUCCESS);
}
コード例 #3
0
ファイル: ssh2connection-server.c プロジェクト: gdh1995/putty
void ssh2channel_send_exit_status(SshChannel *sc, int status)
{
    struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
    struct ssh2_connection_state *s = c->connlayer;

    PktOut *pktout = ssh2_chanreq_init(c, "exit-status", NULL, NULL);
    put_uint32(pktout, status);

    pq_push(s->ppl.out_pq, pktout);
}
コード例 #4
0
ファイル: a.cpp プロジェクト: iamslash/dsalgorithm
int main()
{
  // memset(pqueue, 0LL, sizeof(pqueue));
  for (int i = 0; i < 3; ++i) {
    pqueue[i] = 0;
  }
  
  pq_push(0);
  pq_push(1);
  printf("popped %4d\n", pq_pop());
  printf("popped %4d\n", pq_pop());
  printf("popped %4d\n", pq_pop());
  pq_print();
  
  // // test for __builtin_ctzll
  // printf("%d\n", __builtin_ctzll(0LL)); // 64
  // printf("%d\n", __builtin_ctzll(1LL)); // 0
  // printf("%d\n", __builtin_ctzll(8LL)); // 3
}
コード例 #5
0
ファイル: main.c プロジェクト: chocolatemelt/OPSYS15F
/**
 * lists processes in the form [Q ... ]
 * dirty function
 */
void list_proc(priority_queue pq) {
	printf("[Q");
	int i, sz = pq_size(pq);
	process *procs[sz];
	int pris[sz];
	for(i = 0; i < sz; ++i) {
		procs[i] = pq_pop(pq, &pris[i]);
		printf(" %c", procs[i]->num);
	}
	for(i = 0; i < sz; ++i)
		pq_push(pq, procs[i], pris[i]);
	printf("]\n");
}
コード例 #6
0
ファイル: k_process.c プロジェクト: kimyousee/SE350_RTX
/*@brief: switch out old pcb (p_pcb_old), run the new pcb (gp_current_process)
 *@param: p_pcb_old, the old pcb that was in RUN
 *@return: RTX_OK upon success
 *         RTX_ERR upon failure
 *PRE:  p_pcb_old and gp_current_process are pointing to valid PCBs.
 *POST: if gp_current_process was NULL, then it gets set to pcbs[0].
 *      No other effect on other global variables.
 */
int process_switch(PCB *p_pcb_old) 
{
	PROC_STATE_E state;
	#ifdef DEBUG_0 
		//printf("switching from process %d to process %d\n\r", p_pcb_old->m_pid, gp_current_process->m_pid);
	#endif /* ! DEBUG_0 */
	state = gp_current_process->m_state;

	if (state == NEW) {
		if (gp_current_process != p_pcb_old && p_pcb_old->m_state != NEW) {
			p_pcb_old->mp_sp = (U32 *) __get_MSP();
			if (p_pcb_old->m_state != BLK && p_pcb_old->m_state != BLK_RCV) {
				p_pcb_old->m_state = RDY;
				pq_push(ready_queue, p_pcb_old);
			}
		}
		gp_current_process->m_state = RUN;
		__set_MSP((U32) gp_current_process->mp_sp);
		__rte();  // pop exception stack frame from the stack for a new processes
	} 
	
	/* The following will only execute if the if block above is FALSE */

	if (gp_current_process != p_pcb_old) {
		if (state == RDY){
			p_pcb_old->mp_sp = (U32 *) __get_MSP(); // save the old process's sp
			if (p_pcb_old->m_state != BLK && p_pcb_old->m_state != BLK_RCV) {
				p_pcb_old->m_state = RDY;
				pq_push(ready_queue, p_pcb_old);
			}
			gp_current_process->m_state = RUN;
			__set_MSP((U32) gp_current_process->mp_sp); //switch to the new proc's stack    
		} else {
			gp_current_process = p_pcb_old; // revert back to the old proc on error
			return RTX_ERR;
		} 
	}
	return RTX_OK;
}
コード例 #7
0
ファイル: ssh2connection-server.c プロジェクト: gdh1995/putty
void ssh2channel_send_exit_signal_numeric(
    SshChannel *sc, int signum, bool core_dumped, ptrlen msg)
{
    struct ssh2_channel *c = container_of(sc, struct ssh2_channel, sc);
    struct ssh2_connection_state *s = c->connlayer;

    PktOut *pktout = ssh2_chanreq_init(c, "exit-signal", NULL, NULL);
    put_uint32(pktout, signum);
    put_bool(pktout, core_dumped);
    put_stringpl(pktout, msg);
    put_stringz(pktout, "");           /* language tag */

    pq_push(s->ppl.out_pq, pktout);
}
コード例 #8
0
ファイル: r_utils.c プロジェクト: BusProject/theballot
/*
 *  call-seq:
 *     pq.insert(elem) -> self
 *     pq << elem -> self
 *  
 *  Insert an element into a queue. It will be inserted into the correct
 *  position in the queue according to its priority.
 */
static VALUE
frt_pq_insert(VALUE self, VALUE elem)
{
    PriQ *pq;
    GET_PQ(pq, self);
    if (pq->size < pq->capa) {
        pq_push(pq, elem);
    }
    else if (pq->size > 0 && frt_pq_lt(pq->proc, pq->heap[1], elem)) {
        pq->heap[1] = elem;
        pq_down(pq);
    }
    /* else ignore the element */
    return self;
}
コード例 #9
0
ファイル: SimulationEngine.c プロジェクト: nabilabd/monitorMP
// schedule the event at time-stamp, and provide a callback to its handler
void schedule(double timestamp, void* eventData) {
    
    SimEvent *se = (SimEvent *) malloc(sizeof(SimEvent));
    if (se == NULL) FatalError("schedule", "Could not allocate memory.");
    
    se->data =eventData;
    se->timestamp = timestamp;
    
    // if we have not yet initialize a priority queue, create one
    if (FEL == NULL)
        FEL = pq_create();
    
    // pass the simulation event into the queue with priority equal to timestamp
    pq_push(FEL, timestamp, se);
    
}
コード例 #10
0
ファイル: ssh2connection-server.c プロジェクト: gdh1995/putty
SshChannel *ssh2_serverside_agent_open(ConnectionLayer *cl, Channel *chan)
{
    struct ssh2_connection_state *s =
        container_of(cl, struct ssh2_connection_state, cl);
    PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
    struct ssh2_channel *c = snew(struct ssh2_channel);
    PktOut *pktout;

    c->connlayer = s;
    ssh2_channel_init(c);
    c->halfopen = true;
    c->chan = chan;

    ppl_logevent("Forwarding SSH agent to client");

    pktout = ssh2_chanopen_init(c, "*****@*****.**");
    pq_push(s->ppl.out_pq, pktout);

    return &c->sc;
}
コード例 #11
0
ファイル: ssh2connection-server.c プロジェクト: gdh1995/putty
SshChannel *ssh2_serverside_x11_open(
    ConnectionLayer *cl, Channel *chan, const SocketPeerInfo *pi)
{
    struct ssh2_connection_state *s =
        container_of(cl, struct ssh2_connection_state, cl);
    PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
    struct ssh2_channel *c = snew(struct ssh2_channel);
    PktOut *pktout;

    c->connlayer = s;
    ssh2_channel_init(c);
    c->halfopen = true;
    c->chan = chan;

    ppl_logevent("Forwarding X11 channel to client");

    pktout = ssh2_chanopen_init(c, "x11");
    put_stringz(pktout, (pi && pi->addr_text ? pi->addr_text : "0.0.0.0"));
    put_uint32(pktout, (pi && pi->port >= 0 ? pi->port : 0));
    pq_push(s->ppl.out_pq, pktout);

    return &c->sc;
}
コード例 #12
0
/*************************************************************
 * Support Functionality
 *************************************************************/
static int compute_shortest_path_dijkstra(netloc_data_collection_handle_t *handle,
                                          netloc_node_t *src_node,
                                          netloc_node_t *dest_node,
                                          int *num_edges,
                                          netloc_edge_t ***edges)
{
    int exit_status = NETLOC_SUCCESS;
    int i;
    pq_queue_t *queue = NULL;
    int *distance = NULL;
    bool *not_seen = NULL;
    netloc_node_t *node_u = NULL;
    netloc_node_t *node_v = NULL;
    netloc_node_t **prev_node = NULL;
    netloc_edge_t **prev_edge = NULL;
    int alt;
    int idx_u, idx_v;

    int num_rev_edges;
    netloc_edge_t **rev_edges = NULL;

    struct netloc_dt_lookup_table_iterator *hti = NULL;
    netloc_node_t *cur_node = NULL;

    unsigned long key_int;

    // Just in case things go poorly below
    (*num_edges) = 0;
    (*edges) = NULL;


    /*
     * Allocate some data structures
     */
    queue = pq_queue_t_construct();
    if( NULL == queue ) {
        fprintf(stderr, "Error: Failed to allocate the queue\n");
        exit_status = NETLOC_ERROR;
        goto cleanup;
    }

    distance = (int*)malloc(sizeof(int) * netloc_lookup_table_size(handle->node_list));
    if( NULL == distance ) {
        fprintf(stderr, "Error: Failed to allocate the distance array\n");
        exit_status = NETLOC_ERROR;
        goto cleanup;
    }

    not_seen = (bool*)malloc(sizeof(bool) * netloc_lookup_table_size(handle->node_list));
    if( NULL == not_seen ) {
        fprintf(stderr, "Error: Failed to allocate the 'not_seen' array\n");
        exit_status = NETLOC_ERROR;
        goto cleanup;
    }

    prev_node = (netloc_node_t**)malloc(sizeof(netloc_node_t*) * netloc_lookup_table_size(handle->node_list));
    if( NULL == prev_node ) {
        fprintf(stderr, "Error: Failed to allocate the 'prev_node' array\n");
        exit_status = NETLOC_ERROR;
        goto cleanup;
    }

    prev_edge = (netloc_edge_t**)malloc(sizeof(netloc_edge_t*) * netloc_lookup_table_size(handle->node_list));
    if( NULL == prev_edge ) {
        fprintf(stderr, "Error: Failed to allocate the 'prev_edge' array\n");
        exit_status = NETLOC_ERROR;
        goto cleanup;
    }

    /*
     * Initialize the data structures
     */
    // Make sure to initialize the arrays
    for( i = 0; i < netloc_lookup_table_size(handle->node_list); ++i){
        distance[i] = INT_MAX;
        not_seen[i] = true;
        prev_node[i] = NULL;
        prev_edge[i] = NULL;
    }

    i = 0;
    hti = netloc_dt_lookup_table_iterator_t_construct(handle->node_list);
    while( !netloc_lookup_table_iterator_at_end(hti) ) {
        cur_node = (netloc_node_t*)netloc_lookup_table_iterator_next_entry(hti);
        if( NULL == cur_node ) {
            break;
        }

        if( cur_node == src_node ) {
            pq_push(queue, 0, cur_node);
            distance[i] = 0;
        } else {
            pq_push(queue, INT_MAX, cur_node);
            distance[i] = INT_MAX;
        }

        not_seen[i] = true;

        prev_node[i] = NULL;
        prev_edge[i] = NULL;

        cur_node->__uid__ = i;
        ++i;
    }

    /*
     * Search
     */
    while( !pq_is_empty(queue) ) {
        //pq_dump(queue);

        // Grab the next hop
        node_u = pq_pop(queue);
        // Mark as seen
        idx_u = -1;
        i = 0;
        idx_u = node_u->__uid__;
        not_seen[idx_u] = false;

        // For all the edges from this node
        for(i = 0; i < node_u->num_edges; ++i ) {
            node_v = NULL;
            idx_v  = -1;

            // Lookup the "dest" node
            node_v = node_u->edges[i]->dest_node;
            idx_v = node_v->__uid__;

            // If the node has been seen, skip
            if( !not_seen[idx_v] ) {
                continue;
            }

            // Otherwise check to see if we found a shorter path
            // Future Work: Add a weight factor other than 1.
            //              Maybe calculated based on speed/width
            alt = distance[idx_u] + 1;
            if( alt < distance[idx_v] ) {
                distance[idx_v] = alt;
                prev_node[idx_v] = node_u;
                prev_edge[idx_v] = node_u->edges[i];

                // Adjust the priority queue as needed
                pq_reorder(queue, alt, node_v);
            }
        }
    }

    /*
     * Reconstruct the path by picking up the edges
     * The edges will be in reverse order (dest to source).
     */
    num_rev_edges = 0;
    rev_edges     = NULL;

    // Find last hop
    SUPPORT_CONVERT_ADDR_TO_INT(dest_node->physical_id,
                                handle->network->network_type,
                                key_int);
    node_u  = netloc_lookup_table_access_with_int( handle->node_list,
                                                   dest_node->physical_id,
                                                   key_int);
    idx_u = node_u->__uid__;

    node_v = NULL;
    idx_v  = -1;
    while( prev_node[idx_u] != NULL ) {
        // Find the linking edge
        if( node_u != dest_node) {
            for(i = 0; i < node_u->num_edges; ++i ) {
                if( node_v->physical_id_int == node_u->edges[i]->dest_node->physical_id_int ) {
                    ++num_rev_edges;
                    rev_edges = (netloc_edge_t**)realloc(rev_edges, sizeof(netloc_edge_t*) * num_rev_edges);
                    if( NULL == rev_edges ) {
                        fprintf(stderr, "Error: Failed to re-allocate the 'rev_edges' array with %d elements\n",
                                num_rev_edges);
                        exit_status = NETLOC_ERROR;
                        goto cleanup;
                    }
                    rev_edges[num_rev_edges-1] = node_u->edges[i];
                    break;
                }
            }
        }
        node_v = node_u;
        idx_v  = idx_u;

        // Find the next node
        SUPPORT_CONVERT_ADDR_TO_INT(prev_node[idx_u]->physical_id,
                                    handle->network->network_type,
                                    key_int);
        node_u = netloc_lookup_table_access_with_int( handle->node_list,
                                                      prev_node[idx_u]->physical_id,
                                                      key_int);
        idx_u = node_u->__uid__;
    }

    for(i = 0; i < src_node->num_edges; ++i ) {
        if( NULL == node_v ) {
            fprintf(stderr, "Error: This should never happen, but node_v is NULL at line %d in file %s\n",
                    __LINE__, __FILE__);
            exit_status = NETLOC_ERROR;
            goto cleanup;
        }
        if( node_v->physical_id_int == src_node->edges[i]->dest_node->physical_id_int ) {
            ++num_rev_edges;
            rev_edges = (netloc_edge_t**)realloc(rev_edges, sizeof(netloc_edge_t*) * num_rev_edges);
            if( NULL == rev_edges ) {
                fprintf(stderr, "Error: Failed to re-allocate the 'rev_edges' array with %d elements\n",
                        num_rev_edges);
                exit_status = NETLOC_ERROR;
                goto cleanup;
            }
            rev_edges[num_rev_edges-1] = node_u->edges[i];

            break;
        }
    }


    /*
     * Copy the edges back in correct order
     */
    (*num_edges) = num_rev_edges;
    (*edges) = (netloc_edge_t**)malloc(sizeof(netloc_edge_t*) * (*num_edges));
    if( NULL == (*edges) ) {
        fprintf(stderr, "Error: Failed to allocate the edges array\n");
        exit_status = NETLOC_ERROR;
        goto cleanup;
    }

    for( i = 0; i < num_rev_edges; ++i ) {
        (*edges)[i] = rev_edges[num_rev_edges-1-i];
        //printf("DEBUG: \t Edge: %s\n", netloc_pretty_print_edge_t( (*edges)[i] ) );
    }


    /*
     * Cleanup
     */
 cleanup:
    if( NULL != queue ) {
        pq_queue_t_destruct(queue);
        queue = NULL;
    }

    if( NULL != rev_edges ) {
        free(rev_edges);
        rev_edges = NULL;
    }

    if( NULL != distance ) {
        free(distance);
        distance = NULL;
    }

    if( NULL != not_seen ) {
        free(not_seen);
        not_seen = NULL;
    }

    if( NULL != prev_node ) {
        free(prev_node);
        prev_node = NULL;
    }

    if( NULL != prev_edge ) {
        free(prev_edge);
        prev_edge = NULL;
    }

    netloc_dt_lookup_table_iterator_t_destruct(hti);

    return exit_status;
}
コード例 #13
0
ファイル: main.c プロジェクト: chocolatemelt/OPSYS15F
/**
 * rr simulation
 */
void s_rr(priority_queue q, v_memory vm, int num, int cs_t, float *a_wait_t, int *total_cs, int *total_t, int *total_d, int t_memmove, int t_slice) {
	int cur_t = 0;             // current time
	int cur_cs_t = cs_t + 1;   // current context switch time (-1 when not switching)
	int cur_s_t = 0;           // current time-slice time
	int in_use = 0;            // boolean for processor usage
	int procs_t[num];          // processor countdowns
	process *procs[num];       // processes in use
	process *tmp;              // process to pop from waiting queue
	int active;                // active processor in use
	int pri = 0;               // rr priority (fcfs)
	char *algo;                // fitting algorithm
	priority_queue pq;         // processes queue

	// zero arrays
	int i;
	for(i = 0; i < num; ++i) {
		procs_t[i] = 0;
		procs[i] = 0;
	}

	// initialize stuff
	pq = pq_new(0);

	// print vm algorithm
	switch(vm.algo) {
		case 'f':
			algo = "First-Fit";
			break;
		case 'n':
			algo = "Next-Fit";
			break;
		case 'b':
			algo = "Best-Fit";
			break;
		default:
			break;
	}
	printf("time 0ms: Simulator started for RR (t_slice %d) and %s\n", t_slice, algo);

	while(1) {
		// check process arrival times
		tmp = NULL;
		while(tmp == NULL) {
			tmp = pq_peek(q, NULL);
			if(tmp && tmp->a_t == cur_t) {
				int rc;

				// add process to system
				tmp = pq_pop(q, NULL);
				pq_push(pq, tmp, ++pri);
				rc = vm_add(&vm, *tmp);
				if(rc != 1) { // (defrag)
					event_call("", cur_t, tmp->num, 'a', NULL);
					event_call("", cur_t, 0, 'b', NULL);
					event_call("", cur_t, 0, '9', NULL);
					vm_print(vm);
					*total_d += vm_defrag(&vm, 10);
					event_call("", cur_t, 0, 'c', NULL);
					event_call("", cur_t, 0, '9', NULL);
					vm_print(vm);
					vm_add(&vm, *tmp);
				}

				// print
				event_call("", cur_t, tmp->num, '8', pq);
				event_call("", cur_t, 0, '9', NULL);
				vm_print(vm);
				tmp = NULL;
			} else {
				tmp = NULL;
				break;
			}
		}

		// switching context
		if(cur_cs_t > 0) --cur_cs_t;

		// switched context
		if(cur_cs_t == 0) {
			--cur_cs_t;

			// check if open process available and run it if possible
			if((pq_size(pq) != 0) && !in_use) {
				int procs_u = 0;
				while(procs[procs_u] != 0) ++procs_u; // get next open index
				in_use = 1;
				procs[procs_u] = pq_pop(pq, NULL); // pop the next open process
				change_status(procs[procs_u], 1);  // set status to using cpu
				procs_t[procs_u] = procs[procs_u]->b_t + 1; // start process timer
				cur_s_t = t_slice + 1; // start t_slice timer
				if(procs[procs_u]->cur_t > 0) procs_t[procs_u] = procs[procs_u]->cur_t;
				active = procs_u;
				--procs[procs_u]->b_n;
				event_call("", cur_t, procs[procs_u]->num, '1', pq);
			}
		}

		// process timers
		for(i = 0; i < num; ++i) {
			if(procs[i] != 0) {
				// check timers
				if(procs_t[i] > 0) {
					--procs_t[i];
					if(cur_s_t > 0) --cur_s_t;

					// handle timers
					if(procs_t[i] == 0) {

						// completed cpu burst
						if(procs[i]->status == 1) {
							change_status(procs[i], 2);
							procs_t[i] = procs[i]->io_t;
							if(procs[i]->cur_t > 0) procs[i]->cur_t = 0; // reset cur_t
							in_use = 0;
							cur_cs_t = cs_t;
							++*total_cs;

							// if process does not require i/o
							if(procs_t[i] == 0) {
								if(procs[i]->b_n <= 0) {
									vm_del(&vm, procs[i]->num);
									event_call("", cur_t, procs[i]->num, '5', pq);
									event_call("", cur_t, procs[i]->num, 'd', NULL);
									event_call("", cur_t, procs[i]->num, '9', NULL);
									vm_print(vm);
									change_status(procs[i], 3); // terminate
								}
								else {
									change_status(procs[i], 0);
									pq_push(pq, procs[i], ++pri);
									event_call("", cur_t, procs[i]->num, '2', pq);
									procs[i] = 0;
								}
							}
							else {
								if(procs[i]->b_n != 0) {
									event_call("", cur_t, procs[i]->num, '2', pq);
									event_call("", cur_t, procs[i]->num, '3', pq);
								}
								// terminate process
								else {
									procs_t[i] = 0;
									change_status(procs[i], 3);
									vm_del(&vm, procs[i]->num);
									event_call("", cur_t, procs[i]->num, '5', pq);
									event_call("", cur_t, procs[i]->num, 'd', NULL);
									event_call("", cur_t, procs[i]->num, '9', NULL);
									vm_print(vm);
								}
							}
						}

						// completed i/o
						else if(procs[i]->status == 2) {
							change_status(procs[i], 0);
							if(!in_use) cur_cs_t = cs_t;

							// finished burst
							if(procs[i]->b_n <= 0) {
								// terminate on last burst
								if(pq_size(pq) != 0) {
									vm_del(&vm, procs[i]->num);
									event_call("", cur_t, procs[i]->num, '5', pq);
									event_call("", cur_t, procs[i]->num, 'd', NULL);
									event_call("", cur_t, procs[i]->num, '9', NULL);
									vm_print(vm);
								}
								change_status(procs[i], 3);
							}
							else {
								pq_push(pq, procs[i], ++pri);
								event_call("", cur_t, procs[i]->num, '4', pq);
								procs[i] = 0;
							}
						}
					}

					// check time slice expiration
					else if(cur_s_t == 0 && procs[i]->status == 1) {
						if(pq_size(pq) != 0) {
							procs[active]->cur_t = procs_t[active]; // store the current processing time
							++procs[active]->b_n; // restore burst number since it hasnt finished
							change_status(procs[active], 0); // reset status
							pq_push(pq, procs[active], ++pri); // push back into queue
							preempt_call(cur_t, procs[active]->num, '0', pq); // print
							cur_cs_t = cs_t; // switch contexts
							++*total_cs;

							// void existing processors
							procs_t[active] = 0;
							procs[active] = 0;
							in_use = 0;
							cur_s_t = -1;
						}
					}
				}
			}
		}

		// get statistics from all processes
		for(i = 1; i <= pq_size(pq); ++i) {
			if(((process*)(pq->buf[i].data))->status == 0) {
				++*a_wait_t;
			}
		}

		if(pq_size(pq) == 0 && !in_use && done(procs_t, sizeof(procs_t) / sizeof(int))) {
			*total_t = cur_t;
			event_call("RR", cur_t, 0, '7', pq);
			return;
		}

		++cur_t;
	}
}
コード例 #14
0
ファイル: main.c プロジェクト: chocolatemelt/OPSYS15F
int main(int argc, char **argv) {
	/* SIMULATION VARIABLES */
	int n = 0;      // processes
	int cs_t = 13;  // context switch time (in ms)
	FILE *inf;      // input file (processes.txt)
	FILE *outf;   // output file (simout.txt)

	// recording variables
	float a_cpu_t = 0.0;  // average cpu burst time
	float a_turn_t = 0.0; // average turnaround time
	float a_wait_t = 0.0; // average wait time
	int total_cs = 0;     // total context switches
	int total_burst = 0;  // total cpu bursts
	int total_mem = 256;  // total virtual memory
	int total_t = 0;      // total time
	int total_d = 0;      // total defrag time
	int t_slice = 80;     // round robin time slice
	int t_memmove = 10;   // defragmentation memmove time

	if(argc != 1) {
		printf("There are no arguments associated with this simulation. Input is read from processes.txt.\n");
		return 1;
	}

	// priority queues and process
	priority_queue srtf = pq_new(n), srtn = pq_new(n), srtb = pq_new(n), rrf = pq_new(n), rrn = pq_new(n), rrb = pq_new(n);
	process *p1, *p2, *p3, *p4, *p5, *p6;

	/* read file */
	if((inf = fopen("processes.txt", "r")) == NULL) {
		perror("open() failed");
		return EXIT_FAILURE;
	}

	if((outf = fopen("simout.txt", "w")) == NULL) {
		perror("fopen() failed");
		return EXIT_FAILURE;
	}

	// get current line
	char *line = NULL;
	size_t nbytes = 0;

	while(getline(&line, &nbytes, inf) != -1) {
		// trim the string first
		char *newline;
		newline = trim(line);

		if(*newline != 0) {
			// get values
			char pn;
			int at, bt, bn, it, mem;
			sscanf(newline, "%c|%i|%i|%i|%i|%i", &pn, &at, &bt, &bn, &it, &mem);

			// allocate and init
			p1 = malloc(sizeof(process));
			p2 = malloc(sizeof(process));
			p3 = malloc(sizeof(process));
			p4 = malloc(sizeof(process));
			p5 = malloc(sizeof(process));
			p6 = malloc(sizeof(process));
			*p1 = proc_new(pn, at, bt, bn, it, 0, mem);
			*p2 = *p1;
			*p3 = *p1;
			*p4 = *p1;
			*p5 = *p1;
			*p6 = *p1;

			// calculate burst statistics
			a_cpu_t += bt * bn;
			total_burst += bn;

			// push to queues
			++n;
			pq_push(srtf, p1, at);
			pq_push(srtn, p2, at);
			pq_push(srtb, p3, at);
			pq_push(rrf, p2, at);
			pq_push(rrn, p5, at);
			pq_push(rrb, p6, at);
		}
	}

	// free memory
	free(line);

	// create virtual memory
	v_memory vm_ff = vm_new(total_mem, 'f');
	v_memory vm_nf = vm_new(total_mem, 'n');
	v_memory vm_bf = vm_new(total_mem, 'b');

	/* ===begin simulations=== */
	a_cpu_t /= total_burst;
	s_srt(srtf, vm_ff, n, cs_t, &a_wait_t, &total_cs, &total_t, &total_d, t_memmove);
	simout("SRT", "First-Fit", outf, &a_cpu_t, &a_turn_t, &a_wait_t, &total_cs, total_burst, total_t, total_d);
	printf("\n\n");
	s_srt(srtn, vm_nf, n, cs_t, &a_wait_t, &total_cs, &total_t, &total_d, t_memmove);
	simout("SRT", "First-Fit", outf, &a_cpu_t, &a_turn_t, &a_wait_t, &total_cs, total_burst, total_t, total_d);
	printf("\n\n");
	s_srt(srtb, vm_bf, n, cs_t, &a_wait_t, &total_cs, &total_t, &total_d, t_memmove);
	simout("SRT", "First-Fit", outf, &a_cpu_t, &a_turn_t, &a_wait_t, &total_cs, total_burst, total_t, total_d);
	printf("\n\n");
	s_rr(rrf, vm_ff, n, cs_t, &a_wait_t, &total_cs, &total_t, &total_d, t_memmove, t_slice);
	simout("RR", "First-Fit", outf, &a_cpu_t, &a_turn_t, &a_wait_t, &total_cs, total_burst, total_t, total_d);
	printf("\n\n");
	s_rr(rrn, vm_nf, n, cs_t, &a_wait_t, &total_cs, &total_t, &total_d, t_memmove, t_slice);
	simout("RR", "First-Fit", outf, &a_cpu_t, &a_turn_t, &a_wait_t, &total_cs, total_burst, total_t, total_d);
	printf("\n\n");
	s_rr(rrb, vm_bf, n, cs_t, &a_wait_t, &total_cs, &total_t, &total_d, t_memmove, t_slice);
	simout("RR", "First-Fit", outf, &a_cpu_t, &a_turn_t, &a_wait_t, &total_cs, total_burst, total_t, total_d);

	// close streams
	fclose(inf);
	fclose(outf);

	// free memory and exit gracefully
	vm_free(vm_ff);
	vm_free(vm_nf);
	vm_free(vm_bf);
	pq_free(srtf);
	pq_free(srtn);
	pq_free(srtb);
	pq_free(rrf);
	pq_free(rrn);
	pq_free(rrb);
	free(srtf);
	free(srtn);
	free(srtb);
	free(rrf);
	free(rrn);
	free(rrb);
	return EXIT_SUCCESS;
}
コード例 #15
0
ファイル: k_process.c プロジェクト: kimyousee/SE350_RTX
/**
 * @biref: initialize all processes in the system
 * NOTE: We assume there are only two user processes in the system in this example.
 */
void process_init() 
{
	int i;
	int j;
	U32 *sp;
	timeout_queue = NULL;
        /* fill out the initialization table */
	set_test_procs();
	set_stress_test_procs();
	set_user_procs();
	set_system_procs();
	j = 0;
	// User processes
	for ( i = 0; i < NUM_TEST_PROCS; i++ ) {
		addProcTable(j, g_test_procs[i].m_pid, g_test_procs[i].m_stack_size, 
			g_test_procs[i].m_priority, g_test_procs[i].mpf_start_pc);
		j++;
	}
	
	for ( i = 0; i < NUM_USER_PROCS; i++ ) {
		addProcTable(j, g_user_procs[i].m_pid, g_user_procs[i].m_stack_size, 
			g_user_procs[i].m_priority, g_user_procs[i].mpf_start_pc);
		j++;
	}
	
	// Stress test processes
	for ( i = 0; i < NUM_STRESS_TEST_PROCS; i++ ) {
		addProcTable(j, g_stress_test_procs[i].m_pid, g_stress_test_procs[i].m_stack_size, 
			g_stress_test_procs[i].m_priority, g_stress_test_procs[i].mpf_start_pc);
		j++;
	}
	
	// KCD and CRT processes
	for ( i = 0; i < NUM_SYSTEM_PROCS; i++ ) {
		addProcTable(j, g_system_procs[i].m_pid, g_system_procs[i].m_stack_size, 
			g_system_procs[i].m_priority, g_system_procs[i].mpf_start_pc);
		j++;
	}
	
	// NULL process
	addProcTable(j, 0, SYS_SZ_STACK, LOWEST+1, &nullProc);
	j++;
	
	// UART I Process
	addProcTable(j, PID_UART_IPROC, SYS_SZ_STACK, LOWEST+1, &UART_i_Proc);
	j++;
	
	// Timer I Process
	addProcTable(j, PID_TIMER_IPROC, SYS_SZ_STACK, LOWEST+1, &Timer_i_Proc);
  j++;
	
	/* initilize exception stack frame (i.e. initial context) for each process */
	for ( i = 0; i < TOTAL_PROCS; i++ ) {
		int j;
		(gp_pcbs[i])->m_pid = (g_proc_table[i]).m_pid;
		//printf("pid = %d, start = %d\n", (g_proc_table[i]).m_pid, (g_proc_table[i]).mpf_start_pc);
		(gp_pcbs[i])->m_state = NEW;
		(gp_pcbs[i])->m_priority = (g_proc_table[i]).m_priority;
		
		sp = alloc_stack((g_proc_table[i]).m_stack_size);
		*(--sp)  = INITIAL_xPSR;      // user process initial xPSR  
		*(--sp)  = (U32)((g_proc_table[i]).mpf_start_pc); // PC contains the entry point of the process
		for ( j = 0; j < 6; j++ ) { // R0-R3, R12 are cleared with 0
			*(--sp) = 0x0;
		}
		#ifdef DEBUG_0
		printf("stack = 0x%x\n\r", sp);
		#endif
		
		if (!isIProcess(gp_pcbs[i]->m_pid)) {
			pq_push(ready_queue, gp_pcbs[i]);
		}
		
		(gp_pcbs[i])->mp_sp = sp;
	}
}