Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 26 Dec 1997 11:57:19 -0800
From:      Amancio Hasty <hasty@rah.star-gate.com>
To:        multimedia@freebsd.org
Cc:        Randall Hopper <rhh@ct.picker.com>
Subject:   async i/o code example for -current
Message-ID:  <199712261957.LAA01833@rah.star-gate.com>

next in thread | raw e-mail | index | archive | help

I asked Dyson to repost his aio example and he didn't so I dig up his
old posting.

This is useful for applications such as fxtv when is recording to disk.

	Cheers,
	Amancio


------- Forwarded Message

Return-Path: owner-freebsd-current@FreeBSD.ORG
Received: from sarip.sol.net (sarip.sol.net [169.207.30.120])
          by rah.star-gate.com (8.8.8/8.8.8) with ESMTP id MAA00296
          for <hasty@rah.star-gate.com>; Mon, 1 Dec 1997 12:12:38 -0800 (PST)
          (envelope-from owner-freebsd-current@FreeBSD.ORG)
Received: from hub.freebsd.org (hub.FreeBSD.ORG [204.216.27.18])
	by sarip.sol.net (8.8.8/8.8.8/SNNS-1.02) with ESMTP id OAA28385;
	Mon, 1 Dec 1997 14:11:53 -0600 (CST)
Received: from localhost (daemon@localhost)
          by hub.freebsd.org (8.8.7/8.8.7) with SMTP id LAA06230;
          Mon, 1 Dec 1997 11:33:22 -0800 (PST)
          (envelope-from owner-freebsd-current)
Received: (from root@localhost)
          by hub.freebsd.org (8.8.7/8.8.7) id LAA06160
          for current-outgoing; Mon, 1 Dec 1997 11:32:49 -0800 (PST)
          (envelope-from owner-freebsd-current)
Received: from dyson.iquest.net (dyson.iquest.net [198.70.144.127])
          by hub.freebsd.org (8.8.7/8.8.7) with ESMTP id LAA06082
          for <current@freebsd.org>; Mon, 1 Dec 1997 11:32:05 -0800 (PST)
          (envelope-from toor@dyson.iquest.net)
Received: (from root@localhost)
	by dyson.iquest.net (8.8.7/8.8.8) id OAA00376
	for current@freebsd.org; Mon, 1 Dec 1997 14:26:34 -0500 (EST)
	(envelope-from toor)
From: "John S. Dyson" <toor@dyson.iquest.net>
Message-Id: <199712011926.OAA00376@dyson.iquest.net>
Subject: FYI: usage of new AIO calls
To: current@FreeBSD.ORG
Date: Mon, 1 Dec 1997 14:26:34 -0500 (EST)
X-Mailer: ELM [version 2.4ME+ PL31 (25)]
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Sender: owner-freebsd-current@FreeBSD.ORG
X-Loop: FreeBSD.org
Precedence: bulk


For fun (and actually as a result of a comment by Justin Gibbs), I have put
together a small program that could maybe be used for streaming tape drives
using the new AIO code.  Note that if you try this, it must be used only
on current kernels on/after 19:00 GMT, Dec 1.

This code is NOT pretty, but does show example usage.  The code will
likely have problems on SMP kernels (unless you are writing to a VCHR
device.)


- -- 
John
dyson@freebsd.org
jdyson@nc.com


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/fcntl.h>
#include <sys/time.h>
#include <sys/errno.h>
#include <sys/aio.h>
#include <sys/syscall.h>

#define NBUFS 16
#define BUFSZ (1024*64)
struct aiocb *olist[NBUFS];
void *bufaddrs[NBUFS];
int activeflags[NBUFS];
int nactive;
int eof;

int
main( int argc, char *argv[]) {
	int i;
	int fd1, fd2;
	off_t curptr;
	int nread;
	int rtval;
	int errval;

	if (argc != 2) {
		fprintf(stderr, "usage: fcp destfile");
		exit(0);
	}
	fd1 = 0;
	fd2 = open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0666);
	if (fd2 == -1) {
		perror(argv[1]);
		exit(1);
	}

	for(i=0;i<NBUFS;i++) {
		olist[i] = malloc(sizeof (struct aiocb));
		bzero(olist[i], sizeof (struct aiocb));
		bufaddrs[i] = malloc(BUFSZ);
		activeflags[i] = 0;
	}
	nactive = 0;
	curptr = 0;
	eof = 0;

	while ((eof == 0) || (nactive > 0)) {

		if ((eof && (nactive > 0)) || (nactive == NBUFS)) {
			for (i = 0; i < NBUFS; i++ ) {
				if (activeflags[i]) {
					errval = aio_suspend(&olist[i], 1, NULL);
					if (errval) {
						perror("aio_suspend");
						exit(1);
					}
					break;
				}
			}
		}

		if (nactive > 0) {
			for (i = 0; i < NBUFS; i++) {
				if (activeflags[i] == 0)
					continue;
				errval = aio_error(olist[i]);
				if (errval) {
					if (errval == EINPROGRESS)
						continue;
					perror("aio_error1");
					exit(1);
				}
				activeflags[i] = 0;
				--nactive;
				rtval = aio_return(olist[i]);
				if (rtval != olist[i]->aio_nbytes) {
					perror("buffer not fully written");
					exit(1);
				}
			}
		}

		for (i = 0;
			((eof == 0) && (nactive < NBUFS) && (i < NBUFS));
			i++) {
			if (activeflags[i] == 0) {
				nread = read( fd1, bufaddrs[i], BUFSZ);
				if (nread == 0) {
					eof = 1;
					break;
				}
				if (nread == -1) {
					perror("read");
					exit(1);
				}

				olist[i]->aio_offset = curptr;
				curptr += nread;
				
				olist[i]->aio_nbytes = nread;
				olist[i]->aio_fildes = fd2;
				olist[i]->aio_buf = bufaddrs[i];
				errval = aio_write(olist[i]);
				activeflags[i] = 1;
				nactive++;
				if (errval) {
					perror("aio_write");
					exit(1);
				}
			}
		}
	}
}

------- End of Forwarded Message






Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199712261957.LAA01833>