943,910 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 830
  • C RSS
Mar 6th, 2008
0

can anybody please explain help me understand this code?

Expand Post »
hello guys,
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <errno.h>
  5.  
  6. void
  7. main(int argc, char *argv[])
  8. {
  9. int status;
  10. char *prog = argv[0];
  11. char *new_cmd = "/bin/echo";
  12. char *cmd_args[] = {"echo", "hello", "world", NULL};
  13. char *cmd_env[] = {NULL};
  14. status = execve(new_cmd, cmd_args, cmd_env);
  15. /*
  16. * execve does not return unless there is an error.
  17. */
  18. fprintf(stderr, "%s: ", prog);
  19. perror("execve");
  20. exit(1);
  21. }

A sample run of this program gives the following output:
> ./sys2
hello world
>


what i understood is that this process wants to execute another process.
i googled and find that. execve takes three arguments: a filename to be executed, an array of strings which are the arguments to the filename, and an array of strings which represent the environment for the process.

but i still dont understand how we got this input .

id appreciate it if anybody can explain it line by line.
Last edited by WaltP; Mar 6th, 2008 at 5:18 pm. Reason: Added CODE tags -- you actually typed right over how to use them when you entered this post...
Similar Threads
Reputation Points: 25
Solved Threads: 0
Junior Poster in Training
wonder_laptop is offline Offline
89 posts
since Aug 2007
Mar 6th, 2008
0

Re: can anybody please explain help me understand this code?

First of all, use CODE tags as explained in the Community Rules you read when you joined.


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <errno.h>
  5.  
  6. void main(int argc, char *argv[])
  7. // argc=#of parameters
  8. // argv=array of strings for each parameter
  9. {
  10. int status;
  11. char *prog = argv[0];
  12. // first argv string is the program name
  13. // load prog with the pointer to that string
  14.  
  15. char *new_cmd = "/bin/echo";
  16. // the command to be executed by the O/S
  17.  
  18. char *cmd_args[] = {"echo", "hello", "world", NULL};
  19. // an array of strings containing the arguments
  20. // to the /bin/echo command:
  21. // bin/echo echo hello world
  22.  
  23. char *cmd_env[] = {NULL};
  24. // there are no environment parameters needed
  25.  
  26. status = execve(new_cmd, cmd_args, cmd_env);
  27. // execute the ECHO command with the parameters
  28.  
  29. /*
  30. * execve does not return unless there is an error.
  31. */
  32.  
  33. fprintf(stderr, "%s: ", prog); // output the program name
  34. perror("execve"); // output the error status from the call
  35. exit(1);
  36. }

Also, read this about formatting your code.
Moderator
Reputation Points: 3278
Solved Threads: 892
Posting Sage
WaltP is online now Online
7,732 posts
since May 2006
Mar 6th, 2008
0

Re: can anybody please explain help me understand this code?

Using execve, the code executes the shell command 'echo' and passes to it the arguments "hello" and "world".
This program demonstrates how you can execute command-line commands from inside of a c/c++ program.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <errno.h>
  5.  
  6. void
  7. main(int argc, char *argv[])
  8. {
  9. int status;
  10. char *prog = argv[0]; /* this is what you type to start THIS program (e.g. './a.out') */
  11. char *new_cmd = "/bin/echo"; /* the command that you would like to execute */
  12. char *cmd_args[] = {"echo", "hello", "world", NULL}; /* arguments to pass to the command Note that you must repeat the name of the program, as this is argv[0] */
  13. char *cmd_env[] = {NULL};
  14. status = execve(new_cmd, cmd_args, cmd_env); /* this will execute the program defined above. Note that you pass to it the location of the command, new_cmd, the arguments, cmd_args, and cmd_env, which is NULL */
  15. /*
  16. * Because you did not start another process to run this program, this program is replaced in memory with the program defined above in execve. Should that fail, then this program will continue below. Basically, if the program works correctly, you will never make it down here.
  17. */
  18. fprintf(stderr, "%s: ", prog);
  19. perror("execve");
  20. exit(1);
  21. }

Let me know if you have any qestions.

Thanks,

Nick
Reputation Points: 18
Solved Threads: 4
Junior Poster
stupidenator is offline Offline
192 posts
since Mar 2005
Mar 6th, 2008
0

Re: can anybody please explain help me understand this code?

Thank you so much guys for you assist

i understood very well how this works.

let's say i want to start another process and make it execute another program.

im tryin things now. what i did is i wrote a program called trivial.c all it does is loop 5 times and each time its sleeps for 4 seconds i.e;

for(int i=0; i<5; i++)
{
sleep(time);
}

if i want the program i posted above to execute my trivial.c

the only changes i have to make are these :


char *new_cmd = "/Desktop/trivial" /*suppose i have it on my desktop*/

char *cmd_args[]= {"trivial"}; /*i only pass the name of the program nothing else because it doesnt take any parameters*/

char *cmd_env[] = {NULL};

status = execve(new_cmd, cmd_args, cmd_env);

right?

thanks
Reputation Points: 25
Solved Threads: 0
Junior Poster in Training
wonder_laptop is offline Offline
89 posts
since Aug 2007
Mar 6th, 2008
0

Re: can anybody please explain help me understand this code?

If you want to run it as a separate process, you have to call fork

http://www.csl.mtu.edu/cs4411/www/NO...rk/create.html
Reputation Points: 18
Solved Threads: 4
Junior Poster
stupidenator is offline Offline
192 posts
since Mar 2005
Mar 6th, 2008
0

Re: can anybody please explain help me understand this code?

yes supposed i use fork..will this be correct?

char *new_cmd = "/Desktop/trivial" /*suppose i have it on my desktop*/

char *cmd_args[]= {"trivial"};
char *cmd_env[] = {NULL};

pid_t child;
child==fork ()

if(child==0)
{

execve(new_cmd, cmd_args, cmd_env);
}
Reputation Points: 25
Solved Threads: 0
Junior Poster in Training
wonder_laptop is offline Offline
89 posts
since Aug 2007
Mar 6th, 2008
0

Re: can anybody please explain help me understand this code?

thanks guys
Last edited by wonder_laptop; Mar 6th, 2008 at 8:06 pm.
Reputation Points: 25
Solved Threads: 0
Junior Poster in Training
wonder_laptop is offline Offline
89 posts
since Aug 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Need some help programming an program for school project please help
Next Thread in C Forum Timeline: File copy through a socket.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC