wait3 breaks printf for floats?

C

Chris

(uname -a: Linux srv343.revere.com 2.4.20-30.9 #1 Wed Feb 4 20:44:26
EST 2004 i686 i686 i386 GNU/Linux)

I'm pulling my hair out over here...

I have a program that forks off a child, and uses wait3 to (1) wait
for the child to finish, and to (2) collect and report system usage
info.

Now, in the process of narrowing stuff down, I've come to the
following two lines of code that illustrate the problem:

wait3(&status,0,usage);
printf("...hi...\n\n");
printf(">>> %f \n\n", 67.8);

In theory, the 2nd line should simply print out the number
67.800000... Nothing programmatic or complex going on. However, when
I compile (which gives no errors/warnings) and run, I get this output:

hi....
Segmentation fault


Now, if I comment out the wait3 line (and make no other change
whatsoever), a compile and run gives me:

hi....
....and things seem to be working ok. Also, if I do:

printf(">>> %d",67)

....I get >>> 67 (valid output), with wait3 and without wait3.

So, can anyone think of a reason why printf would smash like this?

Any help is greatly appreciated... Thanks!
 
K

Kieran Simkin

Chris said:
(uname -a: Linux srv343.revere.com 2.4.20-30.9 #1 Wed Feb 4 20:44:26
EST 2004 i686 i686 i386 GNU/Linux)

I'm pulling my hair out over here...

I have a program that forks off a child, and uses wait3 to (1) wait
for the child to finish, and to (2) collect and report system usage
info.

Now, in the process of narrowing stuff down, I've come to the
following two lines of code that illustrate the problem:

1) Off topic (but I'll answer your question anyway)
2) Not enough of your code to diagnose the problem
wait3(&status,0,usage);
printf("...hi...\n\n");
printf(">>> %f \n\n", 67.8);

In theory, the 2nd line should simply print out the number
67.800000... Nothing programmatic or complex going on. However, when
I compile (which gives no errors/warnings) and run, I get this output:

hi....
Segmentation fault

I'm guessing you've either defined "usage" as a pointer to a struct rusage
and assigned no space for it, or you've defined "usage" as a struct rusage
and are passing it (by value) to wait3(). wait3() expects as its third
argument a pointer to a struct rusage. This means you have two possible
options:

int status;
struct rusage *usage;
usage=malloc(sizeof *usage);
wait3(&status,0,usage);
free(usage);

or:

int status;
struct rusage usage;
wait3(&status,0,&usage);

Now, if I comment out the wait3 line (and make no other change
whatsoever), a compile and run gives me:

hi....

...and things seem to be working ok. Also, if I do:

printf(">>> %d",67)

...I get >>> 67 (valid output), with wait3 and without wait3.

You have obfuscated the matter, printf has nothing to do with it.
 
J

John

Ah... Yep, you were right. It's the night bearing down on me.

And I agree, this was OT. Shoulda been in comp.unix.programmer. Thanks for
the help, just the same!
 
D

Dan Pop

In said:
1) Off topic (but I'll answer your question anyway)

Not really. Imagine that wait3 was spelled as foo.
2) Not enough of your code to diagnose the problem

That's true. But enough code for an educated guess, even for the
non-Unix programmer who has no clue about what wait3 might be: this
function was incorrectly called, by someone fool enough not to use the
full diagnosing capabilities of his compiler.

Dan
 

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

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top