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.

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.

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.

#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]; /* this is what you type to start THIS program (e.g. './a.out') */
char *new_cmd = "/bin/echo"; /* the command that you would like to execute */
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] */
char *cmd_env[] = {NULL};
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 */
/*
* 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.
*/
fprintf(stderr, "%s: ", prog);
perror("execve");
exit(1);
}

Let me know if you have any qestions.

Thanks,

Nick

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

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);
}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.