Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > 1024 bites interleaving frequency???

Reply
Thread Tools

1024 bites interleaving frequency???

 
 
Giulio
Guest
Posts: n/a
 
      11-24-2004
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
 
Reply With Quote
 
 
 
 
Dave Vandervies
Guest
Posts: n/a
 
      11-24-2004
In article <co3529$htq$>,
Giulio <> 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
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
 
Reply With Quote
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      11-25-2004
In article <co3529$htq$>,
Giulio <> 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
 
Reply With Quote
 
Gordon Burditt
Guest
Posts: n/a
 
      11-25-2004
>#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
 
Reply With Quote
 
Tom St Denis
Guest
Posts: n/a
 
      11-25-2004
Giulio <> wrote in message news:<co3529$htq$>...
> 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
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      11-25-2004
In <co3529$htq$> Giulio <> 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:
Currently looking for a job in the European Union
 
Reply With Quote
 
Jason Curl
Guest
Posts: n/a
 
      11-25-2004
Dan Pop wrote:
> In <co3529$htq$> Giulio <> 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.?
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      11-25-2004
In article <co5217$imi$>,
Jason Curl <> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CompositeControl's children are interleaving with other controls on the page estebistec@gmail.com ASP .Net 1 09-29-2006 02:58 PM
affect of adsl interleaving on voip Peter Gradwell UK VOIP 14 06-21-2006 01:57 PM
Frame Relay fragmentation and interleaving Mikey Cisco 2 05-12-2006 04:49 AM
Most efficient way of storing 1024*1024 bits Tor Erik Sønvisen Python 15 11-04-2005 10:54 AM
any such thing as list interleaving? Tom Plunket Python 2 07-12-2003 10:12 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57