473,385 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

1024 bites interleaving frequency???

I made for experiment this simple program:

#include <stdlib.h>

int main (){
int i;
if (fork() != 0){
for (i = 0; i < 100000; i++) printf("|");
} else {
for (i = 0; i < 100000; i++) printf("-");
}
exit (0);
}

I tried to count how many '|' and how many '-' in my screen.
they where almost always blocks of 1024 '|' and 1024 '-'
sometimes longer blocks o shorter ones.. but most frequently blocks of
1024 chars...

do someone has an explanation for this fact??

Giulio
Nov 14 '05 #1
7 1612
In article <co**********@domitilla.aioe.org>,
Giulio <fr*************@email.it.invalid> wrote:
I made for experiment this simple program: if (fork() != 0){ do someone has an explanation for this fact??


fork() is not defined by the C language and therefore beyond the scope
of comp.lang.c .

The folks in comp.unix.programmer might be able to help you out.
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
80% of all questions that begin with the word 'why' can be answered
with the simple sentence 'people are stupid.'
--Shamelessly Stolen From Mike in uw.general
Nov 14 '05 #2
In article <co**********@domitilla.aioe.org>,
Giulio <fr*************@email.it.invalid> wrote:
I tried to count how many '|' and how many '-' in my screen.
they where almost always blocks of 1024 '|' and 1024 '-'
sometimes longer blocks o shorter ones.. but most frequently blocks of
1024 chars...


The standard i/o library buffers its output by default. It then uses
some operating-system function to write out the buffer, and this may
well be an atomic operation with respect to switching between
processes.

-- Richard
Nov 14 '05 #3
>#include <stdlib.h>

int main (){
int i;
if (fork() != 0){
for (i = 0; i < 100000; i++) printf("|");
} else {
for (i = 0; i < 100000; i++) printf("-");
}
exit (0);
}

I tried to count how many '|' and how many '-' in my screen.
they where almost always blocks of 1024 '|' and 1024 '-'
sometimes longer blocks o shorter ones.. but most frequently blocks of
1024 chars...

do someone has an explanation for this fact??


stdio output is often buffered (and since you're not outputting any
newline characters, line buffering turns into block buffering). I
wonder what the block size of the buffering is used in your
implementation? 1024 seems a reasonable choice.

Gordon L. Burditt
Nov 14 '05 #4
Giulio <fr*************@email.it.invalid> wrote in message news:<co**********@domitilla.aioe.org>...
I made for experiment this simple program:

#include <stdlib.h>

int main (){
int i;
if (fork() != 0){
for (i = 0; i < 100000; i++) printf("|");
} else {
for (i = 0; i < 100000; i++) printf("-");
}
exit (0);
}

I tried to count how many '|' and how many '-' in my screen.
they where almost always blocks of 1024 '|' and 1024 '-'
sometimes longer blocks o shorter ones.. but most frequently blocks of
1024 chars...

do someone has an explanation for this fact??


It's actually quite trivial once you realize how printf actually sends
data from your argument to the file handle.

[Hint: think of buffering, think fflush....]

Tom
Nov 14 '05 #5
In <co**********@domitilla.aioe.org> Giulio <fr*************@email.it.invalid> writes:
I made for experiment this simple program:

#include <stdlib.h>

int main (){
int i;
if (fork() != 0){
for (i = 0; i < 100000; i++) printf("|");
} else {
for (i = 0; i < 100000; i++) printf("-");
}
exit (0);
}

I tried to count how many '|' and how many '-' in my screen.
they where almost always blocks of 1024 '|' and 1024 '-'
sometimes longer blocks o shorter ones.. but most frequently blocks of
1024 chars...

do someone has an explanation for this fact??


Your code is broken, so there is little point in trying to explain its
behaviour. Try the following fixed version and play with the setvbuf
call (replace _IOFBF by _IONBF or comment out the setvbuf call) and see
if it makes any difference.

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>

#define SIZE 100000
char buff[SIZE];

int main()
{
int i;

setvbuf(stdout, buff, _IOFBF, sizeof buff);

if (fork() != 0) {
for (i = 0; i < SIZE; i++) putchar('p');
wait(NULL);
printf("\nBUFSIZ = %d\n", BUFSIZ);
}
else for (i = 0; i < SIZE; i++) putchar('c');

return 0;
}

As such, my program is quite likely to generate two compact blocks of
output (one generated by the parent, the other by the child) and then
the final line printed by the parent. But even this is not guaranteed.

Using the default buffering or disabling the stdio buffering is likely
to change the output pattern.

BTW, the issue is semi-topical, because it involves the buffering
performed by the standard C library. The off topic parts are the ones
related to creating a second process and waiting for its termination,
but without them the topical part could not be explored.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #6
Dan Pop wrote:
In <co**********@domitilla.aioe.org> Giulio <fr*************@email.it.invalid> writes:

I made for experiment this simple program:

#include <stdlib.h>

int main (){
int i;
if (fork() != 0){
for (i = 0; i < 100000; i++) printf("|");
} else {
for (i = 0; i < 100000; i++) printf("-");
}
exit (0);
}

I tried to count how many '|' and how many '-' in my screen.
they where almost always blocks of 1024 '|' and 1024 '-'
sometimes longer blocks o shorter ones.. but most frequently blocks of
1024 chars...

do someone has an explanation for this fact??

Your code is broken, so there is little point in trying to explain its
behaviour. Try the following fixed version and play with the setvbuf
call (replace _IOFBF by _IONBF or comment out the setvbuf call) and see
if it makes any difference.

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>

#define SIZE 100000
char buff[SIZE];

int main()
{
int i;

setvbuf(stdout, buff, _IOFBF, sizeof buff);

if (fork() != 0) {
for (i = 0; i < SIZE; i++) putchar('p');
wait(NULL);
printf("\nBUFSIZ = %d\n", BUFSIZ);
}
else for (i = 0; i < SIZE; i++) putchar('c');

return 0;
}

As such, my program is quite likely to generate two compact blocks of
output (one generated by the parent, the other by the child) and then
the final line printed by the parent. But even this is not guaranteed.

Using the default buffering or disabling the stdio buffering is likely
to change the output pattern.

BTW, the issue is semi-topical, because it involves the buffering
performed by the standard C library. The off topic parts are the ones
related to creating a second process and waiting for its termination,
but without them the topical part could not be explored.

Dan


Wouldn't the scheduler be more pertinent here than any type of
buffering, etc.?
Nov 14 '05 #7
In article <co**********@newshost.mot.com>,
Jason Curl <j_****************************@foo.bar> wrote:
Try the following fixed version and play with the setvbuf
call (replace _IOFBF by _IONBF or comment out the setvbuf call) and see
if it makes any difference.

[...]
Wouldn't the scheduler be more pertinent here than any type of
buffering, etc.?


Why not try changing the buffering as Dan suggests, and see for yourself?

-- Richard
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: christopher diggins | last post by:
I would like to survey how widespread the usage of smart pointers in C++ code is today. Any anecdotal experience about the frequency of usage of smart pointer for dynamic allocation in your own...
5
by: Senger | last post by:
Hi. How I can Include or Extract bites in a binary file?? I'm tring to use a fopen method, but without success. Do you have any idea??? Thanks!!!
11
by: NC Tim | last post by:
Hello, I think the question i have is fairly straightforward, but I can't seem to replicate the old SAS frequency procedure when I try to accomplish this in MS Access. anyway, i have about 10...
19
by: jason_box | last post by:
I'm alittle new at C and I'm trying to write a simple program that will record the frequency of words and just print it out. It is suppose to take stdin and I heard it's only a few lines but I'm...
1
by: estebistec | last post by:
Hello, I've developed a CompositeControl in C# for ASP.NET 2.0. In this control I am simulating Edit and View modes by hiding or showing appropriate controls (setting Control.Visible). Basically...
7
by: Udhay | last post by:
How to get the frequency of an audio file and how to separate the low and high frequency of an audio file
8
by: Andrew Savige | last post by:
I'm learning Python by reading David Beazley's "Python Essential Reference" book and writing a few toy programs. To get a feel for hashes and sorting, I set myself this little problem today (not...
5
by: sudhivns | last post by:
Hi, I wann use the same collection in interleaving loops. like F1(..) { foreach(child prd in parent) F2(prd ) }
13
by: umpsumps | last post by:
Hello, Here is my code for a letter frequency counter. It seems bloated to me and any suggestions of what would be a better way (keep in my mind I'm a beginner) would be greatly appreciated.. ...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.