debugging fork() inside a child

Atomical
Unregistered
 
Post: #1
Does GDB allow you to debug a fork that is inside a child?
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #2
Im sure it does but have NFI how, but you can still printf to stdout. and printf'ing is a really easy way to debug.

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Atomical
Unregistered
 
Post: #3
Are you sure that works? After it gets past http_fetch it doesn't print out anything. In fact the whole program stalls and I get a zombie process.

int child_pid = fork ();

if (child_pid < 0) {
//error
} else {





char *imageBuf;
imagelen = http_fetch(t_url,&imageBuf,NULL,NULL,NULL,hdatabase,hostDatabaseCount);
printf(t_url);
printf("test");

exit(0);
}
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #4
remember child_pid == 0 if your the child, so both processes are doing http_fetch

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Atomical
Unregistered
 
Post: #5
Alright I changed it. It still isn't printf()ing. Are you sure printf() will work in a process that is a fork of a fork?

Quote: int child_pid = fork ();


if ( child_pid == 0 ) {





char *imageBuf;
//imagelen = http_fetch(ihost, hdatabase[idfornow].info,reqs[i].url, &fileBuf, NULL ,NULL,NULL, hdatabase, hostDatabaseCount);
imagelen = http_fetch(t_url,&imageBuf,NULL,NULL,NULL,hdatabase,hostDatabaseCount);
printf(t_url);
printf("test");

if ( imagelen > 0 ) {

write_file (imagefp,imageBuf,imagelen);

free(imageBuf);

imageBuf = NULL;


} else {

close(imagefp);

unlock_file(imagefp);

unlink(t_local);

}

exit(0);

}
Quote this message in a reply
Atomical
Unregistered
 
Post: #6
Okay I found that the pid was 5096. So it was definitely a non zero value and I'm still in the parent of that fork. I don't get why it isn't creating another process. After I step over fork() in GDB it creates a zombie process.
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #7
from running
Code:
int main(int argc, char *argv[]) {
    fork();
    fork();
    
    printf("a\n");
}

I get something like this:
Code:
fax:~/{(){{()}[(())][]}()} ed$ ./c
a
fax:~/{(){{()}[(())][]}()} ed$ a
a
a

fax:~/{(){{()}[(())][]}()} ed$ ./c
a
a
a
fax:~/{(){{()}[(())][]}()} ed$ a

fax:~/{(){{()}[(())][]}()} ed$ ./c
a
a
a
fax:~/{(){{()}[(())][]}()} ed$ a

fax:~/{(){{()}[(())][]}()} ed$ ./c
a
a
a
a
fax:~/{(){{()}[(())][]}()} ed$ ./c
a
fax:~/{(){{()}[(())][]}()} ed$ a
a
a

due I think to the parent finishing before the child, but you do get four prints as expected.

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Atomical
Unregistered
 
Post: #8
I wonder if that could explain why the child isn't executing. I guess I need to use wait().
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  fork() compiled in Tiger doesn't work in Panther? akb825 2 3,135 Oct 24, 2005 10:59 AM
Last Post: akb825