1

I am using pthread_create() to create a thread and inside that thread i am using fork+execlp to load a new script.

But the problem is some time fork() call is fine but it is not executing the execlp call. So i have several process with the parent name is running and that is the reason some of the scripts are getting missed.

For example: If from my main program should execute 4 scripts.

I create 4 threads, and inside that i use fork+execlp to execute the scripts.

But when i see what are the scripts it is running, it only shows 3 scripts and one process iwth the parent name.

Can you please let me know what is the best way to deal with this situation?

user2769995
  • 19
  • 1
  • 4
  • You realize that `fork` only creates one single thread in the child process? – Kerrek SB Sep 11 '13 at 19:24
  • Usually its not a good idea to mix multiple threads and multiple process. I would suggest checking if you can re-design it using either _one_ of "multiple threads" or "multiple process". In your code above, are you using any locks, semaphores ? Check answers on this one: http://stackoverflow.com/questions/18666877/synchronisation-in-forked-multithreaded-process – bladeWalker Sep 11 '13 at 19:53
  • Hi Spa, Thanks for you reply. No i am not using any locks. I used a global array of structure with 3 field. 1 task name,task status and the task output. I have initialized the task name in the main process. when a new task is running i am checking the running task name with the global array and then updating the corresponding array with the status of the running task and the output of that task. So i don't need any lock. And my application is designed to run only 25 scripts. So i am creating 25 threads and inside that i forking and executing 25 scripts. but some script that did not execute – user2769995 Sep 11 '13 at 20:15
  • What's the underlying logic behind calling `pthread_create` and right after it calling `fork` ? Can you provide some code to show us what exactly you do after `pthread_create`? –  Sep 11 '13 at 22:10
  • Hi Dariusz, I have the requirment of running around 25 scripts. Currently we are managing with fork+exec and waiting for the child to finish. So all we do it on the main process so the main process was also waiting to get the child status(script) or their output. Suppose if some script is taking time to finish(say 2 min) then the main process is waiting for around 50 mins. So now i tried to implement in threading for that from the main process i am creating 25 threads and each thread will do fork+exec and corresponding thread which fork the scripts will wait for its exit status or output. – user2769995 Sep 12 '13 at 12:27

2 Answers2

1

It sounds like the execlp() call might be failing. If it succeeds, it never returns, so any return from execlp() indicates failure. Immediately after the call, you should call perror("execlp") to show the error, then _exit(1); to have the new child process exit.

caf
  • 233,326
  • 40
  • 323
  • 462
0

When combining multiple threads with fork() make sure that you only execute only one fork in parallel. The pthreads method to do this is to lock a mutex with pthread_mutex_lock().

In the child, you will only have one thread -- the one that called fork(). Don't do anything but call async-signal-safe calls after the fork and before the exec.

See this SO question for more information.

Community
  • 1
  • 1
fork2execve
  • 1,561
  • 11
  • 16