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?
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.
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.
Related
I want to use system() to execute a cmd.
If the cmd takes a long time like ping, it will block main thread.
So I want to create a child thread to handle it. In this child thread the system() will be invoked.
As we all know, the system() will fork a child process.
I'm NOT sure whether any problem or side effect when the system() is invoked in a child thread.
system() is marked as thread-safe by Linux/glibc documentation. See the man page. But with a caveat:
According to POSIX.1, it is unspecified whether handlers registered
using pthread_atfork(3) are called during the execution of system().
In the glibc implementation, such handlers are not called.
From what you described, you probably don't have atfork handlers, so it's fine.
I want to use system() to execute a cmd. If the cmd takes a long time like ping, it will block main thread.
However, if the sole purpose of using a separate thread is to run a command, why not fork(2) directly and let it child process exec command?
The main process can still carry on and can check whether the child process completed (e.g., using waitpid with WNOHANG).
This would be cleaner in my opinion and avoids the fork+threads complexity.
I am looking for C code to use on a Linux based system to start another process asynchronously. The second process should continue, even if the first ends. I've looked through the "fork" and "system" and "exec" options, but don't see anything that will spawn a peer process that's not communicating with or a child of the original process.
Can this be done?
Certainly you can. In the parent fork() a child, and in that child first call daemon() (which is an easy way to avoid setsid etc.), then call something from the exec family.
In Linux (and Unix), every process is created by an existing process. You may be able to create a process using fork and then, kill the parent process. This way, the child will be an orphan but still, it gets adopted by init. If you want to create a process that is not inherited by another, I am afraid that may not be possible.
You do a fork (man 2 fork) followed by an execl (man 2 execl)
For creates a new process of the same image as the calling process (so a perfect twin), where execl replaces one of the twins with a new image.
If you search google for "fork execl" you will find many text book examples -- including how to use correctly fork() and exec()
The most common fork-execl you will still have the new process associated to the terminal -- to create a perfect background process you need to create what is called a daemon process -- the template for that can be fornd in this answer here Creating a daemon in Linux
I am working on a multithreaded process that forks to execute another process. Sometimes, the fork may error if the execution file does not exist. Since this process has multiple threads running prior to fork I have a couple questions:
Are threads copied over to the forked process.
What is the best practice to handling an error from fork with a multithreaded process. For example:
/* in a multithreaded process */
pid = fork();
if(pid == 0)
{
execlp(filename, filename, NULL);
fprintf(stderr, "filename doesn't exist");
/* what do i do here if there's multiple threads running
from the fork? */
exit(-1);
}
Well, the fork doesn't error if the executable file doesn't exist. The exec errors in that case. But, to your actual question, POSIX states that fork creates a new process with a single thread, a copy of the thread that called fork. See here for details:
A process shall be created with a single thread. If a multi-threaded process calls fork(), the new process shall contain a replica of the calling thread and its entire address space, possibly including the states of mutexes and other resources.
Consequently, to avoid errors, the child process may only execute async-signal-safe operations until such time as one of the exec functions is called.
So what you have is okay, if a little sparse :-)
A single thread will be running in the child and, if you cannot exec another program, log a message and exit.
And, in the rationale section, it explains why it was done that way:
There are two reasons why POSIX programmers call fork(). One reason is to create a new thread of control within the same program (which was originally only possible in POSIX by creating a new process); the other is to create a new process running a different program. In the latter case, the call to fork() is soon followed by a call to one of the exec functions.
The general problem with making fork() work in a multi-threaded world is what to do with all of the threads. There are two alternatives. One is to copy all of the threads into the new process. This causes the programmer or implementation to deal with threads that are suspended on system calls or that might be about to execute system calls that should not be executed in the new process. The other alternative is to copy only the thread that calls fork(). This creates the difficulty that the state of process-local resources is usually held in process memory. If a thread that is not calling fork() holds a resource, that resource is never released in the child process because the thread whose job it is to release the resource does not exist in the child process.
When a programmer is writing a multi-threaded program, the first described use of fork(), creating new threads in the same program, is provided by the pthread_create() function. The fork() function is thus used only to run new programs, and the effects of calling functions that require certain resources between the call to fork() and the call to an exec function are undefined.
I want to use gdb for looking into the various details of the fork() system call. To do this, I used one breakpoint at the fork() and from there onwards i am using step command but this way it is not working fine.
Can somebody explain me how to use gdb to look into every single step occuring during fork() system call?
Maybe you meant that you want to follow the child process instead of the parent once the fork is called? In that case:
If you want to follow the child
process instead of the parent process,
use the command set follow-fork-mode.
set follow-fork-mode mode
Set the debugger response to a program call of fork or vfork. A call
to fork or vfork creates a new
process. The mode argument can be:
parent: The original process is debugged after a fork. The child
process runs unimpeded. This is the
default.
child: The new process is debugged after a fork. The parent process runs
unimpeded.
If you want to see whats happening best if to look at the kernel code first, check it here.
I don't think you can single step through kernel from user space. You can use virtual server to do the debugging using KGDB. Check blog post here. Or you can use KGDB on main kernel.
Just wondering how if it's possible to execute another program in a thread and send information to/get information from it. Essentially the same concept as with a child process and using pipes to communicate - however I don't want to use fork.
I can't seem to find whether it's possible to do this, any help would be appreciated.
Thanks
You cannot use the exec family of functions to load another executable file within a thread; the exec functions replace the entire process with the process started from the executable. Thus fork() is necessary if you want your original process to keep running.
In theory you could replicate most of the behaviour of the exec system call in userspace, and run an executable within a thread - but as the thread would share the open file table, signal handlers and so on with the rest of the process, it would likely destructively interfere with the main process. It would also be a lot of work.
If you're not using fork (directly or indirectly), then it's not really another process. Of course, you can communicate between threads within a process. That's essential to most multithreading.