can anybody please explain help me understand this code?
hello guys,
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <errno.h>
void
main(int argc, char *argv[])
{
int status;
char *prog = argv[0];
char *new_cmd = "/bin/echo";
char *cmd_args[] = {"echo", "hello", "world", NULL};
char *cmd_env[] = {NULL};
status = execve(new_cmd, cmd_args, cmd_env);
/*
* execve does not return unless there is an error.
*/
fprintf(stderr, "%s: ", prog);
perror("execve");
exit(1);
}
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.
wonder_laptop
Junior Poster in Training
89 posts since Aug 2007
Reputation Points: 25
Solved Threads: 0
First of all, use CODE tags as explained in the Community Rules you read when you joined.
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <errno.h>
void main(int argc, char *argv[])
// argc=#of parameters
// argv=array of strings for each parameter
{
int status;
char *prog = argv[0];
// first argv string is the program name
// load prog with the pointer to that string
char *new_cmd = "/bin/echo";
// the command to be executed by the O/S
char *cmd_args[] = {"echo", "hello", "world", NULL};
// an array of strings containing the arguments
// to the /bin/echo command:
// bin/echo echo hello world
char *cmd_env[] = {NULL};
// there are no environment parameters needed
status = execve(new_cmd, cmd_args, cmd_env);
// execute the ECHO command with the parameters
/*
* execve does not return unless there is an error.
*/
fprintf(stderr, "%s: ", prog); // output the program name
perror("execve"); // output the error status from the call
exit(1);
}
Also, read this about formatting your code.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
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
wonder_laptop
Junior Poster in Training
89 posts since Aug 2007
Reputation Points: 25
Solved Threads: 0
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);
}
wonder_laptop
Junior Poster in Training
89 posts since Aug 2007
Reputation Points: 25
Solved Threads: 0
wonder_laptop
Junior Poster in Training
89 posts since Aug 2007
Reputation Points: 25
Solved Threads: 0