[mpich-discuss] Problems with terminal-IO and mpiexec

Dr. Wilfried Jakob (IAI) wilfried.jakob at kit.edu
Tue Jul 31 09:26:00 CDT 2012


Hi,

I would appreciate if someone can help me with the following problem:

I want to have a communication on the root process (rank=0) in such a 
way that single characters are read from the terminal.
This is achieved in plain C by using  termios.h and the functions 
tcgetattr() and tcsetattr() as shown in the attached test program. With 
gcc  and  Debian Linux this works fine.

I translated the program with mpicc without any problems and started it 
directly from the command line: it works well.
Then I started it with
     mpiexec -f nodes -n 2  tioTestMPI
and received the following:

Waiting for input ...
   error with cbreak
Waiting for input ...
   error with cbreak

A more detailed analysis shows that the call to "tcgetattr" yields the 
error "Invalid argument". This was obtained by a subsequent call to perror.

A version including prior calls to
    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
is given by the second program. It yields the same results:
Waiting for input ...
   error with cbreak
lazy slave

Thanks in advance
Wilfried Jakob

Here is the source code of the test program 1:
=================================================
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>

static struct termios new_io;
static struct termios old_io;

int cbreak(int fd) {
    /* save old terminal settings */
    if((tcgetattr(fd, &old_io)) == -1)
       return -1;
    new_io = old_io;
    /* change terminal settings */
    new_io.c_lflag = new_io.c_lflag & ~(ECHO|ICANON);
    new_io.c_cc[VMIN] = 1;
    new_io.c_cc[VTIME]= 0;

    /* set new settings */
    if((tcsetattr(fd, TCSAFLUSH, &new_io)) == -1)
       return -1;
    return 1;
}


int getch(void) {
    int c;

    if(cbreak(STDIN_FILENO) == -1) {
       printf("  error with cbreak \n");
       exit(EXIT_FAILURE);
    }
    c = getchar();
    /* restore old terminal settings */
    tcsetattr(STDIN_FILENO, TCSANOW, &old_io);
    return c;
}


int main (int argc, char** argv)
{
    int  ii;

    printf ("Waiting for input ...\n");
    ii = getch ();
    printf ("  read: \"%c\" = %d\n", (char)ii, ii);

    printf ("Waiting for input ...\n");
    ii = getch ();
    printf ("  read: \"%c\" = %d\n", (char)ii, ii);

    printf ("\nfinished!\n");
    return 0;
}
=================================================

Here is the source code of the test program 2:
=================================================
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>

#include "mpi.h"

static struct termios new_io;
static struct termios old_io;


int cbreak(int fd) {
    /* save old terminal settings */
    if((tcgetattr(fd, &old_io)) == -1)
       return -1;
    new_io = old_io;
    /* change terminal settings */
    new_io.c_lflag = new_io.c_lflag & ~(ECHO|ICANON);
    new_io.c_cc[VMIN] = 1;
    new_io.c_cc[VTIME]= 0;

    /* set new settings */
    if((tcsetattr(fd, TCSAFLUSH, &new_io)) == -1)
       return -1;
    return 1;
}


int getch(void) {
    int c;

    if(cbreak(STDIN_FILENO) == -1) {
       printf("  error with cbreak\n");
       exit(EXIT_FAILURE);
    }
    c = getchar();
    /* restore old terminal settings */
    tcsetattr(STDIN_FILENO, TCSANOW, &old_io);
    return c;
}


int main (int argc, char** argv)
{
    int  ii, rank, size;

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    if (rank == 0) { /* master */
       printf ("Waiting for input ...\n");
       ii = getch ();
       printf ("  read: \"%c\" = %d\n", (char)ii, ii);

       printf ("Waiting for input ...\n");
       ii = getch ();
       printf ("  read: \"%c\" = %d\n", (char)ii, ii);

       printf ("\nfinished!\n");
    } /* master */
    else
       printf ("lazy slave\n");

    MPI_Finalize();
    return 0;
}
=================================================

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/mpich-discuss/attachments/20120731/cd98fce0/attachment.html>


More information about the mpich-discuss mailing list