| | |
can anybody please explain help me understand this code?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2007
Posts: 63
Reputation:
Solved Threads: 0
hello guys,
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.
c Syntax (Toggle Plain Text)
#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.
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...
First of all, use CODE tags as explained in the Community Rules you read when you joined.
Also, read this about formatting your code.
c Syntax (Toggle Plain Text)
#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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
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.
Let me know if you have any qestions.
Thanks,
Nick
This program demonstrates how you can execute command-line commands from inside of a c/c++ program.
C Syntax (Toggle Plain Text)
#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
•
•
Join Date: Aug 2007
Posts: 63
Reputation:
Solved Threads: 0
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

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
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
http://www.csl.mtu.edu/cs4411/www/NO...rk/create.html
![]() |
Similar Threads
- fstream Tutorial (C++)
- learning php (PHP)
- Giving code to posters rather than helping them (IT Professionals' Lounge)
- Trying to understand this code??? (C)
- Singly-Linked Lists problem (C++)
- hlp me in dis code!! (C++)
- How do i debug this app? (Java)
- code explanation required (C)
- Need Help counting Array Length (C++)
Other Threads in the C Forum
- Previous Thread: Need some help programming an program for school project please help
- Next Thread: File copy through a socket.
| Thread Tools | Search this Thread |
adobe api array arrays bash binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators initialization intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue multi mysql oddnumber odf open opendocumentformat opensource openwebfoundation pattern pdf performance pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition scanf scheduling scripting segmentationfault send shape socketprograming socketprogramming stack standard strchr string strings suggestions test testautomation unix urboc user variable voidmain() win32api windows.h






