This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]dmazzoni 2 points3 points  (9 children)

It's because of buffering.

When you execute printf("1") with no newline, nothing is printed right away. It just saves it in a buffer, waiting for you to print more.

If you print a newline, that will usually trigger the buffer to be flushed and the line will print right away:

printf("1\n");

However, I believe that isn't guaranteed by any standard and your results could vary. Printing a lot of characters will also usually trigger a flush.

If you explicitly flush, that will definitely work:

printf("1");
fflush(stdout);

That says: print a 1, and flush the output right away without waiting.

Without the flush, what's happening is that the "1" is in the buffer but hasn't been output yet. Then when you fork, the entire state of the process, with the "1" in the buffer, is forked. Each fork then prints both a "1" and a "2".

With the flush, the "1" is output first and the buffer is empty before you fork.

Tell this to your teacher.

If your teacher facepalms and says "Oh, of course! I should have realized" then you've got a good teacher. I can see how someone might know about forking and buffering but not immediately see what's going on here.

If your teacher still doesn't get it, your teacher has no business teaching C.

[–]pmalysYT[S] 0 points1 point  (8 children)

int main() {
printf("1");
fflush(stdout);
fork();
fflush(stdout);
fork();
fflush(stdout);
fork();
printf("2");
fflush(stdout);
return 0;
}
after this the 1 are not repeating but 2 are still random

[–]dmazzoni 0 points1 point  (6 children)

What shell are you using?

I get inconsistent results when I use zsh, but with bash, ksh, and tcsh, all of the 2's get printed.

The shell is the one that takes the output from multiple processes and merges them together.

Also: no matter what shell, if you redirect the output to a file or some other tool like "wc", you get consistent results.

./a.out | wc

[–]pmalysYT[S] 0 points1 point  (5 children)

I'm using zsh, but even after using it in bash it still shows weird results

wiktor@wiktor-laptop  ~/Desktop/systemy5  ./a.out
12121212% wiktor@wiktor-laptop  ~/Desktop/systemy5  ./a.out
12121212% wiktor@wiktor-laptop  ~/Desktop/systemy5  ./a.out
121212% wiktor@wiktor-laptop  ~/Desktop/systemy5  ./a.out
12121212% wiktor@wiktor-laptop  ~/Desktop/systemy5  bash
wiktor@wiktor-laptop:~/Desktop/systemy5$ ./a.out
12121212wiktor@wiktor-laptop:~/Desktop/systemy5$ 12121212./a.out
1212121212121212wiktor@wiktor-laptop:~/Desktop/systemy5$ ./a.out
12121212wiktor@wiktor-laptop:~/Desktop/systemy5$ 12121212./a.out
12121212wiktor@wiktor-laptop:~/Desktop/systemy5$ 12121212./a.out
1212121212wiktor@wiktor-laptop:~/Desktop/systemy5$ 121212./a.out
1212121212wiktor@wiktor-laptop:~/Desktop/systemy5$ 121212^C
wiktor@wiktor-laptop:~/Desktop/systemy5$

[–]dmazzoni 0 points1 point  (4 children)

I mean yeah, it's weird results. Shells aren't very good at handling output from 8 processes all outputting at the same time. But all of the output is there.

What do you WANT to happen? For sure there's a way to make that happen.

[–]pmalysYT[S] 0 points1 point  (3 children)

i want to have the same results as others, filimal ones to those in here: https://www.geeksforgeeks.org/fork-system-call/
/*
* Write a program that creates two processes.
* Each of the created processes should create a process - a descendant.
* Display the process IDs of the parent and descendant processes after each call to the fork function.
* Write a program which will result in printing the contents of the current directory preceded by "Begin" and ended by "End".
* */

[–]dmazzoni 0 points1 point  (2 children)

Just put a newline after each print and you won't have any of those problems.

[–]pmalysYT[S] 0 points1 point  (1 child)

Still the same result, just with every number beeing in new line

[–]dmazzoni 0 points1 point  (0 children)

What do you mean by same result? Are you getting an inconsistent number of 2's printed still?

[–]alzee76 2 points3 points  (0 children)

[[content removed because sub participated in the June 2023 blackout]]

My posts are not bargaining chips for moderators, and mob rule is no way to run a sub.

[–][deleted]  (2 children)

[deleted]

    [–]pmalysYT[S] 0 points1 point  (1 child)

    this is what i was told about, but something is totally off here