newbie code question

R

Robert Smith

I don't understand why this code is doing what its doing can someone
explain? I would think that every 1 second the word child gets printed to
the screen.... but what happens is all 10 get printed at once!!!??? why does
it do this?
thanks

#include <iostream>
#include <stdlib.h>
#include <unistd.h>

using namespace std;

int main()
{
pid_t mypid;
mypid=fork();

if(mypid==0)
{

for(int i=0;i<10;i++)
{
cout<<"child";
sleep(1);
}
return 1;
}
//cout<<"Parent";


return 0;
}
 
R

red floyd

Robert said:
I don't understand why this code is doing what its doing can someone
explain? I would think that every 1 second the word child gets printed to
the screen.... but what happens is all 10 get printed at once!!!??? why does
it do this?
thanks

#include <iostream>
#include <stdlib.h>
#include <unistd.h>

using namespace std;

int main()
{
pid_t mypid;
mypid=fork();

if(mypid==0)
{

for(int i=0;i<10;i++)
{
cout<<"child"; cout << endl;
sleep(1);
}
return 1;
}
//cout<<"Parent";


return 0;
}
fork() is OT, but you're not flushing cout.
 
A

AngleWyrm

Robert Smith said:
I don't understand why this code is doing what its doing can someone
explain? I would think that every 1 second the word child gets printed to
the screen.... but what happens is all 10 get printed at once!!!??? why does
it do this?
thanks
#include <unistd.h> [clip]
sleep(1);

On my system, the sleep function is known as _sleep and takes it's argument in
milliseconds; thus the function call is:
_sleep(1000); // one-second wait time
 
S

Siemel Naran

red floyd said:
Robert Smith wrote:
fork() is OT, but you're not flushing cout.

Right, but two questions.

First, I thought I read somewhere that std::cout has the flag ios::unitbuf
on, which means that every insertion into cout is immediately flushed, and
you'd see that characters appear right away. I looked through the standard,
searching for "unitbuf", "flush", "endl", but could not turn up anything
supporting my claim though. Let me know your thoughts.

Second, the standard says in 27.6.2.7 item 3 that the behavior of std::endl
is to insert a newline character into the stream then call flush. Thus an
explicit call to std::flush should not be necessary.

But the OP's implementation may be broken. So here are 2 solutions:

for(int i=0;i<10;i++)
{
cout<<"child";
cout << endl << flush;
sleep(1);
}

or

cout << unitbuf;

for(int i=0;i<10;i++)
{
cout<<"child";
cout << endl;
sleep(1);
}

On my implementation STLPort, endl is defined exactly as the standard
prescribes:

template <class _CharT, class _Traits>
inline basic_ostream<_CharT, _Traits>& _STLP_CALL
endl(basic_ostream<_CharT, _Traits>& __os) {
__os.put(__os.widen('\n'));
__os.flush();
return __os;
}
 
R

Rolf Magnus

Siemel said:
Right, but two questions.

First, I thought I read somewhere that std::cout has the flag
ios::unitbuf on, which means that every insertion into cout is
immediately flushed, and you'd see that characters appear right away.
I looked through the standard, searching for "unitbuf", "flush",
"endl", but could not turn up anything supporting my claim though.
Let me know your thoughts.

There are three standard output streams: cout, cerr and clog. Usually,
cout and clog are buffered, cerr is not.
Second, the standard says in 27.6.2.7 item 3 that the behavior of
std::endl is to insert a newline character into the stream then call
flush.
Thus an explicit call to std::flush should not be necessary.

Either cout << endl; if you want a newline and a flush, or cout <<
flush; if you only want the flush.
But the OP's implementation may be broken.

What makes you think so?
 
D

Default User

AngleWyrm said:
#include <unistd.h> [clip]
sleep(1);

On my system, the sleep function is known as _sleep and takes it's argument in
milliseconds; thus the function call is:
_sleep(1000); // one-second wait time

Well, he is likely working under UNIX, witness the use of <unistd.h>. So
your windows (probably) _sleep() isn't really relative. That's why
platform-specific things don't belong here.

He should be posting in comp.unix.programmer.




Brian Rodenborn
 
T

Tim Slattery

Robert Smith said:
I don't understand why this code is doing what its doing can someone
explain? I would think that every 1 second the word child gets printed to
the screen.... but what happens is all 10 get printed at once!!!??? why does
it do this?

for(int i=0;i<10;i++)
{
cout<<"child";
sleep(1);
}

What does the argument to the "sleep" function mean? The Windows
"Sleep(..)" API function takes an integer argument that represents the
number of milliseconds to sleep for. In order to get it to wait for
one second, you would code:

Sleep(1000);

These things are OS specific, and I have no idea what OS you're using.
But I'd bet the argument to your "sleep" function is not the number of
seconds to sleep for.
 
T

Thomas Matthews

Robert said:
I don't understand why this code is doing what its doing can someone
explain? I would think that every 1 second the word child gets printed to
the screen.... but what happens is all 10 get printed at once!!!??? why does
it do this?
thanks

#include <iostream>
#include <stdlib.h>
Or you could:
#include <unistd.h>
Platform specific header. Two of my platforms don't have this.


using namespace std;

int main()
{
pid_t mypid;
This is declaring a variable to hold a process ID (identifier).
Many operating systems associate identifiers with processes,
whether they be threads, or whole processes. See the manuals
about your operating system.

mypid=fork();
This functions creates another process, "forking" the current
process. The function returns the ID of the new process. At
least that seems to be common operating systems theory.

if(mypid==0)
{
But then, the fork function could return an error value
instead. One must consult the Holy Words of their operating
system.

for(int i=0;i<10;i++)
Execute the following code 10 times.

{
cout<<"child";
sleep(1);
The common understanding of a "sleep" function is to put
your process to sleep. This means that it is not consuming
processor resources. Many operating systems use either a
time limit for the sleep or an event. For example, one
might want to suspend their program until a user presses
a key or until a response is received from a hardware device.
Consult the Holy Words of your operating system for more
information on the sleep function.


The above loop will print "child", sleep, print "child",
and etc., for 10 iterations. The output will be:
childchildchildchildchildchildchildchildchildchild
One might not see the output since the stream is not
flushed, but one has to ask the operating system if
it flushes the stream after a program terminates (or
just before).

return 1;
This statement returns execution to whatever called
the main() function, which is usually the operating
system. The value of one indicates there was a failure
executing the system. A returned value of zero or
EXIT_SUCCESS would be returned upon successful execution.

}
//cout<<"Parent";
This would be a debugging output indicating that
execution is in the parent task.
return 0;
}
To get a better grip on this stuff, read up on multi-
tasking and multiprocessing. And if you really want,
read up on mult-threading. Use these keywords on
your favorite web search engine. These concepts
are off-topic in this newsgroup.


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 

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

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,477
Latest member
ColumbusMa

Latest Threads

Top