/* * Spawn a single command. */ void Spawn_Single_Command(struct Process procList[], int nproc, const char *path) { int pid; if (nproc > 1) { Print("Error: pipes not supported yet\n"); return; } if (procList[0].flags & (INFILE|OUTFILE)) { Print("Error: I/O redirection not supported yet\n"); return; } Print("program:%s\ncommand%s\npath%s\n", procList[0].program, procList[0].command, path ); pid = Spawn_With_Path(procList[0].program, procList[0].command, path); if (pid < 0) Print("Could not spawn process: %s\n", Get_Error_String(pid)); else { int exitCode = Wait(pid); if (exitCodes) Print("Exit code was %d\n", exitCode); } }
int tStatDirectory() { int fd, retS, retC; struct VFS_File_Stat s; retC = Create_Directory("/d/basic10d"); if (retC < 0) { Print("couldn't create basic10d: %d %s\n", retC, Get_Error_String(retC)); return -1; } fd = Open_Directory("/d/basic10d"); if (fd < 0) { Print("couldn't reopen basic10d: %d %s\n", fd, Get_Error_String(fd)); return -1; } retS = FStat(fd, &s); if (Close(fd) < 0) { Print("failed to close"); return -1; } (void)Delete("/d/basic10d", false); if (retS < 0) { Print("couldn't fstat opened basic10d: %d %s\n", retS, Get_Error_String(retS)); } if (!s.isDirectory) { Print("fstat didn't think basic10d was a directory\n"); } return ((retS >= 0) && (s.isDirectory)) ? 1 : -1; }
int main(int argc, char *argv[]) { int rc; if (argc != 2) { Print("Usage: mkdir <directory>\n"); Exit(1); } rc = Create_Directory(argv[1]); if (rc != 0) Print("Could not create directory: %s\n", Get_Error_String(rc)); return !(rc == 0); }
/* * Spawn a single command. */ void Spawn_Single_Command(struct Process *proc, const char *path, int background) { int pid; pid = proc->pid = Spawn_With_Path(proc->program, proc->command, path, background); if (pid < 0) Print("Could not spawn process: %s\n", Get_Error_String(pid)); else { int exitCode = Wait(pid); if (exitCodes) Print("Exit code was %d\n", exitCode); } }
int main(int argc, char *argv[]) { int rc; if (argc != 2) { Print("Usage: mkfile <file>\n"); Exit(1); } rc = Open(argv[1], O_CREATE); if (rc < 0) Print("Could not create file: No%d %s\n", rc, Get_Error_String(rc)); Close(rc); return rc; }
int tBasicReadWrite() { int fd, retW, retR; char buffer[100]; memset(buffer, 'g', 100); fd = Open("/d/basic", O_CREATE | O_WRITE); if (fd < 0) return -1; retW = Write(fd, buffer, 10); if (retW < 0) { Print("write return %d < 10\n", retW); return -1; } if (Close(fd) < 0) { Print("failed to close"); return -1; } fd = Open("/d/basic", O_READ); if (fd < 0) { Print("couldn't reopen for reading: %d %s\n", fd, Get_Error_String(fd)); return -1; } retR = Read(fd, buffer, 10); if (retR < 0) { Print("read return %d < 10\n", retR); return -1; } if (Close(fd) < 0) { Print("failed to close"); return -1; } (void)Delete("/d/basic", false); return ((retW == 10) || (retR == 10)) ? 1 : -1; }
int main(int argc, char **argv) { int n = 0; int child_pid = 0; Print("original\n"); child_pid = Fork(); n++; global ++; if(child_pid > 0) { Print("parent n=%d, global=%d, child_pid=%d, my_pid=%d\n", n, global, child_pid, Get_PID()); } else if(child_pid == 0) { Print("child n=%d, global=%d, child_pid=%d, my_pid=%d\n", n, global, child_pid, Get_PID()); } else { Print("fork failed: %s (%d)\n", Get_Error_String(child_pid), child_pid); } return 0; }
/* * Spawn a pipeline. */ void Spawn_Pipeline(struct Process procList[], int nproc, const char *path) { int i; int numSpawned, exitCode=0; bool err = false; struct Process *prev; /* * Spawn all processes in the pipeline, opening input/output files * and pipes as needed. */ for (i = 0, numSpawned = 0, prev = 0; !err && i < nproc; ++i, ++numSpawned) { struct Process *proc = &procList[i]; int readfd = 0, writefd = 1; proc->readfd = proc->writefd = proc->pipefd = proc->pid = -1; /* Open input file */ if (proc->flags & INFILE) { readfd = Open(proc->infile, O_READ); if (readfd < 0) { Print("Could not open file: %s\n", Get_Error_String(readfd)); err = true; } } if (!err) { /* Open output file or pipe */ if (proc->flags & OUTFILE) { writefd = Open(proc->outfile, O_WRITE|O_CREATE); if (writefd < 0) { Print("Could not open file: %s\n", Get_Error_String(writefd)); err = true; } } else if (proc->flags & PIPE) { int rc = Create_Pipe(&proc->pipefd, &writefd); if (rc != 0) { Print("Could not create pipe: %s\n", Get_Error_String(rc)); err = true; } } } /* Is output being piped from previous process? */ if (!err && prev != 0 && (prev->flags & PIPE)) { readfd = prev->pipefd; } proc->readfd = readfd; proc->writefd = writefd; if (!err) { proc->pid = Spawn_With_Path(proc->program, proc->command, proc->readfd, proc->writefd, path); if (proc->pid < 0) { Print("Could not spawn process: %s\n", Get_Error_String(proc->pid)); err = true; } } prev = proc; } /* * Close all pipes and I/O redirection file descriptors * (except the shell's stdin and stdout). * This won't affect the child processes because they have * their own Cloned versions of the File objects. */ for (i = 0, prev = 0; i < numSpawned; ++i) { struct Process *proc = &procList[i]; if (proc->readfd > 1) Close(proc->readfd); if (proc->writefd > 1) Close(proc->writefd); if (i == numSpawned - 1 && (proc->flags & PIPE)) { /* * Process meant to read from this pipe was never spawned. * The read fd for the pipe is stored in the "pipefd" field. */ Close(proc->pipefd); } } /* * Wait for all processes to exit. */ for (i = 0; i < numSpawned; ++i) { struct Process *proc = &procList[i]; if (proc->pid >= 0) exitCode = Wait(proc->pid); } if (numSpawned > 0 && exitCodes) Print("Exit code was %d\n", exitCode); }
int tReadEntry() { int fd, retR, retC; struct VFS_Dir_Entry dirEntry; retC = Create_Directory("/d/basic11d"); if (retC < 0) { Print("couldn't create basic11d: %d %s\n", retC, Get_Error_String(retC)); return -1; } retC = Create_Directory("/d/basic11d/d1"); if (retC < 0) { Print("couldn't create basic11d/d1: %d %s\n", retC, Get_Error_String(retC)); return -1; } retC = Create_Directory("/d/basic11d/d2"); if (retC < 0) { Print("couldn't create basic11d/d2: %d %s\n", retC, Get_Error_String(retC)); return -1; } fd = Open("/d/basic11d/f1", O_CREATE); if (fd < 0) { Print("couldn't open basic11d/f1: %d %s\n", fd, Get_Error_String(fd)); return -1; } if (Close(fd) < 0) { Print("failed to close"); return -1; } fd = Open_Directory("/d/basic11d"); if (fd < 0) { Print("couldn't opendir basic11d: %d %s\n", fd, Get_Error_String(fd)); return -1; } retR = Read_Entry(fd, &dirEntry); if ((retR < 0) || (strncmp(dirEntry.name, "d1", 2) != 0) || (!dirEntry.stats.isDirectory)) return -1; retR = Read_Entry(fd, &dirEntry); if ((retR < 0) || (strncmp(dirEntry.name, "d2", 2) != 0) || (!dirEntry.stats.isDirectory)) return -1; retR = Read_Entry(fd, &dirEntry); if ((retR < 0) || (strncmp(dirEntry.name, "f1", 2) != 0) || (dirEntry.stats.isDirectory)) return -1; if (Close(fd) < 0) { Print("failed to close"); return -1; } fd = Open_Directory("/d/basic11d"); if (fd < 0) return -1; // no retR = Seek(fd, 2); // no if (retR < 0) // no return -1; // no retR = Read_Entry(fd, &dirEntry); // no if ((retR < 0) || // no (strncmp(dirEntry.name, "f1", 2) != 0) || // no (dirEntry.stats.isDirectory)) // no return -1; if (Close(fd) < 0) { Print("failed to close"); return -1; } (void)Delete("/d/basic11d/d1", false); (void)Delete("/d/basic11d/d2", false); (void)Delete("/d/basic11d/f1", false); (void)Delete("/d/basic11d", false); return 1; }
static void Print_Error(const char *msg, int fd) { Print("%s: %s\n", msg, Get_Error_String(fd)); Exit(1); }