473,499 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I debug this sample

Hello,

Here I have a simple C program. But I am not understanding how I can
debug this?

#include <stdio.h>
main()
{
int pid=0;
pid=fork();
if(pid)
printf("In parent process\n");
else
printf("In child process\n");
}
While running the executable in dbx mode I see the first printf
statement and after that sample directly comes out. But if I run the
executable without dbx I see both the printf statements.

Can somebody help me in this.

Thanks
-Vallabha
Nov 13 '05 #1
4 1806
Vittal <vs*********@yahoo.com> wrote:
Here I have a simple C program. But I am not understanding how I can
debug this? #include <stdio.h>
main()
{
int pid=0;
pid=fork();
if(pid)
printf("In parent process\n");
else
printf("In child process\n");
} While running the executable in dbx mode I see the first printf
statement and after that sample directly comes out. But if I run the
executable without dbx I see both the printf statements.


Sorry, but this is off-topic in comp.lang.c - the C standard never
talks about fork() or having more than one process running at once.
You'll be better served in e.g. comp.unix.programmer where UNIX
specific extensions like fork() to the C language are discussed.
The only standard C specific problem of your code is that you
don't declare main() as returning int as you ought to (at least
if you want to avoid trouble with C99 compilers) and that you
forgot to return an int from main() (which you must, because
even if you don't specify a return type for main() under C89 it
defaults to int).

<OT>
First, you need to include also <unistd.h> for the prototype of
fork(). It's also recommended to use the 'pid_t' type for PIDs.
And, second, your program is working exactly as it is supposed
to work, you spawn a second process which prints one line while
the parent process prints the other one. If you run the whole
thing under a debugger it might grab the terminal so only the
process you're running under the control of the debugger may be
able to get its message printed. Hopefully, the documentation
for your debugger will give you all the gory details.
</OT>
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| Je***********@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
Nov 13 '05 #2
# statement and <<after that sample directly comes out>>. But if I run the

What does that mean?

--
Derk Gwen http://derkgwen.250free.com/html/index.html
Don't say anything. Especially you.
Nov 13 '05 #3
Derk Gwen <de******@HotPOP.com> wrote in message news:<vq************@corp.supernews.com>...
# statement and <<after that sample directly comes out>>. But if I run the

What does that mean?


When I run the executable in dbx mode, here is what is see. It never
entered the child process. I can see the debug message "In parent
process", but never I see "In child process"

[chandram_two_view] Vallabha@brahma> dbx a.out
Reading a.out
Reading ld.so.1
Reading libc.so.1
Reading libdl.so.1
Reading libc_psr.so.1
(dbx) stop in main
(2) stop in main
(dbx) run
Running: a.out
(process id 8528)
stopped in main at line 5 in file "fork1.c"
5 int pid=0;
(dbx) next
stopped in main at line 6 in file "fork1.c"
6 pid=fork();
(dbx) next
In child process
stopped in main at line 7 in file "fork1.c"
7 if(pid)
(dbx) next
stopped in main at line 8 in file "fork1.c"
8 printf("In parent process\n");
(dbx) next
In parent process
stopped in main at line 11 in file "fork1.c"
11 }
(dbx) next

execution completed, exit code is 1
(dbx)
Nov 13 '05 #4
Vittal <vs*********@yahoo.com> wrote:
Derk Gwen <de******@HotPOP.com> wrote in message news:<vq************@corp.supernews.com>...
# statement and <<after that sample directly comes out>>. But if I run the

What does that mean?
When I run the executable in dbx mode, here is what is see. It never
entered the child process. I can see the debug message "In parent
process", but never I see "In child process"
Have another look: the "In child process" line is printed out!
[chandram_two_view] Vallabha@brahma> dbx a.out
Reading a.out
Reading ld.so.1
Reading libc.so.1
Reading libdl.so.1
Reading libc_psr.so.1
(dbx) stop in main
(2) stop in main
(dbx) run
Running: a.out
(process id 8528)
stopped in main at line 5 in file "fork1.c"
5 int pid=0;
(dbx) next
stopped in main at line 6 in file "fork1.c"
6 pid=fork();
(dbx) next
In child process <-------- !!!!!
stopped in main at line 7 in file "fork1.c"
7 if(pid)
(dbx) next
stopped in main at line 8 in file "fork1.c"
8 printf("In parent process\n");
(dbx) next
In parent process
stopped in main at line 11 in file "fork1.c"
11 }
(dbx) next execution completed, exit code is 1
(dbx)


The debugger controls only one process, unless you manage to tell it
otherwise it follows the parent process. The child process is running
"uncontrolled", which you can easily see from its output that got
interspersed with the output of the debugger.

If you still have problems with this please post in comp.unix.programmer
where it would be on-topic.
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| Je***********@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2836
by: John Smith | last post by:
Sorry if this is the wrong forum... Does anyone know what the difference is between a debug build and an optimised debug build in Visual Studio 2003?
4
4507
by: emma middlebrook | last post by:
I have a question regarding asserting ... here's some code: string GetAssertMessage() { ... prepare a message string and return it... } void SomeMethod() { ...
8
1624
by: Steve Teeples | last post by:
I keep getting the error below. ---------------- An unhandled exception of type 'System.NullReferenceException' occurred in Unknown Module. Additional information: Object reference not set to...
7
18775
by: Thomas Pecha | last post by:
Sorry for all who think this is easy, I was not able to handle this Coming from VB6 where with simple debug.print strAString you could write to debug window, I am totalling failing in vb.net...
1
1714
by: Rich | last post by:
Hello, I downloaded a sample asp.net app (in C#). When I try to run it I get the following error message" "Error while trying to run project: Unable to start debugging on the web server. ...
6
9115
by: Andrew Rowley | last post by:
I am having trouble getting debug and release builds to work properly with project references using C++ .NET and Visual Studio 2003. I created a test solution, with a basic Windows form C++...
0
268
by: Jon Davis | last post by:
(Ignore my previous post, had an erroneous sample.) In order to gracefully handle exceptions at runtime but cause the debugger to break in the place where the exceptions occur when debugging, I...
0
1776
by: BA | last post by:
I posted on this once before and could not get a solution, I am hoping someone can help. I have a very strange code debug behavior that I cannot make heads or tails of: I have c# code being...
0
1484
by: Kim | last post by:
Im generally new to ASP, so bear with me. This is not ASP (dot) NET. I have a query which gives results when doing the it directly, but none (maybe) when done in asp. "Maybe" because I can not...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7174
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7388
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...
0
4600
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...
0
3099
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...
0
3091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.