ratrace: avoid blank line prints, make writer the parent
when there where multiple syscalls returning out of order, it would print blank lines between the exits. avoid this by remembering if the last char written was a newline and conditionally insert newline on out of order return. sometimes, ratrace would return before all messages have been printed. make the writer process the parent so ratrace wont exit until all readers are finished avoiding the problem.
This commit is contained in:
parent
059c85dd75
commit
34f3df213c
1 changed files with 26 additions and 15 deletions
|
@ -9,7 +9,7 @@ enum {
|
||||||
Channel *out;
|
Channel *out;
|
||||||
Channel *quit;
|
Channel *quit;
|
||||||
Channel *forkc;
|
Channel *forkc;
|
||||||
int nread = 0;
|
int readers = 1;
|
||||||
|
|
||||||
typedef struct Msg Msg;
|
typedef struct Msg Msg;
|
||||||
struct Msg {
|
struct Msg {
|
||||||
|
@ -36,8 +36,10 @@ die(Reader *r)
|
||||||
snprint(s->buf, sizeof(s->buf), " = %r\n");
|
snprint(s->buf, sizeof(s->buf), " = %r\n");
|
||||||
s->pid = r->pid;
|
s->pid = r->pid;
|
||||||
sendp(quit, s);
|
sendp(quit, s);
|
||||||
close(r->tfd);
|
if(r->tfd >= 0)
|
||||||
close(r->cfd);
|
close(r->tfd);
|
||||||
|
if(r->cfd >= 0)
|
||||||
|
close(r->cfd);
|
||||||
threadexits(nil);
|
threadexits(nil);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,13 +107,12 @@ reader(void *v)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
writer(void *arg)
|
writer(int lastpid)
|
||||||
{
|
{
|
||||||
int lastpid;
|
char lastc = -1;
|
||||||
Alt a[4];
|
Alt a[4];
|
||||||
Msg *s;
|
Msg *s;
|
||||||
|
int n;
|
||||||
lastpid = (int)(uintptr)arg;
|
|
||||||
|
|
||||||
a[0].op = CHANRCV;
|
a[0].op = CHANRCV;
|
||||||
a[0].c = quit;
|
a[0].c = quit;
|
||||||
|
@ -124,20 +125,29 @@ writer(void *arg)
|
||||||
a[2].v = nil;
|
a[2].v = nil;
|
||||||
a[3].op = CHANEND;
|
a[3].op = CHANEND;
|
||||||
|
|
||||||
while(nread > 0){
|
while(readers > 0){
|
||||||
switch(alt(a)){
|
switch(alt(a)){
|
||||||
case 0:
|
case 0:
|
||||||
nread--;
|
readers--;
|
||||||
case 1:
|
case 1:
|
||||||
if(s->pid != lastpid){
|
if(s->pid != lastpid){
|
||||||
lastpid = s->pid;
|
lastpid = s->pid;
|
||||||
fprint(2, s->buf[1]=='='? "\n%d ...": "\n", lastpid);
|
if(lastc != '\n'){
|
||||||
|
lastc = '\n';
|
||||||
|
write(2, &lastc, 1);
|
||||||
|
}
|
||||||
|
if(s->buf[1] == '=')
|
||||||
|
fprint(2, "%d ...", lastpid);
|
||||||
|
}
|
||||||
|
n = strlen(s->buf);
|
||||||
|
if(n > 0){
|
||||||
|
write(2, s->buf, n);
|
||||||
|
lastc = s->buf[n-1];
|
||||||
}
|
}
|
||||||
write(2, s->buf, strlen(s->buf));
|
|
||||||
free(s);
|
free(s);
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
nread++;
|
readers++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -196,7 +206,8 @@ threadmain(int argc, char **argv)
|
||||||
out = chancreate(sizeof(Msg*), 0);
|
out = chancreate(sizeof(Msg*), 0);
|
||||||
quit = chancreate(sizeof(Msg*), 0);
|
quit = chancreate(sizeof(Msg*), 0);
|
||||||
forkc = chancreate(sizeof(void*), 0);
|
forkc = chancreate(sizeof(void*), 0);
|
||||||
nread++;
|
procrfork(reader, (void*)pid, Stacksize, 0);
|
||||||
procrfork(writer, (void*)pid, Stacksize, 0);
|
|
||||||
reader((void*)pid);
|
writer(pid);
|
||||||
|
threadexits(nil);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue