????poll?????÷?????man????????????????????е??????
?????????????
????????????????????????
??????????????????????????????
????????????????????????shell????????500??????????????????"Testing...."
????????????????????
???????????????????????
????Ubuntu10.04:
????poll.cpp?????
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>// waitpid
#include <sys/types.h>// waitpid
#include <string.h>// strlen
#include <poll.h>// poll
/*
comment:
pipe is used between two processes on the same computer.
*/
#define TIMES 50
int main(){
int pipefds[2];
if( -1 == pipe( pipefds)){
printf( "Error when create pipes ");
}else{
int i;
pid_t pid = fork();
if( 0 == pid){// child
printf( "child running ");
close( pipefds[0]);
for( i = 0; i < TIMES; ++ i){
write( pipefds[1]?? "iamagoodguy"?? strlen( "iamagoodguy"));
sleep( 1);
}
}else{
printf( "parent running ");
char buf[256];
close( pipefds[1]);
struct pollfd pf[2];// key structure
pf[0].fd = 0;// console input
pf[0].events = POLLIN;// wait for bytes input
pf[1].fd = pipefds[0];// pipe input
pf[1].events = POLLIN;// wait for bytes input
for( i = 0; i < TIMES; ++ i){
poll( pf?? 2?? 500);// wait for only 500 ms
printf( "Testing... ");
if( pf[1].revents & POLLIN){
buf[ read( pipefds[0]?? buf?? 256)] = '