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 |
use "system("<your commands here");"
|
cool thanks.
|
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? |
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.
|
my bad i forgot to mention that, thnx dford.
|
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.*/ |
thats great thanks alot
|
All times are GMT -5. The time now is 10:44 PM. |