LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-20-2008, 11:45 PM   #1
Mohamed Mydeen A
LQ Newbie
 
Registered: Dec 2007
Posts: 5

Rep: Reputation: 0
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
 
Old 02-21-2008, 01:39 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Please use code tags to ease readability.

You should try the far simpler "system" function.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux alternative milles21 Linux - Software 2 08-18-2007 10:00 PM
problems with windows programs in linux (QEMU, WINE, or linux alternative) bulldogzerofive Linux - Software 2 08-18-2005 05:49 PM
Alternative Linux distros? duke4e Linux - Distributions 8 08-12-2005 05:44 AM
Is there something like ShellExecute in linux?? cppkid Programming 4 08-06-2004 02:15 AM
ShellExecute C Canadian_2k2 Linux - Software 7 03-03-2003 07:28 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration