Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pipe-Related System Calls COS 431 University of Maine.

Similar presentations


Presentation on theme: "Pipe-Related System Calls COS 431 University of Maine."— Presentation transcript:

1 Pipe-Related System Calls COS 431 University of Maine

2 Process Management System Calls Fork system call int pid = fork(); Only to create new process Creates exact duplicate of current process except......different return values Exec system call family int s = exec(file,argv,envp); Replaces the current core image with new program No return value -- why not? Main program in C: main(int argc, char *argv[], char *envp[]);

3 Process Management System Calls Fork and exec work together to start new programs:... if (fork() == 0) { /* child process: execute new program */ execve(command, parameters, 0); } else { /* parent continues */... } Different forms of exec: execl(const char *path, const char *arg0,..., const char *argn, char * /* NULL*/); execv(const char *path, char *const argv[]); execle(const char *path, const char *arg0,...,const char *argn, char *const envp[]); execve(const char *path, char *const argv[], char *const envp[]); execlp(const char *file, const char *arg0,...,const char *argn, char * /* NULL */); execvp (const char *file, char *const argv[]);

4 File Management System Calls int n = read(int fd, void *buffer, int nbytes);  fd: file descriptor  buffer: destination  nbytes: how many bytes to read  n: number read, or 0 on EOF  file position is incremented int n = write(int fd, void *buffer, int nbytes);

5 File Management System Calls fd = dup(fd);  Duplicates a file descriptor -- “aliased”  Useful for situation such as: want to write some output to standard out, then to a file, then back to standard out -- all using printf if close stdout, then it’s gone -- can’t get it back! instead: printf(“this goes to stdout\n”); fd = dup(1); /* duplicate standard out */ close(1); /* close stdout */ fdnew = open(“file”,O_WRONLY); /* fdnew = 1! */ … // write stuff here to "stdout" (fd 1) => file close(fdnew); dup(fd);/* restore 1 (stdout) to old dest. */

6 File Management System Calls int s = dup2(oldfd,newfd); printf(“this goes to stdout\n”); dup2(1,3); // make 3 alias for 1 fdnew = open(“file”,O_WRONLY); // fdnew = 4 close(1); // close old standard out dup2(fdnew,1); // make 1 point to file … // write stuff here to "stdout" (fd 1) => file close(fdnew); dup2(3,1); // restore STDOUT to 1 Close(3);

7 File Management System Calls s = pipe(&fd[0]);  Create a pipe  “fd” is a two-element array of int – holds two file descriptors: fd[0] - for reading fd[1] - for writing  Usually idiom is something like: Parent process creates a pipe Parent forks a new process Parent closes read Child closes write  In Solaris (and possibly others): bidirectional -- data written to fd[0] is read from fd[1] and vice versa

8 Aside: Creating Simple Two-Process Pipeline #define STD_INPUT 0 #define STD_OUTPUT 1 #include /* for Solaris */ pipeline(process1, process2) char *process1, *process2; /* program names */ { int fd[2]; pipe(&fd[0]);/*create pipe*/ if (fork() != 0) { /* parent */ close(fd[0]); close(STD_OUTPUT); dup(fd[1]);/* sets stdout to this pipe end */ close(fd[1]); /* don’t need fd anymore */ execl(process1,process1,0); } else { /* child */ close(fd[1]); close(STD_INPUT); dup(fd[0]);/* replace stdin */ close(fd[0]); execl(proces2,process2,0); }


Download ppt "Pipe-Related System Calls COS 431 University of Maine."

Similar presentations


Ads by Google