LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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-28-2004, 01:34 PM   #1
ocularbob
Member
 
Registered: Nov 2002
Location: brooklyn NYC
Distribution: gentoo
Posts: 212

Rep: Reputation: 30
C -how do i execute linux commands?


how can run other programs from within my program?
I don't want to wait for the program that i call to exit before my C program goes on to the next thing.

thanks
 
Old 02-28-2004, 01:45 PM   #2
chewysplace
Member
 
Registered: Sep 2003
Distribution: Slackware 10 w/ Kernel 2.6.8
Posts: 176

Rep: Reputation: 30
use "system("<your commands here");"
 
Old 02-28-2004, 01:53 PM   #3
ocularbob
Member
 
Registered: Nov 2002
Location: brooklyn NYC
Distribution: gentoo
Posts: 212

Original Poster
Rep: Reputation: 30
cool thanks.
 
Old 02-28-2004, 02:17 PM   #4
ocularbob
Member
 
Registered: Nov 2002
Location: brooklyn NYC
Distribution: gentoo
Posts: 212

Original Poster
Rep: Reputation: 30
when i run a command with system() the command runs fine but i get
the following message.

shell-init: could not get current directory: getcwd: cannot access parent directories: No such file or directory

any idea what i need to do to fix this?
 
Old 02-28-2004, 02:24 PM   #5
dford
Member
 
Registered: May 2003
Location: Kansas
Distribution: RH 9, OpenBSD, FreeBSD
Posts: 60

Rep: Reputation: 19
You might want to read the man page for system. It indicates issues with trying to use system in a privileged enviroment: i.e. suid, sgid or root.
 
Old 02-28-2004, 03:00 PM   #6
chewysplace
Member
 
Registered: Sep 2003
Distribution: Slackware 10 w/ Kernel 2.6.8
Posts: 176

Rep: Reputation: 30
my bad i forgot to mention that, thnx dford.
 
Old 02-28-2004, 10:42 PM   #7
haobaba1
Member
 
Registered: Jul 2003
Location: VA Tech
Distribution: Mandrake 9.1
Posts: 73

Rep: Reputation: 15
you can either start a new process or you can start a new thread other wise execution will not continue until after the system call returns. Creating a new thread uses much less resources than creating a new process.

fork creates a new process here is a simple example.
***************************************
int forks=100;

for(i=0;i<mat_dat[3];i++)
{
if(forks>0)
{
pipe(&ps[i].pp[0]);
pid=fork();
forks--;
if(pid==0)
{
sprintf(fdstr,"%d",ps[i].pp[1]);
sprintf(col,"%d",i);
execl("./vecmult","vecmult", f1_name.c_str(), f2_name.c_str(), col, fdstr, NULL);
}
}
else
{
printf("created fork bomb");
return;
}
}

Here is a simple example using pthreads.
antelope, giraffe, cheetah, lion are are functions that take a void * parameter and return void *.
****************************************************

list_t<pthread_t*> thr_list;/*used to keep track of threads*/
pthread_t* p_thrp;

p_thrp=new pthread_t;/*create a new thread*/
thr_list.insert(p_thrp);/*add new thread to list*/

...
...

/*switch the animal type and then create correct thread type*/
switch(a_type){
case 'A': rc=pthread_create(p_thrp,NULL,antelope,(void*)_num);
break;
case 'G': rc=pthread_create(p_thrp,NULL,giraffe,(void*)a_num);
break;
case 'C': rc=pthread_create(p_thrp,NULL,cheetah,(void*)a_num);
break;
case 'L': rc=pthread_create(p_thrp,NULL,lion,(void*)a_num);
break;
default: break;
} /*end switch*/
if(rc){
printf("pthread_create returned with an error\n");
exit(-1);
}
...
...
pthread_exit(NULL); /*edit*this makes the parent wait for all children before exiting.*/

Last edited by haobaba1; 02-28-2004 at 10:44 PM.
 
Old 02-29-2004, 02:51 PM   #8
ocularbob
Member
 
Registered: Nov 2002
Location: brooklyn NYC
Distribution: gentoo
Posts: 212

Original Poster
Rep: Reputation: 30
thats great thanks alot
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
how can I execute two commands on exec of a find? eantoranz Programming 3 08-03-2010 05:51 PM
cannot execute some commands as an plain user! minike Slackware 5 09-03-2004 07:34 PM
how can i make commands execute when I turn on linux? versaulis Linux - Software 1 08-29-2004 07:51 PM
how can execute some commands when a user logs out rddreamz Programming 2 05-25-2004 04:00 PM
how to execute commands on login k4zau Linux - Software 3 10-12-2003 02:52 PM


All times are GMT -5. The time now is 05:03 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration