struct kinfo_proc2 *
cmp_procs(struct kinfo_proc2 *p1, struct kinfo_proc2 *p2)
{
    if (is_runnable(p1) && !is_runnable(p2))
        return (p1);
    if (!is_runnable(p1) && is_runnable(p2))
        return (p2);

    if (is_stopped(p1) && !is_stopped(p2))
        return (p1);
    if (!is_stopped(p1) && is_stopped(p2))
        return (p2);

    if (p1->p_estcpu > p2->p_estcpu)
        return (p1);
    if (p1->p_estcpu < p2->p_estcpu)
        return (p2);

    if (p1->p_slptime < p2->p_slptime)
        return (p1);
    if (p1->p_slptime > p2->p_slptime)
        return (p2);

    if (p1->p_pid > p2->p_pid)
        return (p1);
    return (p2);
}
Esempio n. 2
0
struct kinfo_proc *
cmp_procs(struct kinfo_proc *p1, struct kinfo_proc *p2)
{
	if (is_runnable(p1) && !is_runnable(p2))
		return (p1);
	if (!is_runnable(p1) && is_runnable(p2))
		return (p2);

	if (is_stopped(p1) && !is_stopped(p2))
		return (p1);
	if (!is_stopped(p1) && is_stopped(p2))
		return (p2);

	if (p1->ki_estcpu > p2->ki_estcpu)
		return (p1);
	if (p1->ki_estcpu < p2->ki_estcpu)
		return (p2);

	if (p1->ki_slptime < p2->ki_slptime)
		return (p1);
	if (p1->ki_slptime > p2->ki_slptime)
		return (p2);

	if (strcmp(p1->ki_comm, p2->ki_comm) < 0)
		return (p1);
	if (strcmp(p1->ki_comm, p2->ki_comm) > 0)
		return (p2);

	if (p1->ki_pid > p2->ki_pid)
		return (p1);
	return (p2);
}
Esempio n. 3
0
struct proc *
cmp_procs(struct proc *p1, struct proc *p2)
{
	void	*ptr1, *ptr2;

	if (is_runnable(p1) && !is_runnable(p2))
		return (p1);
	if (!is_runnable(p1) && is_runnable(p2))
		return (p2);

	if (is_stopped(p1) && !is_stopped(p2))
		return (p1);
	if (!is_stopped(p1) && is_stopped(p2))
		return (p2);

	if (p1->p_estcpu > p2->p_estcpu)
		return (p1);
	if (p1->p_estcpu < p2->p_estcpu)
		return (p2);

	if (p1->p_slptime < p2->p_slptime)
		return (p1);
	if (p1->p_slptime > p2->p_slptime)
		return (p2);

	if ((p1->p_flag & P_SINTR) && !(p2->p_flag & P_SINTR))
		return (p1);
	if (!(p1->p_flag & P_SINTR) && (p2->p_flag & P_SINTR))
		return (p2);

	ptr1 = LIST_FIRST(&p1->p_children);
	ptr2 = LIST_FIRST(&p2->p_children);
	if (ptr1 == NULL && ptr2 != NULL)
		return (p1);
	if (ptr1 != NULL && ptr2 == NULL)
		return (p2);

	if (strcmp(p1->p_comm, p2->p_comm) < 0)
		return (p1);
	if (strcmp(p1->p_comm, p2->p_comm) > 0)
		return (p2);

	if (p1->p_pid > p2->p_pid)
		return (p1);
	return (p2);
}
Esempio n. 4
0
struct kinfo_proc2 *
cmp_procs(struct kinfo_proc2 *p1, struct kinfo_proc2 *p2)
{
	if (is_runnable(p1) && !is_runnable(p2))
		return (p1);
	if (!is_runnable(p1) && is_runnable(p2))
		return (p2);

	if (is_stopped(p1) && !is_stopped(p2))
		return (p1);
	if (!is_stopped(p1) && is_stopped(p2))
		return (p2);

	if (p1->p_estcpu > p2->p_estcpu)
		return (p1);
	if (p1->p_estcpu < p2->p_estcpu)
		return (p2);

	if (p1->p_slptime < p2->p_slptime)
		return (p1);
	if (p1->p_slptime > p2->p_slptime)
		return (p2);

	if ((p1->p_flag & P_SINTR) && !(p2->p_flag & P_SINTR))
		return (p1);
	if (!(p1->p_flag & P_SINTR) && (p2->p_flag & P_SINTR))
		return (p2);

	if (strcmp(p1->p_comm, p2->p_comm) < 0)
		return (p1);
	if (strcmp(p1->p_comm, p2->p_comm) > 0)
		return (p2);

	if (p1->p_pid > p2->p_pid)
		return (p1);
	return (p2);
}
Esempio n. 5
0
static void
handle_file(FileView *view, FileHandleExec exec, FileHandleLink follow)
{
	char full_path[PATH_MAX];
	int executable;
	int runnable;
	const dir_entry_t *const curr = &view->dir_entry[view->list_pos];

	get_full_path_of(curr, sizeof(full_path), full_path);

	if(is_dir(full_path) || is_unc_root(view->curr_dir))
	{
		if(!curr->selected && (curr->type != FT_LINK || follow == FHL_NO_FOLLOW))
		{
			open_dir(view);
			return;
		}
	}

	runnable = is_runnable(view, full_path, curr->type, follow == FHL_FOLLOW);
	executable = is_executable(full_path, curr, exec == FHE_NO_RUN, runnable);

	if(stats_file_choose_action_set() && (executable || runnable))
	{
		/* The call below does not return. */
		vifm_choose_files(view, 0, NULL);
	}

	if(executable && !is_dir_entry(full_path, curr->type))
	{
		execute_file(full_path, exec == FHE_ELEVATE_AND_RUN);
	}
	else if(runnable)
	{
		run_selection(view, exec == FHE_NO_RUN);
	}
	else if(curr->type == FT_LINK)
	{
		follow_link(view, follow == FHL_FOLLOW);
	}
}
Esempio n. 6
0
/*
 * This function expects that *first == NULL.
 *
 * XXX: Technically, there is no need for a linked list, so perhaps we
 * just allocate an array in the future?
 */
static int build_item_list(const char *dirpath, struct run_item **first)
{
	int ret, num, i;
	char path[PATH_MAX + 1];
	struct dirent *dirent;
	struct dirent **namelist;
	struct run_item *item;

	num = scandir(dirpath, &namelist, run_filter, alphasort);
	if (num == -1)
		return errno;
	if (num == 0)
		return 0;

	ret = 0;
	for(i = num - 1; i >= 0; i--) {
		dirent = namelist[i];

		snprintf(path, PATH_MAX + 1, "%s/%s", dirpath, dirent->d_name);

		if (!is_runnable(path))
			continue;

		item = calloc(1, sizeof(struct run_item));
		if (!item) {
			ret = ENOMEM;
			break;
		}

		strcpy(item->ri_name, path);
		item->ri_next = *first;

		*first = item;
	}

	while(num--)
		free(namelist[num]);
	free(namelist);

	return ret;
}
Esempio n. 7
0
void
execute_thread(void *c)
{
  command_t command = (command_t) c;
  pthread_mutex_lock(&d_mutex);
  while(!is_runnable(pthread_self()))
  {
    pthread_mutex_unlock(&d_mutex);
    pthread_yield();
    pthread_mutex_lock(&d_mutex);
  }
  pthread_mutex_unlock(&d_mutex);
  exec_command(c);
  pthread_mutex_lock(&d_mutex);
  pthread_mutex_lock(&tc_mutex);
  thread_count--;
  remove_dependencies(pthread_self());
  pthread_mutex_unlock(&tc_mutex);
  pthread_mutex_unlock(&d_mutex);
  free(command);
  pthread_exit(0);
}
Esempio n. 8
0
//------------------------------------------------------------------------------
//"sc_method_process::trigger_dynamic"
//
// This method sets up a dynamic trigger on an event.
//------------------------------------------------------------------------------
bool
sc_method_process::trigger_dynamic( sc_event* e )
{
    if ( is_runnable() )
    {
        return false;
    }
    m_timed_out = false;
    switch ( m_trigger_type )
    {
    case EVENT:
        m_event_p = 0;
        m_trigger_type = STATIC;
        return true;
    case OR_LIST:
        m_event_list_p->remove_dynamic( this, e );
        m_event_list_p->auto_delete();
        m_event_list_p = 0;
        m_trigger_type = STATIC;
        return true;
    case AND_LIST:
        if ( -- m_event_count == 0 )
        {
            // no need to remove_dynamic
            m_event_list_p->auto_delete();
            m_event_list_p = 0;
            m_trigger_type = STATIC;
            return true;
        }
        return false;
    case TIMEOUT:
        m_trigger_type = STATIC;
        return true;
    case EVENT_TIMEOUT:
        if ( e == m_event_p )
        {
            m_timeout_event_p->cancel();
            m_timeout_event_p->reset();
        }
        else
        {
            m_timed_out = true;
            m_event_p->remove_dynamic( this );
        }
        m_event_p = 0;
        m_trigger_type = STATIC;
        return true;
    case OR_LIST_TIMEOUT:
        if ( e != m_timeout_event_p )
        {
            m_timeout_event_p->cancel();
            m_timeout_event_p->reset();
        }
        else
        {
            m_timed_out = true;
        }
        m_event_list_p->remove_dynamic( this, e );
        m_event_list_p->auto_delete();
        m_event_list_p = 0;
        m_trigger_type = STATIC;
        return true;
    case AND_LIST_TIMEOUT:
        if ( e == m_timeout_event_p )
        {
            m_timed_out = true;
            m_event_list_p->remove_dynamic( this, e );
            m_event_list_p->auto_delete();
            m_event_list_p = 0;
            m_trigger_type = STATIC;
            return true;
        }
        else if ( -- m_event_count == 0 )
        {
            m_timeout_event_p->cancel();
            m_timeout_event_p->reset();
            // no need to remove_dynamic
            m_event_list_p->auto_delete();
            m_event_list_p = 0;
            m_trigger_type = STATIC;
            return true;
        }
        return false;
    case STATIC:
        // we should never get here
        assert( false );
    }
    return false;
}
Esempio n. 9
0
//------------------------------------------------------------------------------
//"sc_method_process::trigger_dynamic"
//
// This method sets up a dynamic trigger on an event.
//
// Notes:
//   (1) This method is identical to sc_thread_process::trigger_dynamic(), 
//       but they cannot be combined as sc_process_b::trigger_dynamic() 
//       because the signatures things like sc_event::remove_dynamic()
//       have different overloads for sc_method_process* and sc_thread_process*.
//       So if you change code here you'll also need to change it in 
//       sc_thread_process.cpp.
//
// Result is true if this process should be removed from the event's list,
// false if not.
//
// If the triggering process is the same process, the trigger is
// ignored as well, unless SC_ENABLE_IMMEDIATE_SELF_NOTIFICATIONS
// is defined.
//------------------------------------------------------------------------------
bool sc_method_process::trigger_dynamic( sc_event* e )
{
    // No time outs yet, and keep gcc happy.

    m_timed_out = false;

    // Escape cases:
    //   (a) If this method issued the notify() don't schedule it for
    //       execution, but leave the sensitivity in place.
    //   (b) If this method is already runnable can't trigger an event.

#if ! defined( SC_ENABLE_IMMEDIATE_SELF_NOTIFICATIONS )
    if( SC_UNLIKELY_( sc_get_current_process_b() == this ) )
    {
        report_immediate_self_notification();
        return false;
    }
#endif // SC_ENABLE_IMMEDIATE_SELF_NOTIFICATIONS

    if( is_runnable() ) 
        return true;

    // If a process is disabled then we ignore any events, leaving them enabled:
    //
    // But if this is a time out event we need to remove both it and the
    // event that was being waited for.

    if ( m_state & ps_bit_disabled )
    {
        if ( e == m_timeout_event_p )
	{
	    remove_dynamic_events( true );  
	    return true;
	}
	else
	{
	    return false;
	}
    }


    // Process based on the event type and current process state:
    //
    // Every case needs to set 'rc' and continue on to the end of
    // this method to allow suspend processing to work correctly.

    switch( m_trigger_type ) 
    {
      case EVENT: 
	m_event_p = 0;
	m_trigger_type = STATIC;
	break;

      case AND_LIST:
        -- m_event_count;
	if ( m_event_count == 0 )
	{
	    m_event_list_p->auto_delete();
	    m_event_list_p = 0;
	    m_trigger_type = STATIC;
	}
	else
	{
	    return true;
	}
	break;

      case OR_LIST:
	m_event_list_p->remove_dynamic( this, e );
	m_event_list_p->auto_delete();
	m_event_list_p = 0;
	m_trigger_type = STATIC;
	break;

      case TIMEOUT: 
	m_trigger_type = STATIC;
	break;

      case EVENT_TIMEOUT: 
        if ( e == m_timeout_event_p )
	{
	    m_timed_out = true;
	    m_event_p->remove_dynamic( this );
	    m_event_p = 0;
	    m_trigger_type = STATIC;
	}
	else
	{
	    m_timeout_event_p->cancel();
	    m_timeout_event_p->reset();
	    m_event_p = 0;
	    m_trigger_type = STATIC;
	}
	break;

      case OR_LIST_TIMEOUT:
        if ( e == m_timeout_event_p )
	{
            m_timed_out = true;
            m_event_list_p->remove_dynamic( this, e ); 
            m_event_list_p->auto_delete();
            m_event_list_p = 0; 
            m_trigger_type = STATIC;
	}

	else
	{
            m_timeout_event_p->cancel();
            m_timeout_event_p->reset();
	    m_event_list_p->remove_dynamic( this, e ); 
	    m_event_list_p->auto_delete();
	    m_event_list_p = 0; 
	    m_trigger_type = STATIC;
	}
	break;
      
      case AND_LIST_TIMEOUT:
        if ( e == m_timeout_event_p )
	{
            m_timed_out = true;
            m_event_list_p->remove_dynamic( this, e ); 
            m_event_list_p->auto_delete();
            m_event_list_p = 0; 
            m_trigger_type = STATIC;
	}

	else
	{
	    -- m_event_count;
	    if ( m_event_count == 0 )
	    {
		m_timeout_event_p->cancel();
		m_timeout_event_p->reset();
		// no need to remove_dynamic
		m_event_list_p->auto_delete();
		m_event_list_p = 0; 
		m_trigger_type = STATIC;
	    }
	    else
	    {
	        return true;
	    }
	}
	break;

      case STATIC: {
        // we should never get here, but throw_it() can make it happen.
	SC_REPORT_WARNING(SC_ID_NOT_EXPECTING_DYNAMIC_EVENT_NOTIFY_, name());
        return true;
      }
    }

    // If we get here then the method has satisfied its next_trigger, if its 
    // suspended mark its state as ready to run. If its not suspended then push
    // it onto the runnable queue.

    if ( (m_state & ps_bit_suspended) )
    {
	m_state = m_state | ps_bit_ready_to_run;
    }
    else
    {
        simcontext()->push_runnable_method(this);
    }

    return true;
}
Esempio n. 10
0
int main(int argc, char *argv[])
{
	int ret, exec_argc, len;
	char path[PATH_MAX + 1];
	char **exec_argv = NULL;
	struct run_item *item_list = NULL;
	struct run_item *tmp_item;

	ret = MPI_Init(&argc, &argv);
	if (ret != MPI_SUCCESS) {
		fprintf(stderr, "MPI_Init failed: %d\n", ret);
		return ret;
	}

	if (argc < 2) {
		usage();
		ret = 1;
		goto out;
	}

	len = strlen(argv[1]);
	if (len > PATH_MAX) {
		fprintf(stderr, "Path \"%s\" is too long\n", argv[1]);
		ret = ENAMETOOLONG;
		goto out;
	}

	strncpy(path, argv[1], PATH_MAX + 1);

	while (len && path[--len] == '/')
		path[len] = '\0';

	exec_argc = argc - 2;
	if (exec_argc)
		exec_argv = &argv[2];

	if (is_runnable(path)) {
		ret = run_executible(path, exec_argc, exec_argv);
		if (ret)
			fprintf(stderr, "Error %d executing %s\n", ret, path);
		goto out;
	}

	ret = build_item_list(path, &item_list);
	if (ret) {
		fprintf(stderr, "Error %d reading directory %s\n", ret, path);
		goto out;
	}

	ret = run_item_list(item_list, exec_argc, exec_argv);
	if (ret)
		fprintf(stderr, "Error %d executing from directory %s\n", ret,
			path);

out:
	while (item_list) {
		tmp_item = item_list;
		item_list = item_list->ri_next;
		free(tmp_item);
	}

	if (ret)
		MPI_Abort(MPI_COMM_WORLD, 1);
	else
		MPI_Finalize();

	return ret;
}
Esempio n. 11
0
char *
osdep_get_name(int fd, char *tty)
{
	int		 mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PGRP, 0 };
	struct stat	 sb;
	size_t		 len;
	struct kinfo_proc *buf, *newbuf, *p, *bestp;
	u_int		 i;
	char		*name;

	buf = NULL;

	if (stat(tty, &sb) == -1)
		return (NULL);
	if ((mib[3] = tcgetpgrp(fd)) == -1)
		return (NULL);

retry:
	if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) == -1)
		return (NULL);
	len = (len * 5) / 4;

	if ((newbuf = realloc(buf, len)) == NULL) {
		free(buf);
		return (NULL);
	}
	buf = newbuf;

	if (sysctl(mib, nitems(mib), buf, &len, NULL, 0) == -1) {
		if (errno == ENOMEM)
			goto retry;
		free(buf);
		return (NULL);
	}

	bestp = NULL;
	for (i = 0; i < len / sizeof (struct kinfo_proc); i++) {
		if (buf[i].ki_tdev != sb.st_rdev)
			continue;
		p = &buf[i];
		if (bestp == NULL) {
			bestp = p;
			continue;
		}

		if (is_runnable(p) && !is_runnable(bestp))
			bestp = p;
		else if (!is_runnable(p) && is_runnable(bestp))
			continue;

		if (!is_stopped(p) && is_stopped(bestp))
			bestp = p;
		else if (is_stopped(p) && !is_stopped(bestp))
			continue;

		if (p->ki_estcpu > bestp->ki_estcpu)
			bestp = p;
		else if (p->ki_estcpu < bestp->ki_estcpu)
			continue;

		if (p->ki_slptime < bestp->ki_slptime)
			bestp = p;
		else if (p->ki_slptime > bestp->ki_slptime)
			continue;

		if (strcmp(p->ki_comm, p->ki_comm) < 0)
			bestp = p;
		else if (strcmp(p->ki_comm, p->ki_comm) > 0)
			continue;

		if (p->ki_pid > bestp->ki_pid)
			bestp = p;
	}

	name = NULL;
	if (bestp != NULL)
		name = strdup(bestp->ki_comm);

	free(buf);
	return (name);
}