[Develope]

Using the devpoll (/dev/poll) Interface

하늘을닮은호수M 2007. 4. 17. 14:21
반응형

출처: http://access1.sun.com/techarticles/devpoll.html

Using the devpoll (/dev/poll) Interface

by Shridhar Acharya
(March 2002)

Introduction
Description of the Test Case
The UDP Server Program -- udpdevpoll.c
The UDP Client Program -- udpclnt.c
The Argument File Generation Program -- generate.c
Source of the Programs
A. udpdevpoll.c
B. udpclnt.c
C. generate.c
Further References

We want to hear from you! Please send us your FEEDBACK.

The following technical article may contain actual software programs in source code form. This source code is made available for developers to use as needed, pursuant to the terms and conditions of this license.

Introduction

The Solaris[tm] 7 Operating Environment (11/99 version) introduced a new mechanism for polling file descriptors. The /dev/poll driver is a special psuedo driver that allows a process to monitor multiple sets of polled file descriptors. Access to the /dev/poll driver is provided through the open(2), write(2) and ioctl(2) system calls.

The /dev/poll driver returns the number of the polled file descriptors that have data in them. One can then read only those file descriptors using a pollfd type array. Thus, one does not have to continuously poll a large number of file descriptors including those that may not have any data in them. This makes for a more efficient use of the system resources.

The /dev/poll interface is recommended for polling a large number of file descriptors, of which only a few may have data at any given time. The devpoll interface works best with the newer set of Ultrasparc® IIi and Ultrasparc® III processors. Applications best suited to use the devpoll driver include:

  • Applications that repeatedly poll a large number of file descriptors.
  • Applications where the polled file descriptors are relatively stable; that is, they are not constantly closed and reopened.
  • Applications where the set of file descriptors which actually have polled events pending is small, compared to the total number of file descriptors being polled.

For a detailed description of the merits of using the new /dev/poll interface versus the poll(2) system call, please refer to the technical article "Polling Made Efficient", by Bruce Chapman at http://soldc.sun.com/articles/polling_efficient.html.

Description of the Test Case

Please note that the following test case is written for programmers wanting to use the devpoll interface, with the intention of providing code that is simple to understand.

The test case consists of a UDP server program that opens thousands of sockets on different ports and polls, all those sockets using the /dev/poll interface. The test case also has a UDP client program that can send data to any of these server ports. The number of ports to be opened by the server and the amount of time each port should be polled can be controlled using arguments to the program. The ports to which the client should connect can also be controlled by arguments to the client program.

It should be noted that the server program should open a large number of sockets. The client should be configured to send data to only a few of those sockets to check the usefulness of the /dev/polldriver.

The following is a detailed description of the test case.

udpdevpoll.c:The UDP-based server program that uses devpoll interface.
udpclnt.c:The UDP-based client program that sends data to the above
server program.
generate.c:Program used to generate the argument files for the test cases.

A. The UDP Server Program - udpdevpoll.c

The udpdevpoll.c program needs two arguments.

./udpdevpoll

The first argument is the name of a file that specifies a list containing the IP address of the server, and the ports on which the program should listen for incoming packets.

Example file (sock_svr):

IP AddressPort Number
100.1.2.135000
100.1.2.135001
.
.
100.1.2.163000

The second argument specifies the timeout value (in milliseconds) that has to be supplied to the devpoll driver. The devpoll driver waits at least
timeout milliseconds for an event to coccur on any of the selected file descriptors. This value should be an integer. If this value is 0, devpoll returns immediately. If the value is -1, devpoll blocks until the requested event occurs.

To compile : $cc -o udpdevpoll udpdevpoll.c -lsocket

To run: ./udpdevpoll sock_svr 1

The program then prints out the IP address and the port numbers
and waits for the data. It prints a line for every 10000 packets
received.

Example :

sv ip: 100.1.2.1 port: 63998
sv ip: 100.1.2.1 port: 63999
aggregate packet count: 108
aggregate packet count: 208
.
.

Please Note: In order to open many sockets, you may be required to increase the number of file descriptor's soft limit. In Bourne shell, you may use "ulimit -n 65536". For more information on this, please refer to the man page of the limit(1) command.

B. The UDP Client Program - udpclnt.c

The udpclnt.c program also needs two arguments.

./udpclnt

The first argument is the name of a file that has the IP address and a port of the client system which should be used to send the data.

Example file (sock_clnt):

100.1.2.2 60001

The second argument is the name of a file containing a list of IP addresses and port numbers of the system running the server program to which the client should send the data.

Example file(sock_svrdest):

IP AddressPort Number
100.1.2.135000
100.1.2.136088
100.1.2.141000
.
.
100.1.2.155000

To compile: $cc -o udpclnt udpclnt.c -lsocket

To run: ./udpclnt sock_clnt sock_svrdest

The program then prints out the IP address and the port numbers and sends the data. It prints a line for every 10000 packets sent.

Example :

sv ip: 100.1.2.1 port: 35000
sv ip: 100.1.2.1 port: 36088
.
.
sv ip: 100.1.2.1 port: 55000
cl ip: 100.1.2.2 port: 60001
aggregate packet count: 108
aggregate packet count: 208
.
.

C. The Argument File Generation Program - generate.c

The generate.c program may be used to generate the argument files.

To compile: $cc -o generate generate.c

To run:$ ./generate > sock_svr
$ ./generate > sock_svrdest

Source of the Programs

Further References

1. The man page of devpoll: man -s 7d poll
2. The Socket Interfaces section of the Network Interface Guide under the Solaris 8 Software Developer Collection in http://docs.sun.com.

반응형