0

I need to implement ps -auxj, grep "user id", and wc. I already have word count, but I'm not sure how to do the others while they have parameters. This is what I have so far.

int main() {
int pfd[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("wc", "wc", (char *) 0);
    perror("wc failed");
    exit(-3);
} 
else {
    close(pfd[0]);
    dup2(pfd[1], 1);
    close(pfd[1]);
    execlp("ls", "ls", (char *) 0);
    perror("ls failed");
    exit(-4);
}
exit(0);

}

Any help in the right direction would be great.

1
  • Have you looked at man 3 exec yet? It should explain everything you need to know about argument passing using the exec* functions. Commented Oct 29, 2013 at 23:41

1 Answer 1

3

exec gives details about how to pass arguments to the exec family of functions. e.g.

execl("ls", "ls", "-l",(char *) 0);

you can choose from there whatever suits you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.