LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C -how do i execute linux commands? (https://www.linuxquestions.org/questions/programming-9/c-how-do-i-execute-linux-commands-151584/)

ocularbob 02-28-2004 12:34 PM

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

chewysplace 02-28-2004 12:45 PM

use "system("<your commands here");"

ocularbob 02-28-2004 12:53 PM

cool thanks.

ocularbob 02-28-2004 01:17 PM

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?

dford 02-28-2004 01:24 PM

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.

chewysplace 02-28-2004 02:00 PM

my bad i forgot to mention that, thnx dford.

haobaba1 02-28-2004 09:42 PM

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.*/

ocularbob 02-29-2004 01:51 PM

thats great thanks alot


All times are GMT -5. The time now is 10:44 PM.