Re: concurrent calls to system(const char *)



anthony.geay@xxxxxxxxx wrote:

I'm trying to launch concurrently in a same process in C++ code,
several independant shell scripts by calling
"int system(const char *)". Look at the bottom code.
When I launch the code, randomly it bugs with the following message
"/bin/sh: bad interpreter: file busy" (if it does not bug on your
computer you can increase
...
Should somebody have any idea of the reason it bugs randomly ?
...

void *f(void *st)
{
std::ostringstream st1;
st1 << "yacs" << *((int *) st);
mkdir(st1.str().c_str(), 00777);
...
int main()
{
pthread_t threadId[NB_OF_THREADS];
for(int i=0;i<NB_OF_THREADS;i++)
{
int *smth=new int(i);
pthread_create(threadId+i,0,f,smth);
}

This has a rather large memory leak, in that you never free all those
integers. Fortunately, you don't NEED to allocate new memory for these.
The context parameter is declared as a pointer, but that doesn't mean you
have to pass the address of something. You can just pass the thread number
directly:

void *f( void * st )
{
std::ostringstream st1;
st1 << "yacs" << (int)st;
mkdir(st1.str().c_str(), 0777);
...
for(int i=0; i < NB_OF_THREADS; i++ )
{
pthread_create(threadId+i, 0, (void*)i, smth );
...

Why do you do the open/close/open thing?
--
Tim Roberts, timr@xxxxxxxxx
Providenza & Boekelheide, Inc.
.



Relevant Pages