By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,218 Members | 1,257 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,218 IT Pros & Developers. It's quick & easy.

How do I debug this sample

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
# 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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.