home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
gdead.berkeley.edu
/
gdead.berkeley.edu.tar
/
gdead.berkeley.edu
/
pub
/
cad-tools
/
ciftomann.tar
/
driver_dir
/
pwait.c
< prev
next >
Wrap
C/C++ Source or Header
|
1988-01-28
|
2KB
|
85 lines
/*
* Wait for a process the right way. We wait for a particular
* process and if any others come along in between, we remember them
* in case they are eventually waited for.
*
* This routine is not very efficient when the number of processes
* to be remembered is large.
*/
char *malloc();
#include <stdio.h>
#define nil(type) ( (type) 0)
#define alloc(num,type) ( (type *) malloc(num*sizeof(type)))
#define dispose(ptr) free( (char *) ptr)
#define LOCAL static
typedef struct pidlist {
int pid;
int status;
struct pidlist *next;
} PIDLIST;
LOCAL PIDLIST *pidlist, *pfind();
pwait(pid, statusp)
int pid, *statusp;
{
PIDLIST *p;
int pnum, status;
p = pfind(pid);
if (p != nil(PIDLIST *)) {
*statusp = p->status;
dispose(p);
return;
}
while ((pnum = wait(&status)) != pid && pnum >= 0) {
p = alloc(1, PIDLIST);
p->pid = pnum;
p->status = status;
p->next = pidlist;
pidlist = p;
}
if (pnum < 0) {
p = pfind(pid);
if (p == nil(PIDLIST *)) {
panic("pwait: pid %d not found", pid);
}
*statusp = p->status;
dispose(p);
} else {
*statusp = status;
}
}
/*
* Look for the given process id on the pidlist.
*
* Unlink it from list if found.
*/
LOCAL PIDLIST *pfind(pid)
int pid;
{
register PIDLIST *p, *prev;
prev = nil(PIDLIST *);
for (p = pidlist; p != nil(PIDLIST *); p = p->next) {
if (p->pid == pid) {
break;
}
prev = p;
}
if (p != nil(PIDLIST *)) {
if (prev == nil(PIDLIST *)) {
pidlist = p->next;
} else {
prev->next = p->next;
}
}
return(p);
}