void start_sched(){
  
  INIT_SCHED();
  
  // Mettre peut être le signal() après les new_proc selon la dernière question ? 
  signal(SIGALRM,&commut);
  
  if(mysetjmp(SCHED_PROC)==0){
    
    new_proc(f,BEGIN_PROC);
    new_proc(g,BEGIN_PROC+1);
    new_proc(f,BEGIN_PROC+2);
    new_proc(g,BEGIN_PROC+3);    


    alarm(1);
    int p = election();
    mylongjmp(p);
  }
  else{
    // Fin d'un processus    
    int p = election();
    mylongjmp(p);
  }
  
  
}
예제 #2
0
// loads the processes in some file 
void load_processes(FILE* file, node_t** head)
{
    // command buffer
    char buff[BUFFER_LEN] = { 0 };

    // get each process from file
    while(fgets(buff, BUFFER_LEN, file))
    {
        // make new process object
        proc pobj = new_proc();
        get_process_info_from_line_q5(buff, &pobj);


        // initialize head or push new value
        if(!*head)
        {
            *head = malloc(sizeof(node_t));
            (*head)->next = NULL;
            (*head)->process = pobj;
        } else {
            push(*head, pobj);
        }
    }
    
}
예제 #3
0
파일: process.cpp 프로젝트: odz/tracef
  /* static */ pprocess 
  process::clone(pid_t pid, bool create_thread, const process& parent)
  {
    pprocess new_proc(new process(pid, 
                                  parent.get_pid(), 
                                  create_thread,
                                  parent.pr_, 
                                  parent.pelf_,
                                  parent.opts_));
    
    if (parent.tracing_ && parent.opts_->trace_child_) {
      new_proc->tracing_ = true;
    } else {
      new_proc->tracing_ = false;
    }

    if (!create_thread) {
      new_proc->call_level_     = parent.call_level_;
      new_proc->call_stack_     = parent.call_stack_;

      // copy 'return address from fork@plt'
      new_proc->plt_call_stack_ = parent.plt_call_stack_;
    }

    mask m; // RAII
    procs_->insert(std::make_pair(pid, new_proc));
    return new_proc;
  }
예제 #4
0
void pop_l_proc(struct proc **inicio) {
    struct proc *nuevo = new_proc();
    nuevo = *inicio;
    *inicio = (*inicio)->sig;
	free(nuevo->nombreProc);
	free(nuevo->pNodos);
    free(nuevo);
}
예제 #5
0
void push_l_proc(struct proc **inicio, char *nombreProc, union nodeTypeTag *pProc) {
    struct proc *nuevo = new_proc();

	nuevo->nombreProc = (char *)malloc(strlen(nombreProc) + 1);
    strcpy(nuevo->nombreProc, nombreProc);
	//nuevo->pNodos = malloc(sizeof(union *pFun));
	nuevo->pNodos = pProc;
    nuevo->sig = *inicio;
    *inicio = nuevo;
}
예제 #6
0
파일: main.c 프로젝트: Masshat/Scheduler
int
main ( int argc, char** argv )
{

  ptr_f f_;
  ptr_f g_;
  int res;

  init_sched();

  f_ = &f;
  new_proc(f_, 0);
  res = election();
  tproc[res].p_state = SRUNNING;
  g_ = &f;
  new_proc(g_, 1);
  election();

  return EXIT_SUCCESS;
}
예제 #7
0
void ListaProcedimentos::AdicionaProcedimento(Procedimento * P) {
	

	std::pair<std::string, Procedimento *> new_proc(P->obtemNome(), P);
	bool OK = false;
	
	OK = this->procedimentos_do_prog->insert(new_proc).second;
	if (OK) return;
	else throw Erro("\nProcedimento declarado duas vezes"+P->obtemNome());
		
}
예제 #8
0
파일: process.cpp 프로젝트: odz/tracef
 /* static */ pprocess 
 process::create_first_process(pid_t pid,
                               boost::shared_ptr<printer> pr,
                               boost::shared_ptr<const cl_options> popt)
 {
   boost::shared_ptr<elf> null;
   tracer_pid_ = ::getpid(); // libc-call
   pprocess new_proc(new process(pid, tracer_pid_,
                                 false,      // non-thread
                                 pr, null, popt));
   mask m; // RAII
   procs_->insert(std::make_pair(pid, new_proc));
   return new_proc;
 }
예제 #9
0
void save_procinfo(char *filename,psinfo_t *psinf)
{
	if(DEBUG) printf("%s\n",filename);
	FILE *fd = fopen(filename,"r");
	if (fd > 0)
	{
		psinfo_t psinfo;
		fread(&psinfo,sizeof(psinfo_t),1,fd);
		proc *p = new_proc(
			psinfo.pr_pid,
			psinfo.pr_fname,
			psinfo.pr_ppid,
			psinfo.pr_uid,
			psinfo.pr_start.tv_sec,
			psinfo.pr_psargs[0] == '-' && in_shells(psinfo.pr_fname)
		);
		proc_structs[proc_count++] = p;
		proc_structs[proc_count] = NULL;
		fclose(fd);
	}
}
예제 #10
0
/**
    Start and intialize it all
*/
int main( )
{
    u32 i;

    // Seed the random generator for unique non-repeating numbers
    //srand( time( NULL ) );

    // Setup the file we write our data too
    outFile = fopen( "output.txt", "w" );

    // Setup our queues
    initialize_queues( );

    // Setup the pages
    for ( i = 1; i < PAGE_COUNT; i++ )
    {
        page_free(i);
    }

    // Jump to the first processes time
    set_time( proc_init[PROC_POINT]._start_time );

    // Create the processes
    u16 proc1 = new_proc( proc_init[PROC_POINT]._start_time, proc_init[PROC_POINT]._run_time, proc_init[PROC_POINT]._code_size, proc_init[PROC_POINT]._data_size, proc_init[PROC_POINT]._priority );

    printf("MAIN - New Process Index #:\t%i\n\n", PROC_POINT);

    // Increment our pointer
    PROC_POINT++;

    // Put the new process on the queue
    ready_enq( proc1 );

    // Start it up
    scheduler( );
}
예제 #11
0
/**
    Runs it all
*/
void scheduler( )
{
    u16 temp_proc;

    // While we have stuff on the queues or list
    while ( blocked_list 
    	    || high_head 
    	    || med_head 
    	    || low_head )
    {
        if ( blocked_list 
        	 && !high_head 
        	 && !med_head 
        	 && !low_head )
        {
            printf("******************\n");
            printf("CURRENT_TIME:\t%llu\n", get_time());
            printf("SET_TIME TO: mem[blocked_list]._proc._time:\t%llu\n", mem[blocked_list]._proc._time);
            printf("******************\n");

            set_time( mem[blocked_list]._proc._time );
            printf("before deq\n");
            
            printf("mem[blocked_list]._proc._arrid:\t%i\n", mem[blocked_list]._proc._arrid);
            printf("mem[mem[blocked_list]._proc._next]._proc._arrid:\t%i\n", mem[mem[blocked_list]._proc._next]._proc._arrid);
            printf("mem[mem[mem[blocked_list]._proc._next]._proc._next]._proc._arrid:\t%i\n", mem[mem[mem[blocked_list]._proc._next]._proc._next]._proc._arrid);

            printf("get_time():\t%llu\n", get_time());
            blocked_deq( );
            printf("after deq\n");
        }

        // High priority
        u8 count = 0;
        while ( high_head 
        	    && count < 4 )
        {
            printf("RUNNING HIGH\n");
            temp_proc = ready_deq( 1 );
            printf("mem[temp_proc]._proc._next:\t%llu\n", mem[temp_proc]._proc._next);
            
            if ( mem[temp_proc]._proc._next ) {
                int x = 1 / 0;
            }

            printf("starting execute process with: %i\n", temp_proc);
            process_exec( temp_proc, get_time( ) + 2500 );
            printf("starting blocked in high\n");
            blocked_deq( );
            printf("done with blocked in high\n");

            count++;
        }

        // Med priority
        count = 0;
        while ( med_head 
        	    && count < 2 )
        {
            printf("RUNNING MED\n");

            temp_proc = ready_deq( 2 );
            process_exec( temp_proc, get_time( ) + 2500 );
            blocked_deq( );

            count++;
        }

        // Low priority
        count = 0;
        while ( low_head 
        	    && count < 1 )
        {
            printf("RUNNING LOW\n");
            temp_proc = ready_deq( 3 );
            process_exec( temp_proc, get_time( ) + 2500 );
            blocked_deq( );

            count++;
        }

        // Try to make other processes with currents
        u16 result = 1;
        while ( result && PROC_POINT < MAX_PROCS )
        {
            printf("IN WHILE - get_time():\t%llu\n", get_time());
            printf("proc_init[PROC_POINT]._start_time:\t%llu\n", proc_init[PROC_POINT]._start_time);
            result = new_proc( proc_init[PROC_POINT]._start_time, proc_init[PROC_POINT]._run_time, proc_init[PROC_POINT]._code_size, proc_init[PROC_POINT]._data_size, proc_init[PROC_POINT]._priority );
            printf("result:\t%llu\n", result);
            //printf("get_time():\t%llu\n", get_time());
            //printf("proc_init[PROC_POINT]._start_time:\t%llu\n\n\n", proc_init[PROC_POINT]._start_time);

            // Proc is ready to start, we got something back, put it on the queue
            if ( result )
            {
                printf("----------------------------------------------\n");
                printf("WHILE - New Process Index #:\t%i\n", PROC_POINT);
                printf("proc_init[PROC_POINT]._start_time:\t%llu\n", proc_init[PROC_POINT]._start_time);
                printf("----------------------------------------------\n");
                PROC_POINT++;
                ready_enq( result );
            }
        }

        // Nothing on queues and list but we haven't ran all the processes
        if ( PROC_POINT < MAX_PROCS 
        	 && !blocked_list 
        	 && !high_head 
        	 && !med_head 
        	 && !low_head )
        {
            printf("----------------------------------------------\n");
            printf("CURRENT TIME BEFORE SET:\t%llu\n", get_time());

            set_time( proc_init[PROC_POINT]._start_time );
            temp_proc = new_proc( proc_init[PROC_POINT]._start_time, proc_init[PROC_POINT]._run_time, proc_init[PROC_POINT]._code_size, proc_init[PROC_POINT]._data_size, proc_init[PROC_POINT]._priority );
            
            printf("IF - New Process Index #:\t%i\n", PROC_POINT);
            printf("NEW PROCESSES START TIME:\t%llu\n", proc_init[PROC_POINT]._start_time);
            printf("CURRENT TIME AFTER SET:\t\t%llu\n", get_time());
            printf("----------------------------------------------\n\n\n");
            
            PROC_POINT++;

            printf("high_head:\t%i\n", high_head);
            printf("med_head:\t%i\n", med_head);
            printf("low_head:\t%i\n", low_head);
            printf("mem[high_head]._proc._next:\t%i\n", mem[high_head]._proc._next);
            printf("mem[med_head]._proc._next:\t%i\n", mem[med_head]._proc._next);
            printf("mem[low_head]._proc._next:\t%i\n", mem[low_head]._proc._next);

            ready_enq( temp_proc );

            printf("high_head:\t%i\n", high_head);
            printf("med_head:\t%i\n", med_head);
            printf("low_head:\t%i\n", low_head);
            printf("mem[high_head]._proc._next:\t%i\n", mem[high_head]._proc._next);
            printf("mem[med_head]._proc._next:\t%i\n", mem[med_head]._proc._next);
            printf("mem[low_head]._proc._next:\t%i\n", mem[low_head]._proc._next);
        }
    }
    printf("FINISHED EXECUTING\n");
}
예제 #12
0
/* execute tree in child subprocess */
int
exchild(struct op *t, int flags, volatile int *xerrok,
    int close_fd)	/* used if XPCLOSE or XCCLOSE */
{
	static Proc	*last_proc;	/* for pipelines */

	int		i;
	sigset_t	omask;
	Proc		*p;
	Job		*j;
	int		rv = 0;
	int		forksleep;
	int		ischild;

	if (flags & XEXEC)
		/* Clear XFORK|XPCLOSE|XCCLOSE|XCOPROC|XPIPEO|XPIPEI|XXCOM|XBGND
		 * (also done in another execute() below)
		 */
		return execute(t, flags & (XEXEC | XERROK), xerrok);

	/* no SIGCHLD's while messing with job and process lists */
	sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);

	p = new_proc();
	p->next = (Proc *) 0;
	p->state = PRUNNING;
	p->status = 0;
	p->pid = 0;

	/* link process into jobs list */
	if (flags&XPIPEI) {	/* continuing with a pipe */
		if (!last_job)
			internal_errorf(1,
			    "exchild: XPIPEI and no last_job - pid %d",
			    (int) procpid);
		j = last_job;
		last_proc->next = p;
		last_proc = p;
	} else {
		j = new_job(); /* fills in j->job */
		/* we don't consider XXCOM's foreground since they don't get
		 * tty process group and we don't save or restore tty modes.
		 */
		j->flags = (flags & XXCOM) ? JF_XXCOM :
		    ((flags & XBGND) ? 0 : (JF_FG|JF_USETTYMODE));
		timerclear(&j->usrtime);
		timerclear(&j->systime);
		j->state = PRUNNING;
		j->pgrp = 0;
		j->ppid = procpid;
		j->age = ++njobs;
		j->proc_list = p;
		j->coproc_id = 0;
		last_job = j;
		last_proc = p;
		put_job(j, PJ_PAST_STOPPED);
	}

	snptreef(p->command, sizeof(p->command), "%T", t);

	/* create child process */
	forksleep = 1;
	while ((i = fork()) < 0 && errno == EAGAIN && forksleep < 32) {
		if (intrsig)	 /* allow user to ^C out... */
			break;
		sleep(forksleep);
		forksleep <<= 1;
	}
	if (i < 0) {
		kill_job(j, SIGKILL);
		remove_job(j, "fork failed");
		sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
		errorf("cannot fork - try again");
	}
	ischild = i == 0;
	if (ischild)
		p->pid = procpid = getpid();
	else
		p->pid = i;

#ifdef JOBS
	/* job control set up */
	if (Flag(FMONITOR) && !(flags&XXCOM)) {
		int	dotty = 0;
		if (j->pgrp == 0) {	/* First process */
			j->pgrp = p->pid;
			dotty = 1;
		}

		/* set pgrp in both parent and child to deal with race
		 * condition
		 */
		setpgid(p->pid, j->pgrp);
		/* YYY: should this be
		   if (ttypgrp_ok && ischild && !(flags&XBGND))
			tcsetpgrp(tty_fd, j->pgrp);
		   instead? (see also YYY below)
		 */
		if (ttypgrp_ok && dotty && !(flags & XBGND))
			tcsetpgrp(tty_fd, j->pgrp);
	}
#endif /* JOBS */

	/* used to close pipe input fd */
	if (close_fd >= 0 && (((flags & XPCLOSE) && !ischild) ||
	    ((flags & XCCLOSE) && ischild)))
		close(close_fd);
	if (ischild) {		/* child */
		/* Do this before restoring signal */
		if (flags & XCOPROC)
			coproc_cleanup(false);
		sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
		cleanup_parents_env();
#ifdef JOBS
		/* If FMONITOR or FTALKING is set, these signals are ignored,
		 * if neither FMONITOR nor FTALKING are set, the signals have
		 * their inherited values.
		 */
		if (Flag(FMONITOR) && !(flags & XXCOM)) {
			for (i = NELEM(tt_sigs); --i >= 0; )
				setsig(&sigtraps[tt_sigs[i]], SIG_DFL,
				    SS_RESTORE_DFL|SS_FORCE);
		}
#endif /* JOBS */
		if (Flag(FBGNICE) && (flags & XBGND))
			nice(4);
		if ((flags & XBGND) && !Flag(FMONITOR)) {
			setsig(&sigtraps[SIGINT], SIG_IGN,
			    SS_RESTORE_IGN|SS_FORCE);
			setsig(&sigtraps[SIGQUIT], SIG_IGN,
			    SS_RESTORE_IGN|SS_FORCE);
			if (!(flags & (XPIPEI | XCOPROC))) {
				int fd = open("/dev/null", 0);
				if (fd != 0) {
					(void) ksh_dup2(fd, 0, true);
					close(fd);
				}
			}
		}
		remove_job(j, "child");	/* in case of `jobs` command */
		nzombie = 0;
#ifdef JOBS
		ttypgrp_ok = 0;
		Flag(FMONITOR) = 0;
#endif /* JOBS */
		Flag(FTALKING) = 0;
		tty_close();
		cleartraps();
		execute(t, (flags & XERROK) | XEXEC, NULL); /* no return */
		internal_errorf(0, "exchild: execute() returned");
		unwind(LLEAVE);
		/* NOTREACHED */
	}

	/* shell (parent) stuff */
	/* Ensure next child gets a (slightly) different $RANDOM sequence */
	change_random();
	if (!(flags & XPIPEO)) {	/* last process in a job */
#ifdef JOBS
		/* YYY: Is this needed? (see also YYY above)
		   if (Flag(FMONITOR) && !(flags&(XXCOM|XBGND)))
			tcsetpgrp(tty_fd, j->pgrp);
		*/
#endif /* JOBS */
		j_startjob(j);
		if (flags & XCOPROC) {
			j->coproc_id = coproc.id;
			coproc.njobs++; /* n jobs using co-process output */
			coproc.job = (void *) j; /* j using co-process input */
		}
		if (flags & XBGND) {
			j_set_async(j);
			if (Flag(FTALKING)) {
				shf_fprintf(shl_out, "[%d]", j->job);
				for (p = j->proc_list; p; p = p->next)
					shf_fprintf(shl_out, " %d", p->pid);
				shf_putchar('\n', shl_out);
				shf_flush(shl_out);
			}
		} else
			rv = j_waitj(j, JW_NONE, "jw:last proc");
	}

	sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);

	return rv;
}
예제 #13
0
파일: jobs.c 프로젝트: adtools/abcsh
int
exchild(struct op *t, int flags,
        int close_fd)       /* used if XPCLOSE or XCCLOSE */
{
        static Proc     *last_proc;     /* for pipelines */

        int             i;
        sigset_t        omask;
        Proc            *p;
        Job             *j;
        int             rv = 0;
        int             forksleep;
        int             ischild;

        if (flags & XEXEC)
                /* Clear XFORK|XPCLOSE|XCCLOSE|XCOPROC|XPIPEO|XPIPEI|XXCOM|XBGND
                 * (also done in another execute() below)
                 */
                return execute(t, flags & (XEXEC | XERROK));

        p = new_proc();
        p->next = (Proc *) 0;
        p->state = PRUNNING;
        p->status = 0;
        p->pid = 0;

        /* link process into jobs list */
        if (flags&XPIPEI) {     /* continuing with a pipe */
                if (!last_job)
                        internal_errorf(1,
                                "exchild: XPIPEI and no last_job - pid %d",
                                (int) procpid);
                j = last_job;
                last_proc->next = p;
                last_proc = p;
        } else {
                j = new_job(); /* fills in j->job */
                /* we don't consider XXCOM's foreground since they don't get
                 * tty process group and we don't save or restore tty modes.
                 */
                j->flags = (flags & XXCOM) ? JF_XXCOM :
                        ((flags & XBGND) ? 0 : (JF_FG|JF_USETTYMODE));
                j->usrtime = j->systime = 0;
                j->state = PRUNNING;
                j->pgrp = 0;
                j->ppid = procpid;
                j->age = ++njobs;
                j->proc_list = p;
                j->coproc_id = 0;
                last_job = j;
                last_proc = p;
                put_job(j, PJ_PAST_STOPPED);
        }

        snptreef(p->command, sizeof(p->command), "%T", t);

        /* create child process */
        forksleep = 1;
        while ((i = fork()) < 0 && errno == EAGAIN && forksleep < 32) {
                if (intrsig)     /* allow user to ^C out... */
                        break;
                sleep(forksleep);
                forksleep <<= 1;
        }
        if (i < 0) {
                kill_job(j, SIGKILL);
                remove_job(j, "fork failed");
                sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
                errorf("cannot fork - try again");
        }
        ischild = i == 0;
        if (ischild)
                p->pid = procpid = getpid();
        else
                p->pid = i;

        /* used to close pipe input fd */
        if (close_fd >= 0 && (((flags & XPCLOSE) && !ischild) ||
                ((flags & XCCLOSE) && ischild)))
                        close(close_fd);
        if (ischild) {          /* child */
                /* Do this before restoring signal */
                if (flags & XCOPROC)
                        coproc_cleanup(false);
                sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
		cleanup_parents_env();
                if ((flags & XBGND)) {
                        setsig(&sigtraps[SIGINT], SIG_IGN,
                                SS_RESTORE_IGN|SS_FORCE);
                        setsig(&sigtraps[SIGQUIT], SIG_IGN,
                                SS_RESTORE_IGN|SS_FORCE);
                        if (!(flags & (XPIPEI | XCOPROC))) {
                                int fd = open("/dev/null", 0);
                                if (fd != 0) {
				        (void) ksh_dup2(fd, 0, true);
                                        close(fd);
                                }

                        }
                }
                remove_job(j, "child"); /* in case of `jobs` command */
                nzombie = 0;
                Flag(FTALKING) = 0;
                tty_close();
                cleartraps();
                execute(t, (flags & XERROK) | XEXEC); /* no return */
                internal_errorf(0, "exchild: execute() returned");
                unwind(LLEAVE);
                /* NOTREACHED */
        }

        /* shell (parent) stuff */
        /* Ensure next child gets a (slightly) different $RANDOM sequence */
        change_random();
        if (!(flags & XPIPEO)) {        /* last process in a job */
                j_startjob(j);
                if (flags & XCOPROC) {
                        j->coproc_id = coproc.id;
                        coproc.njobs++; /* n jobs using co-process output */
                        coproc.job = (void *) j; /* j using co-process input */
                }
                if (flags & XBGND) {
                        j_set_async(j);
                        if (Flag(FTALKING)) {
                                shf_fprintf(shl_out, "[%d]", j->job);
                                for (p = j->proc_list; p; p = p->next)
                                        shf_fprintf(shl_out, " %d", p->pid);
                                shf_putchar('\n', shl_out);
                                shf_flush(shl_out);
                        }
                } else
                        rv = j_waitj(j, JW_NONE, "jw:last proc");
        }

        sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);

        return rv;
}
예제 #14
0
ListaProcedimentos::ListaProcedimentos(Procedimento * P) {
	
	this->procedimentos_do_prog = new Procedimentos;	
	std::pair<std::string, Procedimento *> new_proc(P->obtemNome(), P);
	this->procedimentos_do_prog->insert(new_proc);
}
예제 #15
0
caddr_t
get_process_info(struct system_info * si,
				 struct process_select * sel,
				 int compare_index, char *conninfo, int mode)
{
	struct timeval thistime;
	double		timediff,
				alpha,
				beta;
	struct top_proc *proc;
	pid_t		pid;
	unsigned long now;
	unsigned long elapsed;
	int			i;

	/* calculate the time difference since our last check */
	gettimeofday(&thistime, 0);
	if (lasttime.tv_sec)
	{
		timediff = ((thistime.tv_sec - lasttime.tv_sec) +
					(thistime.tv_usec - lasttime.tv_usec) * 1e-6);
	}
	else
	{
		timediff = 0;
	}
	lasttime = thistime;

	/* round current time to a second */
	now = (unsigned long) thistime.tv_sec;
	if (thistime.tv_usec >= 500000)
	{
		now++;
	}

	/* calculate constants for the exponental average */
	if (timediff > 0.0 && timediff < 30.0)
	{
		alpha = 0.5 * (timediff / 30.0);
		beta = 1.0 - alpha;
	}
	else
	{
		alpha = beta = 0.5;
	}
	timediff *= HZ;				/* convert to ticks */

	/* mark all hash table entries as not seen */
	for (i = 0; i < HASH_SIZE; ++i)
	{
		for (proc = ptable[i]; proc; proc = proc->next)
		{
			proc->state = 0;
		}
	}

	/* read the process information */
	{
		int			total_procs = 0;
		struct top_proc **active;

		int			show_idle = sel->idle;
		int			show_uid = sel->uid != -1;

		int			i;
		int			rows;
		PGconn	   *pgconn;
		PGresult   *pgresult = NULL;

		memset(process_states, 0, sizeof(process_states));

		pgconn = connect_to_db(conninfo);
		if (pgconn != NULL)
		{
			pgresult = pg_processes(pgconn);
			rows = PQntuples(pgresult);
		}
		else
		{
			rows = 0;
		}
		for (i = 0; i < rows; i++)
		{
			char	   *procpid = PQgetvalue(pgresult, i, 0);
			struct top_proc *pp;
			unsigned long otime;

			pid = atoi(procpid);

			/* look up hash table entry */
			proc = pp = ptable[HASH(pid)];
			while (proc && proc->pid != pid)
			{
				proc = proc->next;
			}

			/* if we came up empty, create a new entry */
			if (proc == NULL)
			{
				proc = new_proc();
				proc->pid = pid;
				proc->next = pp;
				ptable[HASH(pid)] = proc;
				proc->time = 0;
				proc->wcpu = 0;
			}

			otime = proc->time;

			read_one_proc_stat(pid, proc, sel);
			if (sel->fullcmd == 2)
				update_procname(proc, PQgetvalue(pgresult, i, 1));

			if (proc->state == 0)
				continue;

			total_procs++;
			process_states[proc->state]++;

			if (timediff > 0.0)
			{
				if ((proc->pcpu = (proc->time - otime) / timediff) < 0.0001)
				{
					proc->pcpu = 0;
				}
				proc->wcpu = proc->pcpu * alpha + proc->wcpu * beta;
			}
			else if ((elapsed = (now - boottime) * HZ - proc->start_time) > 0)
			{
				/*
				 * What's with the noop statement? if ((proc->pcpu =
				 * (double)proc->time / (double)elapsed) < 0.0001) {
				 * proc->pcpu; }
				 */
				proc->wcpu = proc->pcpu;
			}
			else
			{
				proc->wcpu = proc->pcpu = 0.0;
			}
		}
		if (pgresult != NULL)
			PQclear(pgresult);
		PQfinish(pgconn);

		/* make sure we have enough slots for the active procs */
		if (activesize < total_procs)
		{
			pactive = (struct top_proc **) realloc(pactive,
									sizeof(struct top_proc *) * total_procs);
			activesize = total_procs;
		}

		/* set up the active procs and flush dead entries */
		active = pactive;
		for (i = 0; i < HASH_SIZE; i++)
		{
			struct top_proc *last;
			struct top_proc *ptmp;

			last = NULL;
			proc = ptable[i];
			while (proc != NULL)
			{
				if (proc->state == 0)
				{
					ptmp = proc;
					if (last)
					{
						proc = last->next = proc->next;
					}
					else
					{
						proc = ptable[i] = proc->next;
					}
					free_proc(ptmp);
				}
				else
				{
					if ((show_idle || proc->state == 1 || proc->pcpu) &&
						(!show_uid || proc->uid == sel->uid))
					{
						*active++ = proc;
						last = proc;
					}
					proc = proc->next;
				}
			}
		}

		si->p_active = active - pactive;
		si->p_total = total_procs;
		si->procstates = process_states;
	}

	/* if requested, sort the "active" procs */
	if (si->p_active) {
		if (mode == MODE_IO_STATS) {
			qsort(pactive, si->p_active, sizeof(struct top_proc *),
			  		io_compares[compare_index]);
		}
		else
		{
			qsort(pactive, si->p_active, sizeof(struct top_proc *),
			  		proc_compares[compare_index]);
		}
	}

	/* don't even pretend that the return value thing here isn't bogus */
	nextactive = pactive;
	return (caddr_t) 0;
}