Interacting with the linux console

This is a discussion on Interacting with the linux console within the C++ Programming forums, part of the General Programming Boards category; #This may not be the conceptually right place for this post, but I'm hoping for a solution which is a ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528

    Interacting with the linux console

    #This may not be the conceptually right place for this post, but I'm hoping for a solution which is a little portable( and wouldn't have to be changed with ..say..changing kernels..or using another flavour of unix) and is C++ friendly.#

    Using the system command lets the user 'send' a command to the console and (as far as I know)..that is all , nothing more.

    But suppose I want to implement a program which has to give a shell command, get back the response of the console in a string (when the console prompts for input)...and repeat the process.

    What would be the best( or 'only' ) way to do so ?
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    For one-way communication look up popen().

    For bi-directional communication it gets quite a bit more complicated.
    exec
    would be a starting point.

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    I was hoping for something simpler..!..but after going through that ..I'm almost sure that a simpler solution does not exist.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107
    Manasij,

    Paul S. Wang mentioned another facility in his Introduction to ANSI C on UNIX (ISBN: 0-534-14232-X) there about page 370.
    >> A pipe is a direct (in memory) I/O channel between processes.
    >> ...
    >> The pipe can be thought of as a first-in-first-out character buffer (Figure 12.3) with
    >> a read descriptor pointing to one end and a write descriptor pointing to
    >> the other end.
    >> To establish a pipe, use the system call
    Code:
    >> int pipe(int fildes[2])
    >> to obtain a buffer and two descriptors...

    I believe that the operating system (Linux specifically) will report that there is a pipe (I think it calls it a message queue these days) via the ipcs command. Wang's usage involves using fork to generate multiple processes and applying the pipe so that they may communicate directly. Here is a code fragment which he suggested:

    Code:
    int fildes[2];
    ...
       pipe(fildes);   /* establish message queue with operating system */
    
       if( fork() == 0 )
       {
          close(fildes[1]);   /* child reads fildes[0] */
          ...
          _exit(0);
       }
       close(filedes[0]);   /* parent writes fildes[1] */
    He gets more elaborate latter using pairs of pipes so that the children can communicate bi-directionally with the parent process.

    Best Regards,
    Last edited by new_ink2001; 04-12-2011 at 04:25 PM. Reason: semantics
    Kept the text books....
    Went interdisciplinary after college....
    Still looking for a real job since 2005....

    During the interim, I may be reached at ELance, vWorker, FreeLancer, oDesk and WyzAnt.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux Version reccomendation
    By Pobega in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 10-05-2006, 07:48 PM
  2. Why Linux, for the average user?
    By Hunter2 in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 07-07-2006, 03:36 PM
  3. installing linux for the first time
    By Micko in forum Tech Board
    Replies: 9
    Last Post: 12-06-2004, 05:15 AM
  4. cd command in linux console
    By TheUnheardHuman in forum Tech Board
    Replies: 2
    Last Post: 11-19-2002, 03:59 PM
  5. Linux? Windows Xp?
    By VooDoo in forum Linux Programming
    Replies: 15
    Last Post: 07-31-2002, 09:18 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21