Sponsored Content
Top Forums Programming what would happen if a process wrote to its own stdin? Post 302302834 by c_d on Wednesday 1st of April 2009 06:56:21 AM
Old 04-01-2009
Quote:
Originally Posted by pshaikh
Hi,

I know Smilie May be I was not clear enough of what I am trying to do,

The program is as follows -

Code:
int main()
{
   if( write(STDIN_FILENO,"arghh!",6) == -1 )
  {
      fprintf(stderr,"Error in write\n");
      exit(2);
  }
  return 0;
}

I am doing following on shell command line -

$>file.txt (this create an empty file.txt)
$./a.out < file.txt (redirect standard input of a.out to file.txt, a.out is supposed to WRITE to(not READ from) file.txt(that is what it is programmed to do).

($ is not part of command but of shell command prompt)

STDIN_FILENO's file status flag is 2 (O_RDWR - both read write allowed).

I expected that a.out writes to standard input; which is file.txt. But file.txt remains empty.

I am trying to understand 'why'

Anybody. Please illuminate
Code:
[c_d@localhost cfiles]$ cat temp.c
#include<unistd.h>
#include<fcntl.h>

int main()
{

    if((write(STDIN_FILENO,"arrgh!",6))==-1)
    {
        perror("error writing to file");
    }    

}


[c_d@localhost cfiles]$ touch file     #create empty file
[c_d@localhost cfiles]$ cat file        #you can see, that the file is empty
[c_d@localhost cfiles]$ gcc temp.c  #compile temp.c
[c_d@localhost cfiles]$ ./a.out 0>file #redirection notice no "arrgh!" prints
[c_d@localhost cfiles]$ cat file         #because "arrgh!" is redirected to file
arrgh![c_d@localhost cfiles]$ rm file ; touch file  
[c_d@localhost cfiles]$ ./a.out <file      # :(
error writing to file: Bad file descriptor
[c_d@localhost cfiles]$

Smilie i dunno...why that happens. probably because file connects to stdin of a.out first, so when write command executes, stdin is connected to file so it is not able to write into stdin, hence it returns -1.

Last edited by c_d; 04-01-2009 at 08:29 AM..
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

what happen when changing Hostname?

I 'm using RH 7.2 Genome in the Network Configuration I change therer are two places one for static hostname for my machine and in DNS hostname I don't know what happen when restarting my PC when connecting using dialer I can't browse the Internet also I can't use sendmail .......Server timeout... (2 Replies)
Discussion started by: atiato
2 Replies

2. Programming

Any one can tell me how this happen?

The #1 Online Store for Louis Vuitton Replicas is: http://www.opichina.com.cn. We offer Louis Vuitton Replicas and more! Whatever you call it: LV Bags, LV Replicas, Louis Vuitton Fake, Louis Vuitton Knockoffs, Louis Vuitton Bag, Louis Vuitton Purse, Louis Vuitton Wallet, Louis Vuitton Shoes,... (10 Replies)
Discussion started by: jiangyanna
10 Replies

3. UNIX for Dummies Questions & Answers

What would happen if. . .

Hi, Could someone please tell me what would happen if the following were entered into the command line: rm -i /books/*.* rm /books/* Many thanks! (3 Replies)
Discussion started by: crispy
3 Replies

4. UNIX for Advanced & Expert Users

Unix ID deleted - What happen to process

I have an unix id (AIX system) which is used to run a couple of processes. They also write some log files into a file system (that is not in the home directory of the user id, but in different location). One bad day, the id was deleted accidentally. But the home directory, files and everything... (1 Reply)
Discussion started by: cmgreat
1 Replies

5. UNIX for Dummies Questions & Answers

whats happen when we create new user

hi frndz I wanna knw exatly what happen when we create new user... which directories are created ?? which files are modified ?? thanx.... (2 Replies)
Discussion started by: luckypower
2 Replies

6. AIX

How does ITIL processing happen in AIX?

How does ITIL process is implemened in AIX? (6 Replies)
Discussion started by: AIXlearner
6 Replies

7. UNIX for Dummies Questions & Answers

What Does Happen During Boot Process? - BIOS and MBR

I'm talking about boot process in multi-boot Linux perceptive. Please tell me whether my explanation is right or wrong? If wrong, please explain. "The BIOS checks the system and loads this initial bootstrapping code into memory. This initial bootstrap code searches for an active partition... (0 Replies)
Discussion started by: f.ben.isaac
0 Replies

8. UNIX for Advanced & Expert Users

Who actually wrote Professional Linux Programming ?

Hello, Who actually wrote Professional Linux Programming of Wrox publication as there are two different sets of writers, one set consists of Jon Masters and Richard Blum and another set of writers is Neil Mathew with lots of other writers. Plz resolve it. I'm really confused. Regards.. (0 Replies)
Discussion started by: vectrum
0 Replies

9. Shell Programming and Scripting

Crontab - wrote Simple Script but i cant work out how to play it at a certain time.

Hi everyone. Silly might be silly be I'm still new to bash. I'm trying to make an Alarm Clock for in the morning using my laptop i have wrote this Simple Script but i cant work out how to play it at a certain time. #!/bin/bash cd /home/josh/Music/Bruno_Mars/Hooligans/ cvlc... (8 Replies)
Discussion started by: jtsmith90
8 Replies

10. What is on Your Mind?

Ah, the AMIGA, (another poem I wrote in 2005).

Well I wrote this in 2005 and uploaded to AMINET.as a commemoration of a machine that is still in use today. It is now 29 years since this machne came into being. Phenominal and it is still being supported- WOW! My A1200 is on 24/7 and I use it to test code developed on AMIGA emulators... ... (0 Replies)
Discussion started by: wisecracker
0 Replies
OPEN(1) 						    BSD General Commands Manual 						   OPEN(1)

NAME
open -- open files and directories SYNOPSIS
open [-e] [-t] [-f] [-F] [-W] [-R] [-n] [-g] [-j] [-h] [-s sdk] [-b bundle_identifier] [-a application] file ... [--args arg1 ...] DESCRIPTION
The open command opens a file (or a directory or URL), just as if you had double-clicked the file's icon. If no application name is speci- fied, the default application as determined via LaunchServices is used to open the specified files. If the file is in the form of a URL, the file will be opened as a URL. You can specify one or more file names (or pathnames), which are interpreted relative to the shell or Terminal window's current working directory. For example, the following command would open all Word files in the current working directory: open *.doc Opened applications inherit environment variables just as if you had launched the application directly through its full path. This behavior was also present in Tiger. The options are as follows: -a application Specifies the application to use for opening the file -b bundle_indentifier Specifies the bundle identifier for the application to use when opening the file -e Causes the file to be opened with /Applications/TextEdit -t Causes the file to be opened with the default text editor, as determined via LaunchServices -f Reads input from standard input and opens the results in the default text editor. End input by sending EOF character (type Control-D). Also useful for piping output to open and having it open in the default text editor. -F Opens the application "fresh," that is, without restoring windows. Saved persistent state is lost, except for Untitled documents. -W Causes open to wait until the applications it opens (or that were already open) have exited. Use with the -n flag to allow open to func- tion as an appropriate app for the $EDITOR environment variable. -R Reveals the file(s) in the Finder instead of opening them. -n Open a new instance of the application(s) even if one is already running. -g Do not bring the application to the foreground. -j Launches the app hidden. -h Searches header locations for a header whose name matches the given string and then opens it. Pass a full header name (such as NSView.h) for increased performance. -s For -h, partial or full SDK name to use; if supplied, only SDKs whose names contain the argument value are searched. Otherwise the high- est versioned SDK in each platform is used. --args All remaining arguments are passed to the opened application in the argv parameter to main(). These arguments are not opened or inter- preted by the open tool. EXAMPLES
"open '/Volumes/Macintosh HD/foo.txt'" opens the document in the default application for its type (as determined by LaunchServices). "open '/Volumes/Macintosh HD/Applications/'" opens that directory in the Finder. "open -a /Applications/TextEdit.app '/Volumes/Macintosh HD/foo.txt'" opens the document in the application specified (in this case, TextE- dit). "open -b com.apple.TextEdit '/Volumes/Macintosh HD/foo.txt'" opens the document in the application specified (in this case, TextEdit). "open -e '/Volumes/Macintosh HD/foo.txt'" opens the document in TextEdit. "ls | open -f" writes the output of the 'ls' command to a file in /tmp and opens the file in the default text editor (as determined by LaunchServices). "open http://www.apple.com/" opens the URL in the default browser. "open 'file://localhost/Volumes/Macintosh HD/foo.txt'" opens the document in the default application for its type (as determined by Launch- Services). "open 'file://localhost/Volumes/Macintosh HD/Applications/'" opens that directory in the Finder. "open -h NSView" lists headers whose names contain NSView and allows you to choose which ones to open. "open -h NSView.h" immediately opens NSView.h. "open -h NSView -s OSX10.12" lists headers whose names contain NSView in the MacOSX 10.12 SDK and allows you to choose which ones to open. HISTORY
First appeared in NextStep. macOS April 14, 2017 macOS
All times are GMT -4. The time now is 01:22 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy