LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-27-2012, 04:01 AM   #1
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 201

Rep: Reputation: 31
Advanced Linux Programming Ch7


I was following http://www.advancedlinuxprogramming.com/ chapter 7 to play around with the file descriptors.

Here is the source code of a process that I'm looking into.

Code:
/* File: a.c */
#include <stdio.h>
#include <unistd.h>

int main( int argc, char *argv[] )
{
	char what[80];
	what[0] = what[79] = '\0';
	fprintf( stdout, "%d\n", (int)getpid() );
	do {
		fgets( what, 79, stdin );
	} while ( what[0] != 'Q' );

	return 0;
}
Compile this to a.out and run it:
Code:
$ ./a.out
26678
Start another terminal, and attempt to send some text to 26678:
Code:
$ echo "hello" >/proc/26678/fd/0
$ echo "Q"     >/proc/26678/fd/0
a.out in the first terminal doesn't quit. Instead the texts 'hello' and 'Q' are displayed in that terminal. Why doesn't a.out exit at 'Q'?

Thanks.
 
Old 07-27-2012, 04:26 AM   #2
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 201

Original Poster
Rep: Reputation: 31
I think I understand why. It's because the fd/0,1,2 are all connected to the tty. I was simply sending 'hello' and 'Q' to the tty pointed by these fd's.

Code:
$ ll /proc/26678/fd/
total 0
lrwx------ 1 ... 64 Jul 27 17:23 0 -> /dev/pts/42
lrwx------ 1 ... 64 Jul 27 17:23 1 -> /dev/pts/42
lrwx------ 1 ... 64 Jul 27 17:23 2 -> /dev/pts/42
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with 82801G Intel CH7 Audio (ALSA) josefig Slackware 9 09-12-2009 01:28 AM
C programming, learning more advanced stuff microsoft/linux Programming 6 02-15-2006 05:06 PM
New Forum for Linux-Gurus[Programming/Administration] or Linux-Advanced Users [ Progr raees LQ Suggestions & Feedback 5 04-12-2005 07:35 PM
Better buying "advanced linux prog" or "unix advanced prog" Dominik Programming 3 12-31-2003 01:11 PM
Where can I learn more advanced programming concepts? OrganicX Programming 8 03-01-2003 08:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration