How to Implement a single program in C that replicates the following Unix command(s): ps -ef | grep YOUR_USER_id | wc -



How to Implement a single program in C that replicates the following Unix command(s): ps -ef | grep YOUR_USER_id | wc -

this question has reply here:

connecting n commands pipes in shell? 2 answers learning pipes, exec, fork, , trying chain 3 processes together 1 reply

my teacher gave practice assignment studying in operating systems class. assignment pipe 3 processes , implement commands in title @ once. allowed utilize these commands when implementing it:

dup2() 1 of exec() fork() pipe() close()

i can pipe 2 don't know how three. either show me how or @ to the lowest degree point me in right direction?

here code far:

#include <stdio.h> #include <stdlib.h> #include <unistd.h> int main() { int pfd[2]; int pfdb[2]; int pid; if (pipe(pfd) == -1) { perror("pipe failed"); exit(-1); } if ((pid = fork()) < 0) { perror("fork failed"); exit(-2); } if (pid == 0) { close(pfd[1]); dup2(pfd[0], 0); close(pfd[0]); execlp("ps", "ps", "-ef", (char *) 0); perror("ps failed"); exit(-3); } else { close(pfd[0]); dup2(pfd[1], 1); close(pfd[1]); execlp("grep", "grep", "darrowr", (char *) 0); perror("grep failed"); exit(-4); } exit(0); }

any help appreciated. heck tutorial on how finish wondrous!

you're going need 3 processes , 2 pipes connect them together. start 1 process, going need 2 fork() calls, 2 pipe() calls, , 3 exec*() calls. have decide of processes initial process end running; either ps or wc. can write code either way, decide before start.

the middle process, grep, going need pipe input , pipe output. create 1 pipe , 1 kid process , have run ps output going pipe; create pipe , kid process , prepare pipes before running grep; original process have both pipes open , close of file descriptors before running wc.

the key thing pipes create sure close plenty file descriptors. if duplicate pipe standard input or standard output, should close both of original file descriptors returned pipe() call; in example, should close both. , 2 pipes, means there 4 descriptors close.

working code

note utilize of error study , exit function; simplifies error reporting enormously. have library of functions different error reports; simple implementation of 1 of functions. (it's overly simple: doesn't include programme name in messages.)

#define _xopen_source 700 #include <stdio.h> #include <stdlib.h> #include <unistd.h> static void err_syserr(const char *fmt, ...); int main(void) { int p1[2]; int p2[2]; pid_t pid1; pid_t pid2; if (pipe(p1) == -1) err_syserr("failed create first pipe"); if ((pid1 = fork()) < 0) err_syserr("failed fork first time"); if (pid1 == 0) { dup2(p1[1], stdout_fileno); close(p1[0]); close(p1[1]); execlp("ps", "ps", "-ef", (char *)0); err_syserr("failed exec 'ps'"); } if (pipe(p2) == -1) err_syserr("failed create sec pipe"); if ((pid2 = fork()) < 0) err_syserr("failed fork sec time"); if (pid2 == 0) { dup2(p1[0], stdin_fileno); close(p1[0]); close(p1[1]); dup2(p2[1], stdout_fileno); close(p2[0]); close(p2[1]); execlp("grep", "grep", "root", (char *)0); err_syserr("failed exec 'grep'"); } else { close(p1[0]); close(p1[1]); dup2(p2[0], stdin_fileno); close(p2[0]); close(p2[1]); execlp("wc", "wc", (char *)0); err_syserr("failed exec 'wc'"); } /*notreached*/ } #include <stdarg.h> #include <errno.h> #include <string.h> static void err_syserr(const char *fmt, ...) { int errnum = errno; va_list args; va_start(args, fmt); vfprintf(stderr, fmt, args); va_end(args); if (errnum != 0) fprintf(stderr, " (%d: %s)", errnum, strerror(errnum)); putc('\n', stderr); exit(exit_failure); }

sample output:

234 2053 18213

my machine rather busy running root-owned programs, seems.

unix grep fork ps wc

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -