/** * Start a process * @return: PcbPtr of process / NULL if start failed */ PcbPtr startPcb(PcbPtr ps) { pid_t pid; /* if the process is suspended, restart */ if(ps->status == PCB_SUSPENDED) { if (kill(ps->pid, SIGCONT)) { fprintf(stderr, "Restart failed\n"); return NULL; } ps->status = PCB_RUNNING; return ps; } /* otherwise, start Pcb */ switch(pid = fork()) { case -1: fprintf(stderr, "Starting failed\n"); return NULL; break; case 0: // in child(new) process ps->pid = getpid(); ps->status = PCB_RUNNING; printPcb(ps); execvp(ps->args[0], ps->args); break; default: // in parent ps->pid = pid; kill(ps->pid, SIGCONT); ps->status = PCB_RUNNING; break; } return ps; }
/******************************************************* * PcbPtr startPcb(PcbPtr process) - start (or restart) * a process * returns: * PcbPtr of process * NULL if start (restart) failed ******************************************************/ PcbPtr startPcb (PcbPtr p) { if (p->pid == 0) { // not yet started switch (p->pid = fork ()) { // so start it case -1: perror ("startPcb"); exit(1); case 0: // child p->pid = getpid(); p->status = PCB_RUNNING; printPcbHdr(stdout); // printout in child to printPcb(p, stdout); // sync with o/p fflush(stdout); execvp (p->args[0], p->args); perror (p->args[0]); exit (2); } // parent } else { // already started & suspended so continue kill (p->pid, SIGCONT); } p->status = PCB_RUNNING; return p; }