473,624 Members | 2,577 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

trouble with fork() and exec()

15 New Member
Hi all,
I'm writing a manager application (Linux/C++) that'll spawn 2 instances of a program (myApp). The myApp program needs to stay alive after being spawned (both instances) so a 3rd party program could connect to them thru socket. My problem is at the fork and exec function which I'm not really familiar with yet. Could anyone help to point me in the right direction or maybe enlighten me with the correct usage of fork and exec.
Thanks and regards.

Here's the main part of my manager application (that's supposed to spawn 2 instances of myApp)
Expand|Select|Wrap|Line Numbers
  1. -------------------------------------------------------------------------------
  2. int main(int argc, char **argv)
  3. {
  4.     int  buff;
  5.     char msg[MAXLINE];
  6.     int result;
  7.     int count = 0;
  8.     do{
  9.         printf("\n1) To bring up myApp: instance 1\n");        
  10.         printf("2) To bring up myApp: instance 2\n");        
  11.         printf("Input choice or 0 to exit:");        
  12.  
  13.         scanf("%d", &buff);
  14.  
  15.         switch(buff){
  16.         case 1:
  17.             if(fork())
  18.                 wait(0);
  19.             else
  20.             {            
  21.                 result = execlp("../experiment/myApp","myApp", "1", "8050", (char *)NULL);
  22.                 perror("Result1:");
  23.             }
  24.             break;
  25.         case 2:
  26.             if(fork())
  27.                 wait(0);
  28.             else
  29.             {
  30.                 result = execlp("../experiment/myApp","myApp", "2", "8051", (char *)NULL);
  31.                 perror("Result2:");
  32.             }
  33.             break;
  34.         case 0:
  35.             break;
  36.         }
  37.     }while(buff != 0);
  38.  
  39.     return 0;
  40. }
Mar 18 '09 #1
5 3384
gpraghuram
1,275 Recognized Expert Top Contributor
What u are doing is not OK.
See this book which has a example program

Raghu
Mar 19 '09 #2
boddah
15 New Member
Thanks gpraghuram. I'll go thru the PDF file and update here again. BTW, do u have any sample of urs which I can refer to as well?
Mar 19 '09 #3
gpraghuram
1,275 Recognized Expert Top Contributor
I dont have any examples now.
The chapter gives a good explanation about how the fork wrks and the example present there is also very helpful

Raghu
Mar 19 '09 #4
boddah
15 New Member
Problem solved. Actually the prob wasn't with my Parent code but it was the other program code which I was trying to spawn. Added a while(1){} to the code and it worked well. Previously the code had exited (thru return 0) thus, resulting the myApp to appear <defunct> when spawned.
Thanks Raghu and cheers....
Mar 19 '09 #5
boddah
15 New Member
Here's my fork & exec code:

Expand|Select|Wrap|Line Numbers
  1. , "Fork failed.\n");
  2.         log_messaint spawn(int b, int a)
  3. {
  4.     int result;
  5.     char msg[MAXLINE];
  6.     pid_t mypid;
  7.  
  8.     mypid = fork();
  9.  
  10.     if(mypid < 0)
  11.     {
  12.         sprintf(msgge(RM_LOG_FILE, msg);
  13.     }
  14.     else if(mypid == 0)
  15.     {
  16.         printf("I'm the child\n");
  17.         result = execlp("./myApp", "myApp", itoa(b), itoa(a), NULL);
  18.         if(result == -1)
  19.             perror("Execlp failed:\n");
  20.     }
  21.     else
  22.     {
  23.         printf("I'm the parent: %d\n", getpid());
  24.         return mypid;
  25.     }
  26. }
Mar 19 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
5900
by: Benoit Dejean | last post by:
hello, i have a question about forking processes atm, i have some code which i want to rewrite os.system("cd ~ && exec " + cmd + " & disown") i want to remove this os.system call
3
2358
by: Lingyun Yang | last post by:
Hi, I want to use python as a "shell like" program, and execute an external program in it( such as mv, cp, tar, gnuplot) I tried: os.execv("/bin/bash",("/usr/bin/gnuplot",'-c "gnuplot < plot.tmp"')) since it's in a for-loop, it should be executed many times, but It exits after the first time running.
2
1861
by: blah | last post by:
i m using Windows XP, and by tomorrow i will have have fedora core installed too. the problem is, when i use these "fork() and exec()" my windows doesnt do anything, python gives an error about the module, the kind of error when u know u r wrong. is it because these commands work on linux? if so, is it better for me to stick with fedora for my python programs or use windows? --
1
3458
by: Alexander N. Spitzer | last post by:
I am trying to write a program that will fork a process, and execute the given task... the catch is that if it runs too long, I want to clean it up. this seemed pretty straight forward with a single executable being run from the fork. The problem I am having now is that if I call a shell scripts, then lets say calls "xterm &", after the timeout has occurred, I kill the shell script, but the xterm is still running... I cannot seem to kill...
3
2712
by: thrillseekersforever | last post by:
The questions(A&B) are to fine no# of process running from the below codes. However, I couldn't decipher the solution. Will someone please throw some light on this? Thanks a lot!! A] void main() { .... pid1 = fork(); if (pid1) pid2 = fork(); .....
0
1266
by: rahulthathoo | last post by:
Hi, I have to call a perl program from within PHP, but since that program takes a long time to finish, i fork a process in my php code and then call the perl code, using exec. But I am not able to run the perl program, but I think i m not able to fork in the first place, in the sense that I (apache) may not have permissions to fork. Is there any way to remedy this? Rahul
2
2793
by: Rafael Giannetti Viotti | last post by:
Hi, I am working with the subprocess.py module in Python 2.4.4 and I am confused about it's functionality. It uses the standard pipe-fork-exec method to start a subprocess: # create pipes pid = fork()
3
2200
by: Dan Upton | last post by:
I have a Python script that does a fork/exec, so the parent process can get the child's PID and monitor /proc/PID/stat (on a CentOS system). Most of my processes' command lines are straightforward enough to do this with, but I have a handful that use < on the command line, eg ../gobmk_base.linux_x86 --quiet --mode gtp < 13x13.tst The only thing I could really think of to try was
5
2509
by: Jatek | last post by:
Hi i have learned how fork.pl and exec.pl work as a starting point,but i want to create a program called shell.pl that will repeatedly read a line of user inpu until the user enters CTRL-D (end of file). For each line entered, the program should fork a process that will execute the provided command using exec. i want the parent process to use wait to await the completion of the child process. I want the program to handle invalid commands by...
0
8249
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8348
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8493
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7176
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4187
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1493
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.