can anybody please explain help me understand this code?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2007
Posts: 63
Reputation: wonder_laptop is an unknown quantity at this point 
Solved Threads: 0
wonder_laptop wonder_laptop is offline Offline
Junior Poster in Training

can anybody please explain help me understand this code?

 
0
  #1
Mar 6th, 2008
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...
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

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

 
0
  #2
Mar 6th, 2008
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 192
Reputation: stupidenator is an unknown quantity at this point 
Solved Threads: 4
stupidenator's Avatar
stupidenator stupidenator is offline Offline
Junior Poster

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

 
0
  #3
Mar 6th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 63
Reputation: wonder_laptop is an unknown quantity at this point 
Solved Threads: 0
wonder_laptop wonder_laptop is offline Offline
Junior Poster in Training

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

 
0
  #4
Mar 6th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 192
Reputation: stupidenator is an unknown quantity at this point 
Solved Threads: 4
stupidenator's Avatar
stupidenator stupidenator is offline Offline
Junior Poster

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

 
0
  #5
Mar 6th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 63
Reputation: wonder_laptop is an unknown quantity at this point 
Solved Threads: 0
wonder_laptop wonder_laptop is offline Offline
Junior Poster in Training

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

 
0
  #6
Mar 6th, 2008
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);
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 63
Reputation: wonder_laptop is an unknown quantity at this point 
Solved Threads: 0
wonder_laptop wonder_laptop is offline Offline
Junior Poster in Training

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

 
0
  #7
Mar 6th, 2008
thanks guys
Last edited by wonder_laptop; Mar 6th, 2008 at 8:06 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC