c++ call linux commands forks childs - c++

using linux commands in my c++ program. Trying to download a url from a list of arrays, once one download finishes, the next to start.... currently only downloads the first location in the array, then stops.
Does somebody see the error i'm doing?
// Begin the downloading process
pid_t child = 0;
child = fork();
if ( child < 0)
{
cout << "Process Faileld to Fork<<endl;
return 1;
}
if (child == 0)
{
wait(NULL);
}
else
{
for(int i = 0; i < numberOfDownloads; i++)
{
execl("/usr/bin/wget", "wget",locations[i], NULL);
}
}
trying to download something with the command wget, but i get an error

execl replaces the currently running process by the started process. So your loop body is only run once. You should put the fork() inside the loop, assuming you want to fork off numberOfDownloads instances of wget. (but be careful with such things)
Otherwise, use system(), which will not terminate your process but return to your loop after wget exits. Or popen(), if you need more control over the process and want to read wget output, without having to do all the I/O heavylifting yourself.

Related

Creating new processes with fork() vs exec()

#1 code
int child_pid;
for (int i = 0; i < 3; i++)
{
child_pid = fork();
if (child_pid == 0)
{ sleep(10); }
else
{ wait(&status); }
}
execlp("sleep","sleep","20",(char*)NULL);
#2 code
int child_pid;
for (int i = 0; i < 5; i++)
{
child_pid = fork();
if (child_pid == 0)
{ sleep(1); }
else
{ wait(&status);
execlp("sleep","sleep","20",(char*)NULL); }
}
Can someone explain to me what is happening in these codes?
I understand fork() returns a zero to a newly created child process. That's why I don't understand why in #2 code the else branch is also being executed? Execlp replaces the calling process image with a new process image, but what does this really mean?
EDIT:
The answer is that the first code will create 8 processes, but I'm not able to understand how? And why will the first code finish 230 seconds? How does exec affect this?
The second code will create 5 processes, but it will finish after (5*2sec+5*20sec). In the second code why does it go to the else branch?
I'm trying to understand how many processes are created as well as why and how long the code will sleep?
Thanks in advance.
fork will return two times, one in the original process, with the return value of the pid of the child process, the other in the child process, with the return value of zero
DESCRIPTION
fork() creates a new process by duplicating the calling process. The new process is referred to as the child process. The calling process is referred to as the parent process.
and:
RETURN VALUE
On success, the PID of the child process is returned in the parent, and 0 is returned in the child. On failure, -1 is returned in the parent, no child process is created, and errno is set appropriately.
So, after to fork(), you have your newly created process, so you have 2 process, the child (the new process) and the parent (the initial process).
So, what your child (pid == 0) is going into the sleep when you parent (pid > 0) is executing your sleep.
After doing an exec* (execl, execlp, execv, execve, etc...) your process is terminated (try to place a printf or whatever AFTER your execlp, you will see it will never print it (except in case of error (execlp's return value == -1)).
To answer to your initial question:
Yes, your program pass in your if AND in your else (put printf to see clearer), because you have in fact 2 process, and you can even try to put a printf after the else statement, at the very end of your program, and you will see that the child pass (print his pid, it should be equal to 0).

parent process and child process timing

Hi I have a simple question, however the timing issue is troubling me. Assume this is the code.
#include <stdio.h>
int main() {
int p = fork();
if (p==0) {
printf("ok\n");
sleep(1);
} else {
printf("hey!");
sleep(1);
}
printf("done!");
return 0;
}
My question is, will "done!" always be executed twice when the sleep is 1sec for both parent and child. Because I notice that when I increase the sleep to 10 seconds in the child process (p==0 case), I only see "done!" once.
I think when you increase sleep time parent process exited faster and stdout file descriptor closed. note that child and parent process shared their file descriptors.
if you want you can use _exit() in your parent process so when it exited, child process file descriptors will not be closed. in this way after 10 sec you see "done!" in your terminal. for use of this method you must use printf("done!\n") to flush your buffer manually because _exit() did not flush your buffer.
If you want you can use something like wait() in your parent process to issue wait on your child process.

execute multiple processes from a master process

I want to create multiple processes from one master process. I know I want to use a function from the exec family, but it does not seem to be preforming in the way I intended it to. It seems that exec() is a blocking call, or maybe I am just using it wrong. Anyway, on to the code:
const char* ROUTERLOCATION = "../../router";
int main(int argc, char** argv) {
manager manager;
vector<string> instructions = manager.readFile(argv[1]);
...
//file gives me the number of proceses i want to spawn and that value goes in
//rCount
for(int i = 0; i < rCount; i++){
cout << "creating:" << i << endl;
execl(ROUTERLOCATION, "",NULL);
}
}
The output I see is:
creating:0
HI!!!
And then everything exits gracefully. Can I not spawn more than one process using execl()?
Also, I would like to communicate with each of these processes, so I don't want to be blocking while these processes are running.
You need to fork in your master process, the in your child processes call execl. (exec family of functions replaces your current process image with your new process, so hence why your for loop never completes.)
calling exec() means that your current program not longer exists. You might want to create a new process using fork() and then call exec() in it so that exec() replaces your new process and your main process still works as you intend it to.
example:
pid_t pid = fork();
if (pid == 0) {// child
execl();
} else { // parent
}

C++ fork() and execv() problems

I am kind of newbie on C++, and working on a simple program on Linux which is supposed to invoke another program in the same directory and get the output of the invoked program without showing output of the invoked program on console. This is the code snippet that I am working on:
pid_t pid;
cout<<"General sentance:"<<endl<<sentence<<endl;
cout<<"==============================="<<endl;
//int i=system("./Satzoo");
if(pid=fork()<0)
cout<<"Process could not be created..."<<endl;
else
{
cout<<pid<<endl;
execv("./Satzoo",NULL);
}
cout<<"General sentance:"<<endl<<sentence<<endl;
cout<<"==============================="<<endl;
One of the problem I encounter is that I am able to print the first two lines on console but I cant print the last two lines. I think the program stops working when I invoke the Satzoo program.
Another thing is that this code invokes Satzoo program twice, I dont know why? I can see the output on screen twice. On the other hand if I use system() instead of execv(), then the Satzoo works only once.
I haven't figured out how to read the output of Satzoo in my program.
Any help is appreciated.
Thanks
You aren't distinguisng between the child and the parent process after the call to fork(). So both the child and the parent run execv() and thus their respective process images are replaced.
You want something more like:
pid_t pid;
printf("before fork\n");
if((pid = fork()) < 0)
{
printf("an error occurred while forking\n");
}
else if(pid == 0)
{
/* this is the child */
printf("the child's pid is: %d\n", getpid());
execv("./Satzoo",NULL);
printf("if this line is printed then execv failed\n");
}
else
{
/* this is the parent */
printf("parent continues execution\n");
}
The fork() function clones the current process and returns different values in each process. In the "parent" process, it returns the pid of the child. In the child process, it returns zero. So you would normally invoke it using a model like this:
if (fork() > 0) {
cout << "in parent" << endl;
} else {
cout << "in child" << endl;
exit(0);
}
I have omitted error handling in the above.
In your example, both of the above code paths (both parent and child) fall into the else clause of your call to fork(), causing both of them to execv("./Satzoo"). That is why your program runs twice, and why you never reach the statements beyond that.
Instead of using fork() and doing everything manually (properly managing process execution is a fair amount of work), you may be interested in using the popen() function instead:
FILE *in = popen("./Satzoo", "r");
// use "in" like a normal stdio FILE to read the output of Satzoo
pclose(in);
From the fork() manpage:
RETURN VALUE
Upon successful completion, fork() shall return 0 to the child process and shall return the process ID of the child process to the parent process. Both processes shall continue to execute from the fork() function. Otherwise, -1 shall be returned to the parent process, no child process shall be created, and errno shall be set to indicate the error.
You check to make sure it succeeds, but not whether the pid indicates we're in the child or the parent. Thus, both the child and the parent do the same thing twice, which means that your program gets executed twice and the ending text is never printed. You need to check the return value of fork() more than just once.
exec - The exec() family of functions replaces the current process image with a new process image.
system - Blocks on execution of the command. Execution of the calling program continues after the system command returns
There are three return value tests you want with fork
0: you are the child
-1: error
other: you are the parent
You ran the other program from both the child and the parent...

How do I write a program that tells when my other program ends? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How do I write a program that tells when my other program ends?
The only way to do a waitpid() or waitid() on a program that isn't spawned by yourself is to become its parent by ptrace'ing it.
Here is an example of how to use ptrace on a posix operating system to temporarily become another processes parent, and then wait until that program exits. As a side effect you can also get the exit code, and the signal that caused that program to exit.:
#include <sys/ptrace.h>
#include <errno.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
int main(int argc, char** argv) {
int pid = atoi(argv[1]);
int status;
siginfo_t si;
switch (ptrace(PTRACE_ATTACH, pid, NULL)) {
case 0:
break;
case -ESRCH:
case -EPERM:
return 0;
default:
fprintf(stderr, "Failed to attach child\n");
return 1;
}
if (pid != wait(&status)) {
fprintf(stderr, "wrong wait signal\n");
return 1;
}
if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP)) {
/* The pid might not be running */
if (!kill(pid, 0)) {
fprintf(stderr, "SIGSTOP didn't stop child\n");
return 1;
} else {
return 0;
}
}
if (ptrace(PTRACE_CONT, pid, 0, 0)) {
fprintf(stderr, "Failed to restart child\n");
return 1;
}
while (1) {
if (waitid(P_PID, pid, &si, WSTOPPED | WEXITED)) {
// an error occurred.
if (errno == ECHILD)
return 0;
return 1;
}
errno = 0;
if (si.si_code & (CLD_STOPPED | CLD_TRAPPED)) {
/* If the child gets stopped, we have to PTRACE_CONT it
* this will happen when the child has a child that exits.
**/
if (ptrace(PTRACE_CONT, pid, 1, si.si_status)) {
if (errno == ENOSYS) {
/* Wow, we're stuffed. Stop and return */
return 0;
}
}
continue;
}
if (si.si_code & (CLD_EXITED | CLD_KILLED | CLD_DUMPED)) {
return si.si_status;
}
// Fall through to exiting.
return 1;
}
}
On Windows, a technique I've used is to create a global named object (such as a mutex with CreateMutex), and then have the monitoring program open that same named mutex and wait for it (with WaitForSingleObject). As soon as the first program exits, the second program obtains the mutex and knows that the first program exited.
On Unix, a usual way to solve this is to have the first program write its pid (getpid()) to a file. A second program can monitor this pid (using kill(pid, 0)) to see whether the first program is gone yet. This method is subject to race conditions and there are undoubtedly better ways to solve it.
If you want to spawn another process, and then do nothing while it runs, then most higher-level languages already have built-ins for doing this. In Perl, for example, there's both system and backticks for running processes and waiting for them to finish, and modules such as IPC::System::Simple for making it easier to figure how the program terminated, and whether you're happy or sad about that having happened. Using a language feature that handles everything for you is way easier than trying to do it yourself.
If you're on a Unix-flavoured system, then the termination of a process that you've forked will generate a SIGCHLD signal. This means your program can do other things your child process is running.
Catching the SIGCHLD signal varies depending upon your language. In Perl, you set a signal handler like so:
use POSIX qw(:sys_wait_h);
sub child_handler {
while ((my $child = waitpid(-1, WNOHANG)) > 0) {
# We've caught a process dying, its PID is now in $child.
# The exit value and other information is in $?
}
$SIG{CHLD} \&child_handler; # SysV systems clear handlers when called,
# so we need to re-instate it.
}
# This establishes our handler.
$SIG{CHLD} = \&child_handler;
There's almost certainly modules on the CPAN that do a better job than the sample code above. You can use waitpid with a specific process ID (rather than -1 for all), and without WNOHANG if you want to have your program sleep until the other process has completed.
Be aware that while you're inside a signal handler, all sorts of weird things can happen. Another signal may come in (hence we use a while loop, to catch all dead processes), and depending upon your language, you may be part-way through another operation!
If you're using Perl on Windows, then you can use the Win32::Process module to spawn a process, and call ->Wait on the resulting object to wait for it to die. I'm not familiar with all the guts of Win32::Process, but you should be able to wait for a length of 0 (or 1 for a single millisecond) to check to see if a process is dead yet.
In other languages and environments, your mileage may vary. Please make sure that when your other process dies you check to see how it dies. Having a sub-process die because a user killed it usually requires a different response than it exiting because it successfully finished its task.
All the best,
Paul
Are you on Windows ? If so, the following should solve the problem - you need to pass the process ID:
bool WaitForProcessExit( DWORD _dwPID )
{
HANDLE hProc = NULL;
bool bReturn = false;
hProc = OpenProcess(SYNCHRONIZE, FALSE, _dwPID);
if(hProc != NULL)
{
if ( WAIT_OBJECT_0 == WaitForSingleObject(hProc, INFINITE) )
{
bReturn = true;
}
}
CloseHandle(hProc) ;
}
return bReturn;
}
Note: This is a blocking function. If you want non-blocking then you'll need to change the INFINITE to a smaller value and call it in a loop (probably keeping the hProc handle open to avoid reopening on a different process of the same PID).
Also, I've not had time to test this piece of source code, but I lifted it from an app of mine which does work.
Most operating systems its generally the same kind of thing....
you record the process ID of the program in question and just monitor it by querying the actives processes periodically
In windows at least, you can trigger off events to do it...
Umm you can't, this is an impossible task given the nature of it.
Let's say you have a program foo that takes as input another program foo-sub.
Foo {
func Stops(foo_sub) { run foo_sub; return 1; }
}
The problem with this all be it rather simplistic design is that quite simply if foo-sub is a program that never ends, foo itself never ends. There is no way to tell from the outside if foo-sub or foo is what is causing the program to stop and what determines if your program simply takes a century to run?
Essentially this is one of the questions that a computer can't answer. For a more complete overview, Wikipedia has an article on this.
This is called the "halting problem" and is not solvable.
See http://en.wikipedia.org/wiki/Halting_problem
If you want analyze one program without execution than it's unsolvable problem.