Results 1 to 1 of 1
Hi All,
I am trying small master-slave programs using pseudo terminals.
I am trying to use select() call to wait for the arrival of data in the pseudo terminal.
I ...
- 11-26-2008 #1Just Joined!
- Join Date
- Jan 2008
- Posts
- 9
Select call not working with Pseudo terminals
Hi All,
I am trying small master-slave programs using pseudo terminals.
I am trying to use select() call to wait for the arrival of data in the pseudo terminal.
I am not understanding why the select call is returning 1 even if i dont run my slave program.
Also if i run slave program, the select is returning one even before i enter any thing on the pseudo terminal.
Below is the code snippet of Master and Slave.
Can any one have a look at it and please correct me where i am going wrong,
########################
MASTER.cpp
########################
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
#include <wait.h>
#include <sys/select.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace std;
#define SIZE 5
int main ()
{
int masterpt=posix_openpt(O_RDWR);
char buffer[SIZE];
if( masterpt==-1 ) {
perror("Failed to get a pseudo terminal");
return 2;
}
if( grantpt( masterpt )!=0 ) {
perror("Failed to change pseudo terminal's permission");
return 2;
}
if( unlockpt( masterpt )!=0 ) {
perror("Failed to unlock pseudo terminal");
return 2;
}
//setsid();
const char *name=ptsname(masterpt);
int slavept=open(name, O_RDWR ); // This line makes the ptty our controlling tty. We do not otherwise need it open
fprintf(stderr, "Opened %s with fd %d\n", name, slavept);
close( slavept );
fd_set readfd, errfd;
//int t1 = open("/dev/pts/3", O_RDWR);
FD_ZERO(&readfd);
FD_SET(masterpt, &readfd);
//FD_SET(t1, &readfd);
int selret;
{
while(1)
{
// sleep(1);
FD_ZERO(&readfd);
FD_SET(masterpt, &readfd);
selret = select(masterpt+1, &readfd, NULL, NULL, NULL );
cout << "SelRet =" << selret<< endl;
if( selret == 1)
{
int num=read( masterpt, buffer, SIZE );
cout <<"RECEIVED COMMAND FROM PSEUDO=>" << buffer <<endl;
}
}
}
close( masterpt );
return 0;
}
###########################
SLAVE.cpp
###########################
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
#include <wait.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace std;
#define SIZE 5
int main ()
{
char buffer[205];
int slavept=open("/dev/pts/4", O_RDWR ); // Opening /dev/pts/4 because on my machine this is the device name returned by system call "ptsname(masterpt)" in MASTER.cpp
while (1)
{
cout <<"ENTER COMMAND ==>"<< endl;
cin >> buffer;
int num=write( slavept, buffer, SIZE );
}
close( slavept );
return 0;
}
Thanks in Advance
Vikram


Reply With Quote