473,785 Members | 2,459 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

linux redirect problem

Hi
here is my c file, compile in gcc 3.X in linux:

#include <stdio.h>

int main()
{
printf("Hello\n ");
if (fork() == 0) printf("world! \n");
}
When i execute the a.out like this:
a.out > myFile.

Inside myFile:
Hello
Hello
world!

How come?
thanks
from Peter (cm****@hotmail .com)

Jun 28 '06 #1
6 3321

cm****@hotmail. com wrote:
Hi
here is my c file, compile in gcc 3.X in linux:

#include <stdio.h>

int main()
{
printf("Hello\n ");
if (fork() == 0) printf("world! \n");
}
When i execute the a.out like this:
a.out > myFile.

Inside myFile:
Hello
Hello
world!

How come?
thanks


1) The root of the question is off-topic, but I'll answer.
You need to flush the output stream before the fork. The
first printf is putting "Hello\n" into the buffer. The child
inherits that buffer before it is flushed, so when it prints
"World", it is also printing the "Hello" that the parent had
put in the buffer.

2) Your program is invoking undefined behavior because
of an incorrect prototype for main. Also, you failed to
include a prototype for fork(). (it's in <unistd.h>)

Jun 28 '06 #2
cm****@hotmail. com writes:
#include <stdio.h>

int main()
{
printf("Hello\n ");
if (fork() == 0) printf("world! \n");
}


fork() is not part of standard C. Try comp.unix.progr ammer.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 28 '06 #3
Bill Pursell wrote:
cm****@hotmail. com wrote:
Hi
here is my c file, compile in gcc 3.X in linux:

#include <stdio.h>

int main()
{
printf("Hello\n ");
if (fork() == 0) printf("world! \n");
}

<snip>
2) Your program is invoking undefined behavior because
of an incorrect prototype for main.
No, it's a valid definition for main. Obsolescent possibly, but valid.
Also, you failed to
include a prototype for fork(). (it's in <unistd.h>)


If fork takes no parameters and returns an int that does not lead to
undefined behaviour. However, I agree that the correct header should be
included.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc

Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
Jun 28 '06 #4
Bill Pursell wrote:
cm****@hotmail. com wrote:
Hi
here is my c file, compile in gcc 3.X in linux:

#include <stdio.h>

int main()
{
printf("Hello\n ");
if (fork() == 0) printf("world! \n");
}
When i execute the a.out like this:
a.out > myFile.

Inside myFile:
Hello
Hello
world!

How come?
thanks


1) The root of the question is off-topic, but I'll answer.
You need to flush the output stream before the fork. The
first printf is putting "Hello\n" into the buffer. The child
inherits that buffer before it is flushed, so when it prints
"World", it is also printing the "Hello" that the parent had
put in the buffer.


It is worth noting that there will be a difference in the behaviour of
the OPs posted code, depending on whether or not he redirects stdout to
a file.

<note>
The following will be semi-off-topic. While it primarily deals with
behaviours invoked from non-C-standard functions (fork()), it also
deals with the side-effects of behaviours specified by the C standard.
As I cannot find a corresponding thread in any Linux or Unix newsgroup,
I thought it worthwile to respond here. My apologies to newsgroup
topicality pedants.
</note>

In the case of a redirect, stdout is buffered, and not automatically
flushed with the '\n' character (C standard behaviour). This means
that, in the case of the OPs code,
a) the parent process will not flush it's output until the termination
of the main() function, which implies that the buffer is not yet
flushed when fork() is invoked, and
b) the child process will not flush it's output until the termination
of the main() function.

So, in the case of a redirect to file, the output will consist of
a) a line reading "Hello", generated by the parent process and written
on termination of the parent process,
b) a line reading "Hello", inherited from the parent process by the
child process, and written on termination of the child process, and
c) a line reading "World", generated by the child process and written
on termination of the child process

OTOH, in the case of no redirect, stdout is buffered, and flushed
automatically with each '\n' (again, by the C standard). Thus, the
child process will /not/ inherit an unflushed buffer, and will not
print the redundant "Hello". For this pattern, the output will consist
of
a) a line reading "Hello", generated by the parent process and written
on termination of the parent process, and
b) a line reading "World", generated by the child process and written
on termination of the child process
--
Lew Pitcher
(temporarily without GPG keys)

Jun 28 '06 #5

Flash Gordon wrote:
Bill Pursell wrote:
cm****@hotmail. com wrote:
Hi
here is my c file, compile in gcc 3.X in linux:

#include <stdio.h>

int main()
{
printf("Hello\n ");
if (fork() == 0) printf("world! \n");
}
<snip>
2) Your program is invoking undefined behavior because
of an incorrect prototype for main.
No, it's a valid definition for main. Obsolescent possibly, but valid.


A few days ago, in a different thread, Richard Heathfield wrote: Bill Pursell said:
Richard Heathfield wrote:
For each argv[n] where n >= 0 && n < argc, you can write to the
characters starting with argv[n][0] and going no further than the null
terminator.
Which is clear if you prototype main as int main(int argc, char
*const*argv),
plus you'll get a compiler warning if you try to write the values.

That's fine, provided your implementation documents that form for main().
Otherwise, ISTM that you are invoking undefined behaviour.


Is that consistent with what your saying? Is
int main()
merely obsolescent, or does it invoke undefined
behavior. If the former, is that consistent with Richard's
comment? I'm a little confused on this point.

Jun 28 '06 #6
Bill Pursell said:
Flash Gordon wrote:
[ int main() ]
No, it's a valid definition for main. Obsolescent possibly, but valid.


A few days ago, in a different thread, Richard Heathfield wrote:
Bill Pursell said:
> Richard Heathfield wrote:
>> For each argv[n] where n >= 0 && n < argc, you can write to the
>> characters starting with argv[n][0] and going no further than the null
>> terminator. > Which is clear if you prototype main as int main(int argc, char
> *const*argv),
> plus you'll get a compiler warning if you try to write the values.

That's fine, provided your implementation documents that form for main().
Otherwise, ISTM that you are invoking undefined behaviour.


Is that consistent with what your saying?


Yes.
Is int main() merely obsolescent,
Yes.
or does it invoke undefined behavior.


No. In the function definition, an empty parameter list is exactly
equivalent to a parameter list of void, so it's equivalent to
int main(void) which is one of the standard forms.

For future-proofing and good style, favour int main(void), but int main() is
not actually wrong.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 28 '06 #7

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

Similar topics

2
7657
by: Sami Viitanen | last post by:
Hello, I was using os.popen function to get CVS command output to string from script.. Same commands worked well with Windows, but Linux seems to have problem with those. Whole CVS commands seem to froze or given commands aren't executed right at all. I had to replace os.popen commands with os.system. os.system commands just
1
6081
by: Lincoln Yeoh | last post by:
Sorry to repost this but I still haven't figured it out and there weren't any responses. --- Say I use iptables to redirect tcp connections to my perl proxy servers. How then do I get the original destination IP address and tcp port? On FreeBSD I just use ipfw and fwd and then following works: $daddr=$client->sockhost; $dport=$client->sockport;
5
2196
by: Steve Lutz | last post by:
Hello, I have a page that creates a class, and then on certain conditions, redirects user to another page. The class has a Class_Terminate() function that saves itself to a database. The class comes from an includes ASP file, it isn't a COM object. Here's my code outline (not actual code for brevity - in otherwords, there may be syntax errors, but that's not the cause of the problem)
9
8436
by: jgcrawford | last post by:
G'day, I'm writing a ringtone manager for nokia ringtones and I'd like to be able to play the ringtone on the PC speaker. I've had some success in DOS with turbo C and its sound(), delay() and nosound(). Is there anything similar for Linux? I know I can make a simple beep with '\a', but that's not what I need. If not, can someone please show me how to do this with the /dev/audio device?
2
6014
by: news://news.microsoft.com/microsoft.public.de.germ | last post by:
Hallo! Ich habe ein Frameset mit 3 Frames. Im 1. Frame wird eine ASPX-Seite geöffnet. In dieser werden einige Steuerelemente angezeigt. Nun soll bei bestimmten Benutzeraktivitäten eine andere ASPX-Seite geöffnet werden, und zwar in einem der beiden anderen Frames. Mein Problem ist, dass die Seite, die geöffnet werden soll, manchmal im 2. und manchmal im 3. Frame angezeigt werden soll (je nach Benutzeraktivität).
5
4573
by: venner | last post by:
I'm having an issue with an ASP.NET website after upgrading to ASP.NET 2.0. The website makes use of a central authentication service (CAS) provided at the university I work for. Each page checks a session variable, and if it is not present, does a Response.Redirect to a webpage for the CAS passing a url parameter for the url to post back to. The CAS provides a page for the user to log into, validates the username and password, and then...
12
3471
by: =?Utf-8?B?cGI=?= | last post by:
I am having trouble doing a redirect in an async asp.net implemention. Most of the time it works, but when it doesn't it just "hangs", the browser never gets any return page. If I run it under the debugger, it works fine, though every so often I get a HttpException. System.Web.HttpException was caught ErrorCode=-2147024809 Message="An error occurred while communicating with the remote host. The error code is 0x80070057."...
1
5224
by: mlaris | last post by:
Howdy, We are experiencing a bizarre problem under Linux attempting to restore a DB2 database. The backup was created on this machine, and is being restored to the same machine, but changing the name of the database, and redirecting the tablespaces. The system is running DB2 Express C V9.5 and RHEL5 with latest updates The restore commands we are trying to run are as follows: RESTORE DATABASE CARDS FROM "/opt/rccs/data/backups"...
2
6606
by: xtremebass | last post by:
Hi Bytes, in Linux , is it possible to redirect Mysql table output to a file in Linux. i tried it, shows error , but output has displayed in linux prompt when no redirection of file has given(say select * from tablename) but when i am trying redirect output to file in Linux it shows error? can you tell me suggestion for this issue. code i used : <code> #!bin/sh password=xyzbac mysql -u root -p$password -h 192.168.1.8 << y use sys;
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.