Beispiel #1
0
output_que()
{
	int choice;
	while(1)
	{
		printf("1.Insert at right\n");
		printf("2.Insert at left\n");
		printf("3.Delete from left\n");
		printf("4.Display\n");
		printf("5.Quit\n");
		printf("Enter your choice : ");
		scanf("%d",&choice);

		switch(choice)
		{
		 case 1:
			insert_right();
			break;
		 case 2:
			insert_left();
			break;
		 case 3:
			delete_left();
			break;
		 case 4:
			display_queue();
			break;
		 case 5:
			exit();
		 default:
			printf("Wrong choice\n");
		}/*End of switch*/
	}/*End of while*/
}/*End of output_que() */
int main()
{
  int option;
  printf("\n");
  printf(">>> c program to implement queue operations <<<");
  do
  {
   printf("\n\n 1.Insert an element");
   printf("\n 2.Delete an element");
   printf("\n 3.Display queue");
   printf("\n 4.Exit");
   printf("\n Enter your choice: ");
   scanf("%d",&option);
   switch(option)
   {
     case 1: insert_element();
              break;
     case 2: delete_element();
             break;
     case 3: display_queue();
             break;
     case 4: return 0;
   }
 
  }while(option!=4);
}
void main()
{
    int choice, n, flag = 0;
    char ch;
 
    for (int i = 0; i < 100; i++)
    {
        printf("MENU\n");
        printf("Enter 1 to INSERT an element in the queue\n");
        printf("Enter 2 to DELETE an element in the queue\n");
        printf("Enter 3 to DISPLAY the elements of the queue\n");
        printf("Enter 4 to CHECK if the queue is EMPTY\n");
        printf("Enter 5 to KNOW the FIRST element of the queue\n");
        printf("Enter 6 to KNOW the queue SIZE\n");
        printf("Enter 7 to Destroy the Queue\n");
        printf("Enter 8 to EXIT the program\n");
        printf("Enter your Choice:");
        choice = i % 7 + 1;
        switch(choice)
        {
        case 1: 
            insert_rear();
            break;
        case 2: 
            delete_front();
            break;
        case 3: 
            display_queue();
            break;
        case 4: 
            empty_queue();
            break;
        case 5: 
            front_ele();
            break;
        case 6: 
            n = queue_size();
            printf("\nthe queue size is: %d", n);
            break;
        case 7: 
            destroy();
            flag = 1;
            break;
        case 8: 
            exit(0);
            break;
        default: 
            printf("WRONG CHOICE\n");
        }
    } 
    if (flag == 0)
    {
        destroy();
    }
}
int main(void) {
	uart_init();
    q1 = init_robocol_queue(8);
    q2 = init_robocol_queue(8);
    q3 = init_robocol_queue(8);
    enqueue(q1, 5);
    display_queue(q1);
	
	while(1){

	}
	/*
    command* c = init_command("ZAS", "PWD", "");
    display_command(c);
    destroy_command(c);
    display_command(c);
    */
    //float list[] = {1.0, 2.0, 3.0};
    int val = 0;
    //printf("%s\n", format_string("name", list, sizeof(list) / sizeof(list[0])));
    //format_string("name", list, sizeof(list) / sizeof(list[0]), &val);
    printf("%s\n", get_command_response("SEN", &val));
    uart_close();
}
unsigned srtf_scheduler(struct process_node *p)
{
	struct queue *i=NULL,*o=NULL,*d=NULL,*r=NULL;
	struct queue *temp_queue=NULL;
	struct process_node *temp_process=NULL;	
	struct burst_node *b=NULL;
	time_t current_time,start_time,end_time;
	unsigned flag=0;

	time(&start_time);

	r=initialize_ready_queue(p,r,5);
	initialize_page(p,r); //initialize page number for the processes to request first page number

	printf("\nReady Queue Initialized as follows: ");
	display_queue(p,r);
	printf("\n\nInitial Memory Map is as follows: ");
	display_memory_map(p,r);

	do
	{
		temp_queue=r;
		while(temp_queue!=NULL)
		{
			for(temp_process=p;temp_process!=NULL;temp_process=temp_process->next)
			{
				if(temp_queue->pid==temp_process->pid)
					break;
			}
			
			b=temp_queue->burst;

			if(b->next!=NULL)
			{
				//CPU burst
				time(&current_time);
				temp_process->waiting_time+=(current_time-temp_queue->timestamp);

				temp_process->execution_time+=b->burst_duration;

				r=r->next;			//changed ready queue
				temp_queue->timestamp=current_time;
				time(&current_time);
				temp_queue->burst=b->next;
				temp_queue->next=NULL;
				display_task_manager(temp_process->pid,p,i,o,d,r,b->burst_duration);
				sleep(b->burst_duration);
				
				//push node to blocked queue - 3 cases
				switch(b->exit_code)
				{
					case 0:			//input blocked queue
						i=insert_process_at_end_of_queue(i,temp_queue);
						break;
					case 1: 		//output blocked queue
						o=insert_process_at_end_of_queue(o,temp_queue);
						break;
					case 2:			//disk blocked queue
						d=insert_process_at_end_of_queue(d,temp_queue);
						handle_disk_block(p,d,temp_queue->pid, random_number(6));
						display_memory_map(p,r);

						break;
				}
			}
			else
			{
				time(&current_time);
				temp_process->waiting_time+=(current_time-temp_queue->timestamp);
				temp_process->execution_time+=b->burst_duration;
				r=r->next;
				display_task_manager(temp_process->pid,p,i,o,d,r,b->burst_duration);
				sleep(b->burst_duration);
				printf("\n*****************  ");
				printf("PID: %u\tProcess %s  finished execution",temp_queue->pid,temp_process->name);
				printf("  *****************\n");
				sleep(3);
				//free memory
				free(temp_queue);
				temp_queue=NULL;
			}

			r=switch_process_to_ready_queue(p,r,&i,5);
			r=switch_process_to_ready_queue(p,r,&o,5);
			r=switch_process_to_ready_queue(p,r,&d,5);
			temp_queue=r;
		}
		
		display_task_manager(-1,p,i,o,d,r,0);

		if(!isEmpty(i)||!isEmpty(o)||!isEmpty(d)||!isEmpty(r))
			flag=1;
		else
			flag=0;

		if(flag)
		{
			r=switch_process_to_ready_queue(p,r,&i,5);
			r=switch_process_to_ready_queue(p,r,&o,5);
			r=switch_process_to_ready_queue(p,r,&d,5);
		}

	}while(flag);

	time(&end_time);

	return((int)end_time-start_time);
}
main()
{
	int i,v;
	int topo_order[MAX],indeg[MAX];
	int count;

	int j;

	create_graph();
	
	/*Find the indegree of each vertex*/
	for(i=0; i<n; i++)
	{
		indeg[i] = indegree(i);
		if( indeg[i] == 0 )
			insert_queue(i);
	}
	
	for(j=0; j<n; j++)
		printf("In(%d) = %d\t",j, indeg[j]);
	printf("\n");
	display_queue();	

	count = 0;
	while( !isEmpty_queue() && count<n )
	{
		v = delete_queue();
        printf("Delete %d and edges going from it\n",v);
		topo_order[++count] = v; /*Add vertex v to topo_order array*/
		/*Delete all edges going fron vertex v */
		for(i=0; i<n; i++)
		{
			if(adj[v][i]==1)
			{
				adj[v][i] = 0;
				indeg[i] = indeg[i]-1;
				if(indeg[i] == 0)
				{
					printf("Now vertex %d has zero indegree so insert it in the queue\n",i);
					insert_queue(i);
				}
			}
		}
		for(j=0; j<n; j++)
		{
			if(indeg[j]!=0)
				printf("In(%d) = %d\t",j, indeg[j]);
		}
		printf("\n");
		display_queue();
		STOP;
	}

	printf("count = %d\n",count);	
	if( count < n )
	{
		printf("No topological ordering possible, graph contains cycle\n");
		exit(1);
	}
	printf("Vertices in topological order are :\n");
	for(i=1; i<=count; i++)
		printf( "%d ",topo_order[i] );
	printf("\n");
}/*End of main()*/
unsigned round_robin_scheduler(struct process_node *p,unsigned time_quantum)
{
	struct queue *i=NULL,*o=NULL,*d=NULL,*r=NULL;
	struct process_node *temp_process=NULL;	
	struct queue *temp_queue=NULL;
	struct burst_node *b=NULL;
	time_t current_time,start_time,end_time;
	unsigned flag=0;

	time(&start_time);

	r=initialize_ready_queue(p,r,6);
	printf("\nReady Queue initialized as follows:");
	display_queue(p,r);

	do
	{
		temp_queue=r;
		while(temp_queue!=NULL)
		{
			for(temp_process=p;temp_process!=NULL;temp_process=temp_process->next)
				if(temp_queue->pid==temp_process->pid)
					break;
			
			b=temp_queue->burst;

			if(b->next!=NULL)
			{
				//CPU burst
				time(&current_time);
				temp_process->waiting_time+=(current_time-temp_queue->timestamp);
				r=r->next;			//changed ready queue
				
				temp_queue->timestamp=current_time;
				time(&current_time);				
				temp_queue->next=NULL;

				if(b->burst_duration>time_quantum)
				{
					temp_process->execution_time+=time_quantum;

					temp_queue->burst=create_burst_node(b,time_quantum);

					display_task_manager(temp_process->pid,p,i,o,d,r,time_quantum);
					sleep(time_quantum);
					//insert process back into ready queue
					r=insert_in_ready_queue(p,r,temp_queue,6);
				}
				else
				{
					temp_process->execution_time+=b->burst_duration;
					temp_queue->burst=b->next;
					display_task_manager(temp_process->pid,p,i,o,d,r,b->burst_duration);
					sleep(b->burst_duration);
					switch(b->exit_code)
					{
						case 0:			//input blocked queue
							i=insert_process_at_end_of_queue(i,temp_queue);
							break;
						case 1: 		//output blocked queue
							o=insert_process_at_end_of_queue(o,temp_queue);
							break;
						case 2:			//disk blocked queue
							d=insert_process_at_end_of_queue(d,temp_queue);
							break;
					}
				}
			}
			else
			{

				time(&current_time);
				temp_process->waiting_time+=(current_time-temp_queue->timestamp);
				r=r->next;		//changed ready queue

				temp_queue->timestamp=current_time;
				time(&current_time);
				temp_queue->next=NULL;

				if(b->burst_duration>time_quantum)
				{
					temp_process->execution_time+=time_quantum;
					temp_queue->burst=create_burst_node(b,time_quantum);

					display_task_manager(temp_process->pid,p,i,o,d,r,time_quantum);
					sleep(time_quantum);
					//insert process back into ready queue
					r=insert_in_ready_queue(p,r,temp_queue,6);
				}
				else
				{
					temp_process->execution_time+=b->burst_duration;
					display_task_manager(temp_process->pid,p,i,o,d,r,b->burst_duration);
					sleep(b->burst_duration);
					printf("\n*****************  ");
					printf("PID: %u\tProcess %s  finished execution",temp_queue->pid,temp_process->name);
					printf("  *****************\n");
					sleep(3);
					//free memory
					free(temp_queue);
					temp_queue=NULL;
				}
			}

			r=switch_process_to_ready_queue(p,r,&i,6);
			r=switch_process_to_ready_queue(p,r,&o,6);
			r=switch_process_to_ready_queue(p,r,&d,6);
			
			temp_queue=r;
		}
		
		display_task_manager(-1,p,i,o,d,r,0);

		if(!isEmpty(i)||!isEmpty(o)||!isEmpty(d)||!isEmpty(r))
			flag=1;
		else
			flag=0;

		if(flag)
		{
			r=switch_process_to_ready_queue(p,r,&i,6);
			r=switch_process_to_ready_queue(p,r,&o,6);
			r=switch_process_to_ready_queue(p,r,&d,6);
		}

	}while(flag);

	time(&end_time);

	return((int)end_time-start_time);
}
int main()
{
    char buffer[128], z[128];
    int num_nodes;
    struct node *nodes;
    int i, edge;
    char name[128];
    struct item **adj, *ptr;
    struct node *u;
    int source;
    struct entry *queue = NULL;
    
    printf("Enter nuber of nodes: ");
    fgets(buffer, 127, stdin);
    sscanf(buffer, "%d", &num_nodes);
    nodes = (struct node *) malloc(num_nodes * sizeof(struct node));
    
    /*Initialize the vertices*/
    for(i=0; i<num_nodes; ++i)
    {
        nodes[i].id = i+1;
        printf("Enter name of node %d: ", i+1);
        fgets(buffer, 127, stdin);
        sscanf(buffer, "%[^\n]s", name);
        nodes[i].name = (char *) (malloc((strlen(name)+1) * sizeof(char)));
        strcpy(nodes[i].name, name);
        nodes[i].d = LONG_MAX;
        nodes[i].c = 'w';
        nodes[i].p = NULL;
    }
    
    adj = (struct item **)malloc(num_nodes * sizeof(struct item *));
    /*Initialize adjacency list to NULL*/
    for(i=0; i<num_nodes; ++i)
        adj[i] = NULL;
        
    /*Add all the edges to adjacency list. No mechanism implemented to prevent multi-edges or self-loop*/
    for(i=0; i<num_nodes; ++i)
    {
        printf("Enter all edges from %s: (-1 to end)\n", nodes[i].name);
        while(1)
        {
            fgets(buffer, 127, stdin);
            sscanf(buffer, "%s", z);
            if(strcmp(z, "exit") == 0)
                break;
            edge = find_vertex(nodes, num_nodes, z);
            if(edge == -1)
            {
                printf("Vertex %s does not exist\n", z);
                break;
            }
            add_edge(&adj[i], edge);
        }
    }
    
    #ifdef DEBUG
    /*Display all the edges*/
    for(i=0; i<num_nodes; ++i)
        display_edges(adj[i], nodes, i);
    #endif
    
    /*Choose a source vertex*/
    printf("Enter the source vertex: ");
    fgets(buffer, 127, stdin);
    sscanf(buffer, "%s", z);
    source = find_vertex(nodes, num_nodes, z);
    if(source == -1)
    {
        printf("Invalid vertex\n");
        exit(-1);
    }
    #ifdef DEBUG
    printf("***DEBUG MODE ON***\n");
    #endif
    /*Perform a BFS*/
    nodes[source-1].d = 0;
    nodes[source-1].p = NULL;
    nodes[source-1].c = 'g';
    push(&queue, &nodes[source-1]);
    #ifdef DEBUG
    printf("Pushed %s\n", nodes[source-1].name);
    display_queue(queue);
    #endif
    while(!empty(queue))
    {
        u = pop(&queue);
        #ifdef DEBUG
        printf("Popped %s\n", u->name);
        display_queue(queue);
        #endif
        i = u->id;
        ptr = adj[i-1];
        while(ptr != NULL)
        {
            if(nodes[ptr->vertex-1].c == 'w')
            {
                nodes[ptr->vertex-1].c = 'g';
                nodes[ptr->vertex-1].d = u->d + 1;
                nodes[ptr->vertex-1].p = u;
                push(&queue, &nodes[ptr->vertex-1]);
                #ifdef DEBUG
                printf("Pushed %s\n", nodes[ptr->vertex-1].name);
                display_queue(queue);
                #endif
            }
            ptr = ptr->next;
        }
        u->c = 'b';
    }
    
    #ifdef DEBUG
    printf("***Printing paths***\n");
    #endif
    for(i=0; i<num_nodes; ++i)
    {
        if(i != source-1)
        {
            print_path(&nodes[source-1], &nodes[i]);
            printf("\n");
        }
    }
    
    return 1;
}