https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66202

            Bug ID: 66202
           Summary: Weird behaviour when fork and printf without newline
                    are being used
           Product: gcc
           Version: 4.9.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: nichlas_severinsen at hotmail dot com
  Target Milestone: ---

Created attachment 35566
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=35566&action=edit
visbug.c and notvisbug.c, described in the post

Hi, I'm rather new so tear me apart. Not really sure what to put on severity.
GCC VERSION: 4.9.2 (Ubuntu 4.9.2-10ubuntu13)
UNAME: Linux 3.19.0-16-generic #16-Ubuntu SMP x86_64 GNU/Linux

I'll try to explain what I've bumped into, but I may be wrong, so please ravage
me if this is my fault.

Attached are two C files (in a .tar.gz because I'm only allowed to add one
attachment..), the only difference between them is that "notvisbug.c" uses the
'\n' character in its printf function calls. The other attachment "visbug.c"
does not.

I've compiled them with "gcc visbug.c -o visbug" and "gcc notvisbug.c -o
notvisbug". (and ran them in a cli with ./name, of course)

What they essentially do (or should do) is create a process tree looking
something like this, using fork(), and each process except main also prints out
who they are; "I am X":

Main
  |
  A
 / \
 B  C
 |
 D

notvisbug.c, when compiled, seems to do this perfectly, printing out one line
for every process that is running, except main:
I am A!
I am B!
I am D!
I am C!
visbug.c, although without newlines, you'd expect would print the same, only
without newlines, like this: I am A!I am B!I am D!I am C!
It does not, instead it prints this: 
I am A!n@Computer:~/dev$ I am A!I am B!I am A!I am B!I am D!I am A!I am C!
(or something similiar, the two first A's seem to repeats itself, but the rest
come and go in different sequences)

I have looked at the assembly code generated with this version of GCC and found
that notvisbug uses callputs while visbug uses callprintf to print the strings,
I also read here http://cs.lmu.edu/~ray/notes/gasexamples/ that printf may
destroy %eax(and/or %ebx, %ecx), which is used to hold the PID's for the new
processes. (tell me if you need to see the assembly files (.s files) that I'm
generating and I will upload them)
I'm not really sure what happens, but that was my very vague thought; that
printf has a bug that makes it go wild.

I'm probably wrong though. Apologies if this is the wrong place to post this.

Thanks for reading, anyway.

Reply via email to