/** * \brief Kill all threads (send "k" into pipe) */ void PipeListener::killAll() { _done = true; std::ofstream fpipe(_pipename, std::ios::out); fpipe << "k\n"; fpipe.close(); removePipe(); }
lispval Lprocess() { int wflag , childsi , childso , child; lispval handy; char *command, *p; int writep, readp; int itemp; int (*handler)(), (*signal())(); FILE *bufs[2],*obufs[2], *fpipe(); Savestack(0); writep = readp = FALSE; wflag = TRUE; switch(np-lbot) { case 3: if(lbot[2].val != nil) writep = TRUE; case 2: if(lbot[1].val != nil) readp = TRUE; wflag = 0; case 1: command = (char *) verify(lbot[0].val, "*process: non atom first arg"); break; default: argerr("*process"); } childsi = 0; childso = 1; /* if there will be communication between the processes, * it will be through these pipes: * parent -> bufs[1] -> bufs[0] -> child if writep * parent <- obufs[0] <- obufs[1] <- parent if readp */ if(writep) { fpipe(bufs); childsi = fileno(bufs[0]); } if(readp) { fpipe(obufs); childso = fileno(obufs[1]); } handler = signal(SIGINT,SIG_IGN); if((child = vfork()) == 0 ) { /* if we will wait for the child to finish * and if the process had ignored interrupts before * we were called, then leave them ignored, else * set it back the the default (death) */ if(wflag && handler != SIG_IGN) signal(2,SIG_DFL); if(writep) { close(0); dup(childsi); } if (readp) { close(1); dup(childso); } if ((p = (char *)getenv("SHELL")) != (char *)0) { execlp(p , p, "-c",command,0); _exit(-1); /* if exec fails, signal problems*/ } else { execlp("csh", "csh", "-c",command,0); execlp("sh", "sh", "-c",command,0); _exit(-1); /* if exec fails, signal problems*/ } } /* close the duplicated file descriptors * e.g. if writep is true then we've created two desriptors, * bufs[0] and bufs[1], we will write to bufs[1] and the * child (who has a copy of our bufs[0]) will read from bufs[0] * We (the parent) close bufs[0] since we will not be reading * from it. */ if(writep) fclose(bufs[0]); if(readp) fclose(obufs[1]); if(wflag && child!= -1) { int status=0; /* we await the death of the child */ while(wait(&status)!=child) {} /* the child has died */ signal(2,handler); /* restore the interrupt handler */ itemp = status >> 8; Restorestack(); return(inewint(itemp)); /* return its status */ }