Alternative for ShellExecute in linux
Hi,
ShellExecute is used to call one application (a file.exe) in Windows. But I want to call the application in a same manner but in Linux. Is there any command available ?
Can I use fork() with execv() together? I have tried this method but I am facing problem in this. Can anybody please tell anyway to do this ?
I got this following code from the net.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int foo(char *adr[])
{
pid_t pid;
//printf ("%s\t", adr[0]);
//printf ("%s", **adr);
pid=fork();
if (pid==0)
{
if (execv("/home/vlc",adr)<0)
return -1;
else
return 1;
}
else if(pid>0)
return 2;
else
return 0;
}
int main(int argc,char *argv[])
{
if (foo(argv)<=0)
perror("foo");
return 0;
}
This is very exactly working to call the application. But I need to give this in as a command line argument.
Actually I want to work in the following way with fork() and execv().
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
int foo1(char *adr[])
{
execv("/home/client_server/vlc",adr);
// pid_t pid;
//printf ("%s\t", adr[0]);
//printf ("%s", **adr);
// pid=fork();
}
int foo2(char *adr[])
{ // if (pid==0)
//{
execv("/home/vlc",adr);
//return -1;
//else
//return 1;
//}
//else if(pid>0)
// return 2;
//else
// return 0;
}
void forkfun()
{
int x = 1;
pid_t pid = fork();
if (pid == 0) {
foo1(argv);
printf("Child has x = %d\n", ++x);
// foo1(argv);
} else {
printf("Parent has x = %d\n", --x);
foo2(argv);
}
printf("Bye from process %d with x = %d\n", getpid(), x);
}
int main(int argc, char *argv[])
{
forkfun();
int option = 0;
// if (argc > 1)
// option = atoi(argv[1]);
foo1(argv);
foo2(argv);
//perror("foo");
return 0;
}
This gives the error. Actually as far as to my application, I can only one time supply arguments in command line. If I call execv to run the application then I need to go with fork. But the above situation is needed one. But I am getting error.Once the program is started, I cannot come back and my application name in command line argument. So kindly tell me the way to do it.
Thanks & Regards,
Mohamed Mydeeen A
|