Solved

fork+exec in c++, linux

Posted on 2010-11-29
12
1,278 Views
Last Modified: 2012-05-10
I have 2 processes (actually working programs) in 2 different directories, I want to spawn the second process from the first.

Now I am running both of them in two different terminals like client and server and able to communicate. I donot know much about child process creation. can some one explain using c++ code. Now I need to start the client in my program or spawn the child from my program (server) in different location using only fork()+ ecec**().

I need both of them need to execute concurrently...and want to see the outputs in 2 tabs of terminals...can some one help me regarding this.

Thanks in advance.
0
Comment
Question by:shragi
  • 4
  • 3
  • 3
  • +2
12 Comments
 
LVL 32

Expert Comment

by:phoffric
ID: 34236182
Here are links (with code) to help you:

http://www.yolinux.com/TUTORIALS/ForkExecProcesses.html
http://www-h.eng.cam.ac.uk/help/tpl/unix/fork.html

Wish I had more time to help but tied up for a couple of days.

Here is a related question that may also help:
     http://www.experts-exchange.com/Software/Development/Management_Debug/Version_Control_CVS/MS_Visual_SourceSafe/Q_26533583.html
0
 
LVL 3

Accepted Solution

by:
chrios earned 63 total points
ID: 34236679
This should get you started I hope.

Fork return 0 in the child process and the childs PID in the parent process.
Exec replaces the child process with the program called.

#include <unistd.h>
#include <iostream>

using std::cout;
using std::endl;

int main(int argv, char** argc)
{
   int p = fork();
   if(p == 0)
   {
       cout << "This is the child" << endl;
       execl("/bin/ls", "/bin/ls", "-l", (char *) 0);
       cout << "If this line is printing, something went wrong with exec" << endl;
   }
   else if(p > 0)
   {
       cout << "This is the parent and the childs pid is " << p << endl;
   }
   else
   {
       // p must be < 0
       cout << "Fork failed" << endl;
   }

   cout << "this would have run by both processes" << endl;
   cout << "unless we used exec in the child process" << endl;
   return 0;
}

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 34236732
>> ...and want to see the outputs in 2 tabs of terminals

That's the tricky bit of your requirement. It is easy to achieve with a simple command (in a shell script eg.). In Gnome eg., you could do :

        gnome-terminal --tab-with-profile=Default -e "program1" --tab-with-profile=Default -e "program2"

where program1 and program2 are your two programs (with path, parameters, etc.).

I wouldn't do something like this in C.
0
 
LVL 12

Expert Comment

by:HappyCactus
ID: 34237570
The second requirement can be achieved with a different way: in the second program, write the output on a named pipe, then spawn (manually or by a script) a terminal with a "cat /named/pipe" command. This is simpler than executing the terminal and the program inside it, in my opinion.

On the other side, pay attention that when you exec the second program by using fork(), the second program is a child of the first; this means that the second program must terminate before the first one, or you'll end with a zombie process.
To prevent this you have two options: 1) make the second (child) program end before the first one, and call waitpid() to cleanup the ended process, or 2) use a double fork()  (+ setsid()) technique to unlink the second process form the first one.

0
 

Author Comment

by:shragi
ID: 34239705
Initially about my first question, For example my executable names are prg1 and prg2. i.e ./prg1 and ./prg2.

Please specify me ./prg2 isvused and where I can specify the exact path. I am  little confused about this line.

execl("/bin/ls", "/bin/ls", "-l", (char *) 0); help me understand the parameters  better.
0
 
LVL 12

Expert Comment

by:HappyCactus
ID: 34239787
The first parameter is the path to the executable, intended as "complete file name of the executable". The second and the following parameters are command line arguments. The first (argv[0]) should be the name of the executable. This list must be terminated by a null pointer. See [1].
So, if you need to execute ./prg2:

execl("./prg2", "./prg2", (char *)0);

[1] http://linux.die.net/man/3/exec
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 62 total points
ID: 34239998
shragi, did you try the command I posted in my previous post ? Does it suit your needs ? If not, what's missing ?

Here's the command again with the ./prg1 and ./prg2 :

        gnome-terminal --tab-with-profile=Default -e "./prg1" --tab-with-profile=Default -e "./prg2"
0
 

Author Comment

by:shragi
ID: 34255396
I want to do something programatically, I hope above script executes the above executables in 2 different terminals.

I dont understand How I can use this  

  gnome-terminal --tab-with-profile=Default -e "./prg1" --tab-with-profile=Default -e "./prg2" in my program.

Sorry If I do not understand that properly.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 34255472
If you are using gnome, try running the command I mentioned in a terminal window. Make sure that the paths to both executables are correct. And it will run both executables in a separate tab of a new terminal window.

Which is exactly what you wanted :)


>> I want to do something programatically

You can place the command in a bash script, and then run the bash script.
0
 

Author Comment

by:shragi
ID: 34258939
I am already using fork() and execl() to run by 2 programs as a parent and child.

I am seeing the outputs in the same terminal, my outputs are pretty large and continues...So I wanted to know How I can separate the output of a child from parent.  

The above bash script will make my 2 executables execute.  But I am already running them in my single program.

If this task is pretty difficult, let me know know how I can store my child output (displayed on the terminal) to store to a file.

Because by output is continues, I though displaying it in a tab of terminal is easy to debug and observe. Otherwise suggest me how I can append the output in a file.
0
 
LVL 12

Expert Comment

by:HappyCactus
ID: 34259067
redirect the output to a named pipe
then (manually or by a script) open a terminal performing "cat named_pipe"
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 34261444
>> The above bash script will make my 2 executables execute.  But I am already running them in my single program.

Right, but you can undo the changes that made them run in a single program, and have two separate programs which you can then easily run in separate tabs.

It's the logical approach.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now