1

Okay what the heck am I doing wrong? I am doing this on Ubuntu and I want this to take the system command "ls" and a parameter such as "-a" and then have the child execute it and then the parent just print something out. I can't understand why I keep getting "Parent" returned twice. Any ideas?

#include <stdio.h> #include <sys/types.h> #include <time.h> #include <stdlib.h> #include <sys/time.h> #include <unistd.h> #include <sys/wait.h> void Cprocess(char *commands, char *scommands[]); void Pprocess(void); void main(int argc, char *argv[]) { char *sendcommand[] = {argv[1],argv[2],0}; char *commands = argv[0]; int pid; if((pid=fork()) ==-1) { perror("Error!!\n"); } else if(pid==0) Cprocess(commands, sendcommand); else { wait(0); printf("Parent\n"); } } void Cprocess(char *argv1, char *argv2[]) { execvp(argv1, argv2); exit(19); } 

That wasn't very nice of me here is the command I enter:

./filename ls -a 

Here is my result:

filename1 filename2 filename3 Parent Parent 
2
  • You are telling your child to run command which is argv[0], which is the current program. Presumably, you meant to copy argv[1] rather than argv[0]? Or you could do without the separate command and simply use sendcommand[0] as the first argument to execvp(). I've not tracked why you don't get many copies of the child running the parent again, though. Commented Jan 22, 2017 at 21:59
  • BTW: main() should return int. Commented Jan 22, 2017 at 23:00

1 Answer 1

2

Congratulations: your program pretty much works. I think you've made things a bit more complicated than they need to be by interposing the Cprocess() function; without it, what's going on might be a bit clearer. Allow me to rearrange your program a bit:

void main(int argc, char *argv[]) { int pid = fork(); if (pid ==-1) { perror("Error!!\n"); } else if (pid==0) { char *sendcommand[] = { argv[1], argv[2], 0 }; execvp(argv[0], sendcommand); exit(19); } else { wait(0); printf("Parent\n"); } } 

That's functionally equivalent to your original main(), and I bet you'll see the problem in it right away.

Hint:

Look carefully at the arguments to execvp().

Hint 2:

The first argument to execvp() designates the program to run.

Hint 3:

Are you serious? Put your brain to work, or at least your debugger.

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

3 Comments

Added your code exactly and changed: execvp(argv[0], sendcommand); to execvp(argv[1], sendcommand); and it worked. why did I get two responses? P.S. like the hints!
The argv[0] received by your program among its arguments is, normally, the name of the program itself. If you pass that as the first argument to execvp() then you're telling it to run another copy of the parent program. Note also that the array passed as the second argument to execvp() is expected to follow the same convention -- the first element (at index 0) is expected to be the program name in some form; the actual arguments start at index 1.
Thank you for your quick response and explanation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.