How do I debug this sample

V

Vittal

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
 
J

Jens.Toerring

Vittal said:
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
--
_ _____ _____
| ||_ _||_ _| (e-mail address removed)-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
 
D

Derk Gwen

# statement and <<after that sample directly comes out>>. But if I run the

What does that mean?
 
V

Vittal

Derk Gwen said:
# 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)
 
J

Jens.Toerring

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
--
_ _____ _____
| ||_ _||_ _| (e-mail address removed)-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,093
Messages
2,570,608
Members
47,228
Latest member
ValentinCh

Latest Threads

Top