Character driver finished

This commit is contained in:
2025-02-24 14:18:48 +00:00
parent 5500b9dc74
commit 3681dbfaac
12 changed files with 340 additions and 1 deletions

View File

@@ -18,5 +18,8 @@ int main(){
n = read( fd2, buf, 10);
printf(" read data = %s from ptwo \n", buf);
close(fd1);
close(fd2);
return 0;
}

Binary file not shown.

View File

@@ -20,7 +20,7 @@ int main(){
FD_ZERO(&read_set);
FD_SET(fd1, &read_set);
FD_SET(fd2, &read_set);
n = select(FD_SETSIZE, &read_set, NULL, NULL, &timeout);
if( n < 0 ){
perror("select ");
@@ -40,6 +40,9 @@ int main(){
n = read(fd2, buf, 10);
printf(" read data = %s from ptwo \n", buf);
}
// close(fd1);
// close(fd2);
return 0;

46
select_poll/poll1.c Normal file
View File

@@ -0,0 +1,46 @@
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <poll.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdlib.h>
int main(){
int fd1, fd2;
struct pollfd pollarray[10];
char buf[10];
int n;
fd1 = open("./pone", O_RDWR);
fd2 = open("./ptwo", O_RDWR);
pollarray[0].fd = fd1;
pollarray[1].fd = fd2;
pollarray[0].events = POLLIN;
pollarray[1].events = POLLIN;
n = poll(pollarray, 2, 10000);
if(n < 0){
perror("poll:");
exit(1);
}
//test whether fd1 is ready or not
if( (pollarray[0].revents & POLLIN)){
printf(" reading from fd1 (pone)\n");
n = read(fd1, buf, 10);
printf(" read data = %s from pone\n", buf);
}
//test whether fd2 is ready or not
if( (pollarray[1].revents & POLLIN)){
printf(" reading from fd2 (ptwo)\n");
n = read(fd2, buf, 10);
printf(" read data = %s from ptwo\n", buf);
}
// close(fd1);
// close(fd2);
return 0;
}