Ejemplo n.º 1
0
void type(run_params* par) {
	int i;

	if (par->argc < 1) {
		pipe_write_s(par->err, "Too few arguments for 'type'.\n");
		return;
	}

	char* filename = par->args[0];
	node* n = node_get(filename, par->root_node, par->start_node);

	if (n == NULL) {
		pipe_write_s(par->err, "File does not exist.\n");
		return;
	}
	if (n->directory) {
		pipe_write_s(par->err, "File is a directory.\n");
		return;
	}

	if (!node_try_lock(n)) {
		pipe_write_s(par->err, "Can't open the file.\n");
		return;
	}

	for (i = 0; i < n->content_len; i++) {
		pipe_write(par->out, n->content[i]);
	}

	node_unlock(n);
}
Ejemplo n.º 2
0
static guint _worker_processNode(Worker* worker, Node* node, SimulationTime barrier) {
	/* update cache, reset clocks */
	worker->cached_node = node;
	worker->clock_last = SIMTIME_INVALID;
	worker->clock_now = SIMTIME_INVALID;
	worker->clock_barrier = barrier;

	/* lock the node */
	node_lock(worker->cached_node);

	EventQueue* eventq = node_getEvents(worker->cached_node);
	Event* nextEvent = eventqueue_peek(eventq);

	/* process all events in the nodes local queue */
	guint nEventsProcessed = 0;
	while(nextEvent && (nextEvent->time < worker->clock_barrier))
	{
		worker->cached_event = eventqueue_pop(eventq);
		MAGIC_ASSERT(worker->cached_event);

		/* make sure we don't jump backward in time */
		worker->clock_now = worker->cached_event->time;
		if(worker->clock_last != SIMTIME_INVALID) {
			g_assert(worker->clock_now >= worker->clock_last);
		}

		/* do the local task */
		gboolean complete = shadowevent_run(worker->cached_event);

		/* update times */
		worker->clock_last = worker->clock_now;
		worker->clock_now = SIMTIME_INVALID;

		/* finished event can now be destroyed */
		if(complete) {
			shadowevent_free(worker->cached_event);
			nEventsProcessed++;
		}

		/* get the next event, or NULL will tell us to break */
		nextEvent = eventqueue_peek(eventq);
	}

	/* unlock, clear cache */
	node_unlock(worker->cached_node);
	worker->cached_node = NULL;
	worker->cached_event = NULL;

	return nEventsProcessed;
}
Ejemplo n.º 3
0
void wc(run_params* par) {
	if (par->argc >= 1) {
		char* filename = par->args[0];
		node* n = node_get(filename, par->root_node, par->start_node);

		if (n == NULL) {
			pipe_write_s(par->err, "File does not exist.\n");
			return;
		}
		if (n->directory) {
			pipe_write_s(par->err, "File is a directory.\n");
			return;
		}

		if (!node_try_lock(n)) {
			pipe_write_s(par->err, "Can't open the file.\n");
			return;
		}

		wc_count(par, n->content, n->name);
		node_unlock(n);

	}
	else {
		int pos, c = -1, buf_size = 300;
		char* buf = (char*)malloc(sizeof(char) * buf_size);

		pos = 0;
		while (true) {
			c = pipe_read(par->in);
			if ((c == -1)) break;
			
			if (pos == buf_size - 1) {
				buf_size *= 2;
				buf = (char*)realloc(buf, sizeof(char*) * buf_size);
			}
			buf[pos] = c;
			pos++;
		}
		buf[pos] = '\0';

		wc_count(par, buf, "");
		free(buf);
		buf = NULL;
	}
}