Exemple #1
3
int traverseBFS(Graph graph, int start, Dllist close) {
	Dllist node, queue;
	JRB visited;
	int *output;
	int temp;

	int i, n, counter = 0;

	visited = make_jrb();
	queue = new_dllist();
	dll_append(queue, new_jval_i(start));

	while(!dll_empty(queue)) {
		node = dll_first(queue);
		temp = jval_i(node->val);
		dll_delete_node(node);

		if(jrb_find_int(visited, temp) == NULL) {
			counter++;
			// reportFunc(temp);
			jrb_insert_int(visited, temp, new_jval_i(temp));
			
			n = outdegree(graph, temp, output);
			for(i = 0; i < n; i++) {
				if(jrb_find_int(visited, output[i]) == NULL) {
					dll_append(queue, new_jval_i(output[i]));
				}
			}
		}
	}

	return counter;
}
Exemple #2
0
t_queue* dc_new_queue(void (*data_destructor)(void*))
{
	t_queue* queue;

	queue=kmalloc(sizeof(t_queue));
	queue->data=new_dllist();
	queue->data_destructor=data_destructor;!!!!!!!!!!!!!!!!!!!qui
	return new_dllist();
}
Exemple #3
0
int searchController(Graph graph, int start, int stop, int option) {
	Dllist close;
	Dllist node;

	close = new_dllist();

	switch(option) {
		case BFS_SEARCH:
		breathFirstSearch(graph, start, stop, close);
		break;
		case BFS_TRAVERSE:
		traverseBFS(graph, start, close);
		break;
		case DFS_SEARCH:
		deepFirstSearch(graph, start, stop, close);
		break;
		case DFS_TRAVERSE:
		traverseDFS(graph, start, close);
		break;
		default:
		printf("This is not an option\n");
		free_dllist(close);
		return;
	}

	node = dll_first(close);
	printf("Visit %d\n", jval_i(node->val));

	free_dllist(close);
}
Exemple #4
0
KOS() {
  
  /* Semaphores */
  writeOK =  make_kt_sem(0);
  writers = make_kt_sem(1);	
  readers = make_kt_sem(1);
  nElem = make_kt_sem(0);
  consoleWait = make_kt_sem(0);

  /* Generics */
  sys_stop_read = 0;
  current_pid = 0;
  console_size = 256;
  buffer_head, buffer_tail;
  
  // Zero out memory
  bzero(main_memory, MemorySize);
  bzero(memory_space_array, 8);
  
  /* Initializers */
  currentProcess = (PCB *) malloc(sizeof(PCB));
  initialize_console_buffer(&buffer_head, &buffer_tail);
  readyQ = new_dllist();
  found_node = make_jrb();
  pid_tree = make_jrb();
  kt_fork(initialize_user_process, (void *)kos_argv);
  kt_fork(console_buf_read, (void *)kos_argv[0]);
  kt_joinall();
  start_timer(10);
  scheduler();
}
Exemple #5
0
main(int argc, char **argv)
{
  IS is;
  int n;
  Dllist l;
  Dllist tmp;

  if (argc != 2) {
    fprintf(stderr, "usage: dlltail n\n");
    exit(1);
  }
  n = atoi(argv[1]);
  if (n < 0) {
    fprintf(stderr, "usage: dlltail n  -- n must be >= 0\n");
    exit(1);
  }

  is = new_inputstruct(NULL);
  l = new_dllist();

  while (get_line(is) >= 0) {
    dll_append(l, new_jval_s(strdup(is->text1)));
    if (is->line > n) {
      tmp = dll_first(l);
      free(jval_s(dll_val(tmp)));
      dll_delete_node(tmp);
    }
  }

  dll_traverse(tmp, l) printf("%s", jval_s(tmp->val));
}
Exemple #6
0
main()
{
  IS is;
  Queue q;
  Stack s;
  Dllist l;
  int i;
  Jval j;

  is = new_inputstruct(NULL);

  while (get_line(is) > 0) {
    q = new_queue();
    s = new_stack();
    l = new_dllist();
    for (i = 0; i < strlen(is->fields[0]); i++) {
      queue_enqueue(q, new_jval_c(is->fields[0][i]));
      stack_push(s, new_jval_c(is->fields[0][i]));
      dll_append(l, new_jval_c(is->fields[0][i]));
      dll_prepend(l, new_jval_c(is->fields[0][i]));
    }
    while (!queue_empty(q)) {
      j = queue_dequeue(q); printf("%c", j.c);
      j = stack_pop(s); printf("%c", j.c);
      printf("%c", l->flink->val.c); dll_delete_node(l->flink);
      printf("%c", l->flink->val.c); dll_delete_node(l->flink);
      printf(" ");
    }
    printf("\n");
    free_queue(q);
    free_stack(s);
    free_dllist(l);
  }
}
Exemple #7
0
void
InitKThreadSystem()
{
	int ok;

	if(KtInit_d)
	{
		return;
	}

	ok = 0;

#ifdef SOLARIS
	ok++;
#endif

#ifdef LINUX
	ok++;
#endif

	if (ok == 0) {
          fprintf(stderr, "KThread error: No architecture specified %s\n",
                          "at compile time");
          exit(1);
        }
        if (ok > 1) {
          fprintf(stderr, "KThread error: Multiple architectures %s\n",
                          "specified at compile time");
          exit(1);
        }

	ktActive = make_jrb();
	ktRunnable = new_dllist();
	ktFree_me = new_dllist();
	ktBlocked = make_jrb();
	ktSleeping = make_jrb();
        ktThread_count = 0;
	ktTidCounter = 1;
	ktSidCounter = -1;

	ktOriginal = InitKThread(0,NULL,NULL);
	ktRunning = ktOriginal;

	KtInit_d = 1;

	return;
}
Exemple #8
0
t_tcp_conn_map* tcp_conn_map_init()
{
	t_tcp_conn_map* tcp_conn_map;
	
	tcp_conn_map = kmalloc(sizeof(t_tcp_conn_map));
	tcp_conn_map->conn_map = hashtable_init(TCP_CONN_MAP_SIZE);
	tcp_conn_map->duplicate_conn_list = new_dllist();
	tcp_conn_map->is_key_unique = 0;
 }
void initialize_simulation(Elevator_Simulation *es)
{
    Queue *list = mmalloc(Queue, 1);
    list->passengers = new_dllist();
    dll_empty(list->passengers);
    list->cond = mmalloc(pthread_cond_t, 1);
    pthread_cond_init(list->cond, NULL);
    list->count = 0;
    (*es).v = list; 
}
Exemple #10
0
void initialize_simulation(Elevator_Simulation *es){
    cond = malloc(sizeof(pthread_cond_t));
    lock = malloc(sizeof(pthread_mutex_t));
    
    sim = es;
    top = sim -> nfloors;
    people = new_dllist();
    pthread_cond_init(cond, NULL);
    pthread_mutex_init(lock, NULL);
}
Exemple #11
0
int UShortestPath(Graph graph, Jval start, Jval stop, 
	Jval (*cloneFunc)(Jval), int (*compare)(Jval, Jval), void (*reportFunc)(Jval)) {

	Dllist node, queue, stackVisit;
	JRB visited;
	Jval *output;
	Jval temp, tmp;

	int i, n;

	visited = make_jrb();
	queue = new_dllist();
	stackVisit = new_dllist();
	dll_append(queue, start);

	if((output = myMalloc(sizeof(Jval), 100)) == NULL) {
		return 0;
	}

	while(!dll_empty(queue)) {
		node = dll_first(queue);
		temp = cloneFunc(node->val);
		dll_delete_node(node);

		if(jrb_find_gen(visited, temp, compare) == NULL) {
			jrb_insert_gen(visited, temp, temp, compare);
			dll_prepend(stackVisit, temp);
			
			if(compare(temp, stop) == 0) {
				return solution(graph, start, stop, stackVisit, cloneFunc, compare, reportFunc);
			}

			n = getAdjacentVertices(graph, temp, output, compare);
			for(i = 0; i < n; i++) {
				if(jrb_find_gen(visited, output[i], compare) == NULL) {
					dll_append(queue, output[i]);
				}
			}
		}
	}

	return -1;
}
Exemple #12
0
int main (int argc, char **argv)
{
  struct msgbuff{
    long mtype;
    pid_t pid;
  }message;

  pid_t pid, npid;
  int i;

  int           msqid;
  key_t         keyx;
  struct msqid_ds msq;

  Dllist q = new_dllist();
  Dllist n;

  keyx = ftok(KEYFILE_PATH, (int)ID);

  msqid = msgget(keyx, 0666 | IPC_CREAT);

  if (msqid == -1) {
    perror("msgget");
    exit(1);
  }

  while(1) { 
    if((msgrcv(msqid, &message, sizeof(pid_t), 1, 0)) ==
       MSGQ_NG){
      perror("msgrcv");
      exit(1);
    }
    pid = message.pid;
    if(pid != 1) {
      if(dll_empty(q)) kill(pid, SIGUSR1);
      else {
        n = dll_first(q);
        npid = jval_i(dll_val(n));
        if(pid == npid) { 
          kill(pid, SIGUSR1);
          dll_delete_node(n);
        } else dll_append(q, new_jval_i(pid));
      }
    } else {
      if(!dll_empty(q)) { 
        n = dll_first(q);
        npid = jval_i(dll_val(n));
        kill(npid, SIGUSR1);
        dll_delete_node(n);
      }
    }
  }

  return 0;
}
Exemple #13
0
void init_ata(t_device_desc* device_desc)
{	
	struct t_i_desc i_desc;
	
	i_desc.baseLow=((int)&int_handler_ata) & 0xFFFF;
	i_desc.selector=0x8;
	i_desc.flags=0x0EF00;
	i_desc.baseHi=((int)&int_handler_ata)>>0x10;
	set_idt_entry(0x2E,&i_desc);
	device_desc->pending_request=new_dllist();
	device_desc->read=_read_28_ata;
	device_desc->write=_write_28_ata;
	device_desc->status=REQUEST_COMPLETED;
}
Exemple #14
0
int UShortestPath(Graph graph, int start, int stop, Dllist close) {
	Dllist node, queue, stackVisit;
	JRB visited;
	int output[100];
	int temp;

	int i, n;

	visited = make_jrb();
	queue = new_dllist();
	stackVisit = new_dllist();
	dll_append(queue, new_jval_i(start));

	while(!dll_empty(queue)) {
		node = dll_first(queue);
		temp = jval_i(node->val);
		dll_delete_node(node);

		if(jrb_find_int(visited, temp) == NULL) {
			jrb_insert_int(visited, temp, new_jval_i(temp));
			dll_prepend(stackVisit, new_jval_i(temp));
			
			if(temp == stop) {
				return solution(graph, start, stop, stackVisit, close);
			}

			n = outdegree(graph, temp, output);
			for(i = 0; i < n; i++) {
				if(jrb_find_int(visited, output[i]) == NULL) {
					dll_append(queue, new_jval_i(output[i]));
				}
			}
		}
	}

	return -1;
}
Exemple #15
0
/* parseResult
 * Parses a message containing the graph adjacency matrix
 * Message format "[graphsize]:[graph adjacency matrix]"
 */
static void parseResult(char *pch) {
	/* Get gsize */
	pch = strtok(NULL, ":");
	int gsize = atoi(pch);

	/* Get Clique Count */
	pch = strtok(NULL, ":");
	int clCount = atoi(pch);

	/* Get matrix */
	pch = strtok(NULL, ":");
	int *g = ChartoGraph(pch, gsize);

	/* Verify integrity of g */
	int realCount = CliqueCount(g, gsize);

	/* Message is invalid */
	if (realCount != clCount) {
		fprintf(stderr, "Message could not be validated!\n");
		fprintf(stderr, "Clique count from message: %d, actual clique count: %d!\n", clCount, realCount);
		return;
	}

	/* Update scheduler */
	if(clCount == 0) {
		fprintf(stderr, "Counterexample successfully received!\n");
		if(gsize > _Scheduler->currCEsize) { /* Found a counterexample */
			/* Update Scheduler */
			_Scheduler->currCEsize = gsize;
			/* clear list and add new counterexample */
			free_dllist(_Scheduler->counterExamples);
			_Scheduler->counterExamples = new_dllist();
			_Scheduler->listSize = 0;
			addCounterExample(g);
			/* Update current pointer */
			_Scheduler->currPtr = dll_first(_Scheduler->counterExamples);
			/*print only when save a counterexample*/
			fprintf(stderr, "get a counterexample with bigger size, size: %d\n, currCEsize: %d\n", gsize, _Scheduler->currCEsize);
			/* Save counterexample into a file */
			SaveGraph(g,gsize, "../../../counterexamples");
		}
		/* Just add new counterexample */
		else if(gsize == _Scheduler->currCEsize) {
				fprintf(stderr, "Saving a counterexample with same size\n");
				addCounterExample(g);
				SaveGraph(g,gsize, "../../../counterexamples");
		}
	}
}
Exemple #16
0
int solution(Graph graph, Jval start, Jval stop, Dllist stackVisit, Jval (*cloneFunc)(Jval),
	int (*compare)(Jval, Jval), void (*reportFunc)(Jval)) {

	Dllist stackRes;
	Dllist node;
	Jval nowNode, temp;
	int counter = 0;

	stackRes = new_dllist();
	node = dll_first(stackVisit);
	nowNode = cloneFunc(node->val);
	dll_delete_node(node);
	dll_prepend(stackRes, nowNode);

	while(!dll_empty(stackVisit)) {
		if(isAdjacent(graph, nowNode, start, compare)) {
			dll_prepend(stackRes, start);
			counter++;
			break;
		}

		do {
			node = dll_first(stackVisit);
			temp = cloneFunc(node->val);
			dll_delete_node(node);	

			if(isAdjacent(graph, nowNode, temp, compare)) {
				dll_prepend(stackRes, temp);
				nowNode = temp;
				counter++;
				break;
			}
		} while(!dll_empty(stackVisit));
	}

	printf("Solution: The shortest path between two node: \n");

	while(!dll_empty(stackRes)) {
		node = dll_first(stackRes);
		reportFunc(node->val);
		dll_delete_node(node);
	}

	free_dllist(stackVisit);

	return counter;
}
Exemple #17
0
/* initializeScheduler
 * Initializes scheduler if it has not been initialized yet.
 * Returns 0 if the scheduler was initialized correctly.
 * Returns 1 if the scheduler already exists.
 * Returns -1 if the initialization was unsuccessful.
 */
int initializeScheduler(void) {
	if(_Scheduler == NULL) {
		_Scheduler = (Scheduler*) malloc(sizeof(Scheduler));
		if(_Scheduler == NULL)
			return -1;

		/* Set the Scheduler pointer to be shared */
#ifdef __APPLE__
		_Scheduler = mmap(NULL, sizeof(_Scheduler), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
#else
		_Scheduler = mmap(NULL, sizeof(_Scheduler), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
#endif

		/* Initialize fields */
		_Scheduler->counterExamples = new_dllist();
		_Scheduler->listSize = 0;

		/* Load best graph counterexamples */
		int file_count;
		char **CEfiles = getCounterExamplesFromFolder("../../../counterexamples/more121", &file_count);
		int i;
		for(i = 0; i < file_count; i++) {
			int *g;
			char *fname;
			asprintf(&fname, "../../../counterexamples/more121/%s", CEfiles[i]);
			if(ReadGraph(fname, &g, &(_Scheduler->currCEsize))) {
				fprintf(stderr, "Loaded graph %s successfully!\n", fname);
				addCounterExample(g);
			}
		}
		/*Print list size*/
		fprintf(stderr, "List size after initialization: %d\n", _Scheduler->listSize);
		/* Initialize current list pointer */
		_Scheduler->currPtr = dll_first(_Scheduler->counterExamples);

		/* free CEfiles */
		for(i = 0; i < _Scheduler->listSize; i++) {
			free(CEfiles[i]);
		}
		free(CEfiles);

		return 0;
	}
	else
		return 1;
}
Exemple #18
0
//static char *Argv[5] = {"argtest","Rex", "my", "man",NULL};
KOS()
{
    //exitSignal = 0;
  // printf("Starting kos\n");
    bzero(main_memory, MemorySize);
    //make a PCB
    curProc = (PCB *) malloc(sizeof(PCB));
    //make a dllist(readyq) which holds user processes to be run
    readyq = new_dllist();
    //circular console size, head, and tail
    cbSize = 256; //Step 16
    cbHead = 0;
    cbTail = 0;

    //step 14: should be initialized to 1
    writers = make_kt_sem(1);
    readers = make_kt_sem(1);
    writeok = make_kt_sem(0);
    //step 17:
    consoleWait= make_kt_sem(0);
    nelem = make_kt_sem(0);
    nslots = make_kt_sem(256);
    //printf("Before Forking\n");

    //Step 10: pids
    curpid = 0;
    rbtree = make_jrb();
    foundPid = make_jrb(); //has to be its own mini tree

    //Lab4
    globalFD = 0;
    //Step 12: Memory splitting 8
    bzero(mem8,8);
    //fork to the file with args provided, just loading it
    kt_fork(initialize_user_process, kos_argv);
    //Step 4: fork to the file with args provided, now reading it, start from the very beginning kos_argv[0]
    kt_fork(console_buffer, kos_argv[0]);
    
    //now join all the threads
    kt_joinall();
    //call the scheduler
    //    start_timer(10);
    //scheduler();
    scheduler();
}
Exemple #19
0
main(int argc, char **argv){
 
  // Check for correct input 
  if(argc != 3){
    printf("usage: mmu <config_file> <trace_file>\n");
    exit(1);
  }
  
  parseConf(&conf , argv[1]);

  traceStack = new_dllist();
  parseTrace(&traceStack, argv[2]);

  setupSim(&conf, &mmuSim);

  runSim(&mmuSim, &traceStack);

  endSim(&mmuSim);
}
Exemple #20
0
void DFS(Graph graph, Jval start, Jval stop, 
	Jval (*cloneFunc)(Jval), int (*compare)(Jval, Jval), void (*reportFunc)(Jval)) {
	Dllist node, stack;
	JRB visited;
	Jval *output;
	Jval temp, tmp;

	int i, n;

	visited = make_jrb();
	stack = new_dllist();
	dll_prepend(stack, start);

	if((output = myMalloc(sizeof(Jval), 100)) == NULL) {
		return;
	}

	while(!dll_empty(stack)) {
		node = dll_first(stack);
		temp = cloneFunc(node->val);
		dll_delete_node(node);

		if(jrb_find_gen(visited, temp, compare) == NULL) {
			reportFunc(temp);
			jrb_insert_gen(visited, temp, temp, compare);
			
			if(compare(temp, stop) == 0) {
				jrb_free_tree(visited);
				free_dllist(stack);
				free(output);
				return;
			}

			n = getAdjacentVertices(graph, temp, output, compare);
			for(i = 0; i < n; i++) {
				if(jrb_find_gen(visited, output[i], compare) == NULL) {
					dll_prepend(stack, output[i]);
				}
			}
		}
	}
}
Exemple #21
0
int deepFirstSearch(Graph graph, int start, int stop, Dllist close) {
	Dllist node, stack;
	JRB visited;
	int output[100];
	int temp;

	int i, n;

	visited = make_jrb();
	stack = new_dllist();
	dll_prepend(stack, new_jval_i(start));

	while(!dll_empty(stack)) {
		node = dll_first(stack);
		temp = jval_i(node->val);
		dll_delete_node(node);

		if(jrb_find_int(visited, temp) == NULL) {
			// reportFunc(temp);
			dll_append(close, new_jval_i(temp));
			jrb_insert_int(visited, temp, new_jval_i(temp));
			
			if(compare(temp, stop) == 0) {
				jrb_free_tree(visited);
				free_dllist(stack);
				return 1;
			}

			n = outdegree(graph, temp, output);
			for(i = 0; i < n; i++) {
				if(jrb_find_int(visited, output[i]) == NULL) {
					dll_prepend(stack, new_jval_i(output[i]));
				}
			}
		}
	}

	jrb_free_tree(visited);
	free_dllist(stack);

	return 0;
}
Exemple #22
0
/*
 * insert bid in descending cost
 */
void insert_bid(char *msg)
{
  char *p;
  Bid *b, *tb;
  int i, id;
  Dllist ptr;

  // create a new bid
  b = (Bid *) malloc(sizeof(Bid));
  p = strstr(msg, "B");
  p++;
  b->cost = atoi(p);
  p = strstr(p, "$");
  p++;
  b->numrobot = atoi(p);
  b->coalition = new_dllist();
  for (i = 0; i < b->numrobot; i++) {
    p = strstr(p, "$");
    p++;
    id = atoi(p);
    if (dll_empty(b->coalition)) b->leaderID = id;
    dll_append(b->coalition, new_jval_i(id)); 
  }

  printf("insert bid: %s\n", msg);
  // insert the bid to bidList, increasing cost
  if (dll_empty(bidList)) {
    dll_append(bidList, new_jval_v(b));
  } else {
    dll_traverse(ptr, bidList) {
      tb = (Bid *) jval_v(dll_val(ptr));
      if (tb->cost > b->cost) {
        dll_insert_b(ptr, new_jval_v(b));
        break;
      }
      if (ptr == dll_last(bidList)) {// append it to the last
        dll_append(bidList, new_jval_v(b));
        break;
      } 
    }
  }
void BFS_Visit(Graph_Struct Graph,char start[])
{
	Graph_Symbol_Table *Table;
	char *u,*v;
	JRB node_rbt,tree,ele;
	Dllist node_queue,queue = new_dllist();
	printf("%s ",start);
	
	u = malloc(MAX_LENGTH_STRING);
	v = malloc(MAX_LENGTH_STRING);
	
	Table = Search_On_Table(Graph,start);
	Table->colour = 'g';
	
	dll_append(queue, new_jval_s(start));
	while(!dll_empty(queue))
	{
		node_queue = dll_first(queue);
		u = jval_s(node_queue->val);
		dll_delete_node(node_queue);
		node_rbt = jrb_find_str(Graph.Edges,u);
		if(node_rbt != NULL)
		{
			tree = (JRB)jval_v(node_rbt->val);
			jrb_traverse(ele,tree)
			{
				v = jval_s(ele->key);
				Table = Search_On_Table(Graph,v);
				if(Table->colour == 'w')
				{
					printf("%s ",v);
					Table->colour = 'g';
					if(Table->previous == NULL)
					{
						Table->previous = malloc(MAX_LENGTH_STRING);
					}
					strcpy(Table->previous,u);
					dll_append(queue, new_jval_s(v));
				}
			}
		}
Exemple #24
0
SOS()
{
        readyq = new_dllist();
        mt_init();
        writeok     = mt_sem_create(0);
        writers     = mt_sem_create(1);
        readers     = mt_sem_create(1);
        nelem       = mt_sem_create(0);
        consoleWait = mt_sem_create(0);
        
        wr_iobuf = make_io_buffer(1);
        cr_iobuf = make_io_buffer(256);
        crb_no_chars = 0;
        crb_end = 0; 
        crb_begin = 0;

        curpid = -1;
//      pids = make_rb();
        init_partitions(); 
        DEBUG('e', "pagesize: %d\n", PageSize);

	jrbTree = make_jrb();		// Step 20

	init = new_pcb();		// Step 22
	init->pid = get_new_pid();	// Step 22
   
        cread_vnode = new_vnode();
        cread_vnode->iobuf =cr_iobuf;
        cr_iobuf->nwriters = 1;
        cwrite_vnode = new_vnode();               
        cwrite_vnode->iobuf = wr_iobuf;
        wr_iobuf->nreaders = 1;
        
        start_timer(10);	
	bzero(main_memory, MemorySize);
        mt_create(read_console_io, (void *)cr_iobuf);
        mt_create(write_console_io, (void *)wr_iobuf);
        //mt_create(read_console, NULL);
        mt_create(initialize_user_process, (void *)Argv);
        schedule();
}
Exemple #25
0
void compress(int argc, char **argv, int verb) {
	int i, size;
	IS is;
	Dllist dir_list, DLLnode;
	JRB inode, JRBnode, paths, links;
	void *buf;
	tar *t;
	
	links = make_jrb();
	dir_list = new_dllist();
	inode = make_jrb();
	JRBnode = make_jrb();
	paths = make_jrb();

	for(i = 2; i < argc; i++){
		process_files(dir_list, inode, paths, argv[i], links);
	}

	dll_traverse(DLLnode, dir_list){
		process_files(dir_list, inode, paths, jval_s(DLLnode->val),links);
	}
Exemple #26
0
int BFStraverse(Graph graph, Jval start,
	Jval (*cloneFunc)(Jval), int (*compare)(Jval, Jval), void (*reportFunc)(Jval)) {
	
	Dllist node, queue;
	JRB visited;
	Jval *output;
	Jval temp, tmp;

	int i, n, counter = 0;

	visited = make_jrb();
	queue = new_dllist();
	dll_append(queue, start);

	if((output = myMalloc(sizeof(Jval), 100)) == NULL) {
		return counter;
	}

	while(!dll_empty(queue)) {
		node = dll_first(queue);
		temp = cloneFunc(node->val);
		dll_delete_node(node);

		if(jrb_find_gen(visited, temp, compare) == NULL) {
			counter++;
			reportFunc(temp);
			jrb_insert_gen(visited, temp, temp, compare);
			
			n = getAdjacentVertices(graph, temp, output, compare);
			for(i = 0; i < n; i++) {
				if(jrb_find_gen(visited, output[i], compare) == NULL) {
					dll_append(queue, output[i]);
				}
			}
		}
	}

	return counter;
}
Exemple #27
0
Fichier : kos.c Projet : cbia4/KOS
KOS() {
	writeok = make_kt_sem(0);
	writers = make_kt_sem(1);
	readers = make_kt_sem(1);
	nelem = make_kt_sem(0);
	consoleWait = make_kt_sem(0);

	sysStopRead = 0;
	consoleSize = BUFSIZE;

	bzero(main_memory, MemorySize);

	current = (PCB *) malloc(sizeof(PCB));
	initialize_console_buffer(&consoleBufferHead, &consoleBufferTail);
	queue = new_dllist();

	kt_fork(initialize_user_process, NULL);
	kt_fork(console_buf_read, NULL);
	kt_joinall();

	scheduler();
}
Exemple #28
0
void *elevator(void *arg){
    Elevator *e = (Elevator *) arg; 
    Dllist item, next, pickup;
    Person *p;
    int direction = 1;
    pickup = new_dllist();

    while(1){

        if(e -> onfloor >= top) direction = -1;
        else if(e -> onfloor <= 1) direction = 1;
        //printf("\tElevator[%i] on floor %i going %s:\n",
        //        e -> id, e -> onfloor, direction == 1 ? "up": "down"); 

        /* pick people up */
        pthread_mutex_lock(lock);

        item = dll_first(people);
        while(!dll_empty(people) && item != dll_nil(people)){
            next = dll_next(item);
            p = (Person *) item -> val.v;

            //printf("\t\tShould I get %s %s going from %i to %i? ",
            //        p -> fname, p -> lname, p -> from, p -> to);

            if(e -> onfloor == p -> from){
                if(p -> to > e -> onfloor && direction == 1 || 
                   p -> to < e -> onfloor && direction == -1){
                    dll_append(pickup, item -> val);
                    dll_delete_node(item);
                    //printf("yes!\n");

                }
                //else printf("no!\n");
            }
            //else printf("no!\n");

            item = next;
        }

        pthread_mutex_unlock(lock);

        item = dll_first(pickup);
        while(!dll_empty(pickup) && item != dll_nil(pickup)){
            next = dll_next(item);
            p = (Person *) item -> val.v;

            if(!e -> door_open) open_door(e);
            pthread_mutex_lock(p -> lock);

            p -> e = e;

            pthread_cond_signal(p -> cond);
            pthread_mutex_lock(e -> lock);
            pthread_mutex_unlock(p -> lock);

            pthread_cond_wait(e -> cond, e -> lock);
            pthread_mutex_unlock(e -> lock);

            dll_delete_node(item);
            item = next;
        }
        if(e -> door_open) close_door(e);

        move_to_floor(e, e -> onfloor + direction);

        /* drop people off */
        item = dll_first(e -> people);
        while(!dll_empty(e -> people) && item != dll_nil(e -> people)){
            next = dll_next(item);
            p = (Person *) item -> val.v;

            if(p -> to == e -> onfloor){
               if(!e -> door_open) open_door(e);
                
               pthread_mutex_lock(p -> lock);
               pthread_cond_signal(p -> cond);
               pthread_mutex_lock(e -> lock);
               pthread_mutex_unlock(p -> lock);

               pthread_cond_wait(e -> cond, e -> lock);
               pthread_mutex_unlock(e -> lock);
            }

            item = next;
        }
        //if(e -> door_open) close_door(e);

    }

    return NULL;
}
Exemple #29
0
Queue makeQueue() {
	return new_dllist();
}
Exemple #30
0
void *initialize_user_process(void *arg)
{
  //  printf("Enters initalize_user_process\n");
  int i;
  char **filename = (char **)arg;
  int argc = 0;
    
  while (filename[argc]!=NULL)
    {
       printf("This is filename[%i]: %s\n", argc, filename[argc]);
      argc++;
    }
  //    printf("This is argc in init: %i\n", argc);
    
  //Step 19: putting in argc
  //k = WordToMachine(argc);
  //memcpy(main_memory+MemorySize-40+12, &k, 4);
    
  //L3 Step 18: start first process, is this init? 
    init=(PCB *)malloc(sizeof(PCB));
    init->registers = (int *)malloc(NumTotalRegs*sizeof(int));
    for (i=0; i < NumTotalRegs; i++)
    {  
      init->registers[i] = 0;
    }
    init->pid = (int *)0;
    //    printf("This is init's pid: %i\n", (int)init->pid);
    //L3 Step 19: 
    init->waiter_sem = make_kt_sem(0);
    init->waiters = new_dllist();
    init->children = make_jrb();

    //Allocate a new PCB
  PCB *temp=(PCB *)malloc(sizeof(PCB));
  temp->registers = (int *)malloc(NumTotalRegs*sizeof(int));
  temp->user_base = 0;
  //printf("Initial user_base: %i\n", temp->user_base);
  //Changed Step 12: temp->user_limit = MemorySize-2048;
  temp->user_limit = MemorySize/8;
  //printf("Initial user_limt: %i\n", temp->user_limit);
  
  //L3 Step 18: 
  temp->parent = init;
  //L3 Step 19: 
  temp->waiter_sem = make_kt_sem(0);
  temp->waiters = new_dllist();
  //L3 Step 21: make rb tree for children 
  temp->children = make_jrb();
  
  //Changed at Step 12: User_Base = temp->user_base;
  User_Base = memory8();
  User_Limit = temp->user_limit;
  //printf("This is User_Base in initialize: %i\n", User_Base);
  //printf("This is User_Limit in initialize: %i\n", User_Limit);

  //set the regs of the
  //printf("Setting all the registers to 0 in initalize_user_process\n");
  for (i=0; i < NumTotalRegs; i++)
    temp->registers[i] = 0;
  
  //printf("Setting pid in init\n");
  temp->pid = (int *)get_new_pid();
  printf("First Pid: %i and its parent's should be 0 init: %i\n", temp->pid, temp->parent->pid );
  /* set up the program counters and the stack register */
  temp->registers[PCReg] = 0;
  temp->registers[NextPCReg] = 4;

  //insert the first process as init's child; WOW you can use this function!
  Jval tempN = new_jval_v((void*)temp); //can only insert Jvals 
  jrb_insert_int(init->children, (int)temp->pid, tempN); //JRB tree, int ikey, Jval val 
  //JRB ptr;
  //  jrb_traverse(ptr, init->children)
  //{
  //printf("This is child pid %i of init %i\n", ptr->key, (int)init->pid);
  //}
  //perform_execve(job, fn, argv)
  // where job is the new PCB, fn is the name of your initial executable (at this point, mine is a.out),
  //and argv is the argv of this initial job.
  //returns-> 0 success, errno if error
  //PrintStack(temp->registers[StackReg], temp->user_base);
  int errno = perform_execve(temp, filename[0],filename);
  // printf("This is perform_execve: %i\n", errno);
    //returns to initialize_user_process(), it either exits because there was an error, or it puts the new job onto the ready queue and calls kt_exit()
  PrintStack(temp->registers[StackReg], temp->user_base);
  if (errno!=0)
  {
      printf("Perform_execve returned unsucessful\n"); 
      kt_exit();
  }
  //printf("Placing jval into queue\n");
  Jval value = new_jval_v((void *)temp);
  //value.v = temp;
  //printf("This is the value being placed into the readyq in the initalize_user_process: %s\n",value.v );
  dll_append(readyq, value);
  // printf("Program %s loaded\n", kos_argv[0]);
  kt_exit();
}