LinuxQuestions.org
Visit Jeremy's Blog.
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 09-15-2005, 02:55 PM   #1
neo_in_matrix
Member
 
Registered: Sep 2003
Posts: 52

Rep: Reputation: 15
How to send CTRL-D to child process via a pipe?


I am successful at spawning a child process and connect its stdin/stdout to two pipes. The program (gdb) needs input. I tried fputc('q', f); fputc('\r', f); and fputs("q", f), but none worked.
 
Old 09-15-2005, 03:03 PM   #2
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Did you try fputs("q\n",f);? If you really want to send CTRL-D, send 0x04.
 
Old 09-15-2005, 03:06 PM   #3
neo_in_matrix
Member
 
Registered: Sep 2003
Posts: 52

Original Poster
Rep: Reputation: 15
Tried fputs("q\n",f) - did not work. :-(
 
Old 09-15-2005, 03:27 PM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,793

Rep: Reputation: 496Reputation: 496Reputation: 496Reputation: 496Reputation: 496
Can you issue other commands to gdb successfully and get their output ?
 
Old 09-15-2005, 03:39 PM   #5
neo_in_matrix
Member
 
Registered: Sep 2003
Posts: 52

Original Poster
Rep: Reputation: 15
I found that if I do fflush(f); after fputs, then it works.

And also setting setvbuf(f, NULL, _IOLBF, 0); also works (fflush not needed).

It's strange I have to send fputs("h\n", f) - I thought it should be the CR key - \r.
 
Old 09-15-2005, 05:29 PM   #6
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
\r by itself is a mac thing. \n is the unix way. (Newline).
 
Old 09-15-2005, 05:56 PM   #7
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Quote:
Originally posted by Matir
\r by itself is a mac thing. \n is the unix way. (Newline).
And Windows is a combination of CRLF. (e.g. \r\n.)
 
Old 09-15-2005, 06:14 PM   #8
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Quote:
Originally posted by deiussum
And Windows is a combination of CRLF. (e.g. \r\n.)
Yep, sure is. Why they thought they needed TWO characters for the same thing that is accomplished by one in every other operating system is beyond me.
 
Old 09-15-2005, 07:34 PM   #9
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Technically, they aren't the same, though. CR returns the cursor to the beginning of the line, and LF moves the cursor down a line, (but doesn't necessarily return it to the beginning of the line.) In Windows, though, when writing a \n it is typically translated underneath to \r\n when writing to text-mode files.
 
Old 09-15-2005, 08:00 PM   #10
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Yes, that is true. But I have honestly never seen any good use of one by itself in windows. Have you?
 
Old 09-15-2005, 08:09 PM   #11
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,793

Rep: Reputation: 496Reputation: 496Reputation: 496Reputation: 496Reputation: 496
Quote:
Why they thought they needed TWO characters for the same thing that is accomplished by one in every other operating system is beyond me.
This is wrong, many of the non Unix/Unix like O/Ses use CR-LF, and not exactly the problem.

Every terminal or terminal emulator need both CR and LF to go to the next line, including GNU/Linux and Unix.
Before Unix and C were invented, that convention was used too for representing an end of line in text files on all operating systems using ASCII. That was easing the display of a file on a screen (or a teletype) as no processing was required to "cat" a file.

C created the "\n" convention (newline) to represent the end of lines in text files, regardless of the underlying O/S (of course at the beginning the only C underlying O/S was Unix).

That was saving a little storage space, and easing all programs dealing with files (a single character is faster to locate that two) and improve response time of interactive programs at a time when sending one byte could cost a noticeable amount of time ...

However that added some complexity to the terminal drivers, which had (and still have) to convert this delimitor in what the terminal expects/sends.

See the numerous stty options to deal with that mess: inlcr igncr icrnl onlcr ocrnl onocr onlret echonl
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
how a father process know which child process send the signal SIGCHLD icoming Programming 10 07-20-2010 08:26 AM
How to kill a Child and all its subsequent child process in C shayer009 Programming 3 12-04-2007 01:40 AM
Killing a child process from another child marri Programming 6 10-01-2004 08:08 PM
Bash Scripting - child process affecting parent process mthaddon Linux - General 1 05-02-2004 02:19 PM
about parent and child process winwar Solaris / OpenSolaris 3 07-23-2003 07:07 AM

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

All times are GMT -5. The time now is 11:48 AM.

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