LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-15-2009, 03:06 AM   #1
charlitos
Member
 
Registered: Feb 2009
Posts: 51

Rep: Reputation: 16
How to debug fork()


Im writing a program which is doing some multitasking by using fork(), but I have no clue of how to debug it so that I can see what the child process is doing, I can only keep track of the parent process which is communicating with the child through a pipe, it also has a line to wait for the child process to finish, but I never see whats happening with fork() == 0. I know its a different process running somewhere else in memory but how can keep track of both of them ? I dont know how I would do it since both processes will be using the same variable names...

The only thing I get from the child(while my debugger is running the parent process) is its response, but i want to know how it decides to send me that response...

Last edited by charlitos; 03-15-2009 at 03:11 AM.
 
Old 03-15-2009, 04:04 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
"print" statement (in this case with own PID) is still a _major_ debugging tool.

To the same extent, log files whose names are based on PID also are.
 
Old 03-15-2009, 06:13 AM   #3
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
If you're running Linux kernel 2.6, gdb will give you complete control. Go here for details.

(This also applies if you're running HP-UX 11.x or later.)

If you're running Linux kernel 2.4 or something else, I'll quote from the above link, and insert my comment in red:
Quote:
However, if you want to debug the child process there is a workaround which isn't too painful. Put a call to sleep in the code which the child process executes after the fork. It may be useful to sleep only if a certain environment variable is set, or a certain file exists, so that the delay need not occur when you don't want to run GDB on the child. While the child is sleeping, use the ps program to get its process ID. Or, just before the call to sleep, printf("%d\n",getpid());. Then tell GDB (a new invocation of GDB if you are also debugging the parent process) to attach to the child process (see section 4.7 Debugging an Already-running Process). From that point on you can debug the child process just like any other process which you attached to.
Remember that if you're running Linux 2.6, you can avoid the previous quotation and get complete control.

If you're going to be doing stuff like this, it would do you good to browse through the complete online gdb documentation. If you use that documentation frequently, as I do, you might wish to download it from here (html tarball) or here (other formats).

Hope this helps.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
using fork() culin Programming 12 01-17-2007 10:59 PM
How to debug without having debug section in an executable ? unclesam Linux - Newbie 0 02-02-2006 06:23 AM
Help me, how to debug this? lmmix Programming 1 03-03-2005 07:21 AM
fork beginner_84 Programming 2 08-20-2004 04:53 AM
[debug]what does the following debug information mean icoming Programming 21 06-08-2004 02:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration