Pipe test

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@775 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo 2008-07-22 00:54:03 +00:00
parent 0b22d6a034
commit 106f18b460
4 changed files with 409 additions and 0 deletions

0
examples/pipe/.depend Normal file
View File

30
examples/pipe/Make.dep Normal file
View File

@ -0,0 +1,30 @@
pipe_main.o: pipe_main.c \
/home/Owner/projects/nuttx/nuttx/include/sys/types.h \
/home/Owner/projects/nuttx/nuttx/include/nuttx/config.h \
/home/Owner/projects/nuttx/nuttx/include/arch/types.h \
/home/Owner/projects/nuttx/nuttx/include/nuttx/compiler.h \
/home/Owner/projects/nuttx/nuttx/include/sys/stat.h \
/home/Owner/projects/nuttx/nuttx/include/time.h \
/home/Owner/projects/nuttx/nuttx/include/stdio.h \
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/stdarg.h \
/home/Owner/projects/nuttx/nuttx/include/sched.h \
/home/Owner/projects/nuttx/nuttx/include/nuttx/sched.h \
/home/Owner/projects/nuttx/nuttx/include/queue.h \
/home/Owner/projects/nuttx/nuttx/include/signal.h \
/home/Owner/projects/nuttx/nuttx/include/semaphore.h \
/home/Owner/projects/nuttx/nuttx/include/limits.h \
/home/Owner/projects/nuttx/nuttx/include/arch/limits.h \
/home/Owner/projects/nuttx/nuttx/include/pthread.h \
/home/Owner/projects/nuttx/nuttx/include/mqueue.h \
/home/Owner/projects/nuttx/nuttx/include/queue.h \
/home/Owner/projects/nuttx/nuttx/include/nuttx/irq.h \
/home/Owner/projects/nuttx/nuttx/include/assert.h \
/home/Owner/projects/nuttx/nuttx/include/arch/irq.h \
/home/Owner/projects/nuttx/nuttx/include/nuttx/net.h \
/home/Owner/projects/nuttx/nuttx/include/nuttx/fs.h \
/home/Owner/projects/nuttx/nuttx/include/stdlib.h \
/home/Owner/projects/nuttx/nuttx/include/unistd.h \
/home/Owner/projects/nuttx/nuttx/include/string.h \
/home/Owner/projects/nuttx/nuttx/include/stddef.h \
/home/Owner/projects/nuttx/nuttx/include/fcntl.h \
/home/Owner/projects/nuttx/nuttx/include/errno.h

75
examples/pipe/Makefile Normal file
View File

@ -0,0 +1,75 @@
############################################################################
# Makefile
#
# Copyright (C) 2008 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/.config
-include $(TOPDIR)/Make.defs
ASRCS =
AOBJS = $(ASRCS:.S=$(OBJEXT))
CSRCS = pipe_main.c
COBJS = $(CSRCS:.c=$(OBJEXT))
SRCS = $(ASRCS) $(CSRCS)
OBJS = $(AOBJS) $(COBJS)
BIN = lib$(CONFIG_EXAMPLE)$(LIBEXT)
all: $(BIN)
$(AOBJS): %$(OBJEXT): %.S
$(call ASSEMBLE, $<, $@)
$(COBJS): %$(OBJEXT): %.c
$(call COMPILE, $<, $@)
$(BIN): $(OBJS)
@( for obj in $(OBJS) ; do \
$(call ARCHIVE, $@, $${obj}); \
done ; )
.depend: Makefile $(SRCS)
@$(MKDEP) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
@touch $@
depend: .depend
clean:
@rm -f $(BIN) *~ .*.swp
$(call CLEAN)
distclean: clean
@rm -f Make.dep .depend
-include Make.dep

304
examples/pipe/pipe_main.c Normal file
View File

@ -0,0 +1,304 @@
/****************************************************************************
* pipe_main.c
*
* Copyright (C) 2008 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
/****************************************************************************
* Definitions
****************************************************************************/
#ifndef CONFIG_EXAMPLES_FIFO_PATH
# define CONFIG_EXAMPLES_FIFO_PATH "/tmp/testfifo"
#endif
#define MAX_BYTE 13
#define WRITE_SIZE MAX_BYTE
#define NWRITES 1400
#define NWRITE_BYTES (NWRITES * WRITE_SIZE)
#define READ_SIZE (2*MAX_BYTE)
#define NREADS (NWRITES / 2)
#define NREAD_BYTES NWRITE_BYTES
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: reader
****************************************************************************/
static void *reader(pthread_addr_t pvarg)
{
char buffer[READ_SIZE];
int fd = (int)pvarg;
int ret;
int nbytes;
int value;
int ndx;
printf("reader: started\n");
for (nbytes = 0, value = 0; nbytes < NREAD_BYTES;)
{
ret = read(fd, buffer, READ_SIZE);
if (ret < 0 )
{
fprintf(stderr, "reader: read failed, errno=%d\n", errno);
return (void*)1;
}
else if (ret == 0)
{
#warning BUG: pipe should return zero when writer closes
fprintf(stderr, "reader: Received zero bytes\n");
return (void*)2;
}
for (ndx = 0; ndx < ret; ndx++)
{
if (value >= WRITE_SIZE)
{
value = 0;
}
if (buffer[ndx] != value)
{
fprintf(stderr, "reader: Byte %d, expected %d, found %d\n",
nbytes + ndx, value, buffer[ndx]);
return (void*)3;
}
value++;
}
nbytes += ret;
}
printf("reader: %d bytes read\n", nbytes);
return (void*)0;
}
/****************************************************************************
* Name: writer
****************************************************************************/
static void *writer(pthread_addr_t pvarg)
{
char buffer[WRITE_SIZE];
int fd = (int)pvarg;
int ret;
int i;
printf("writer: started\n");
for (i = 0; i < WRITE_SIZE; i++)
{
buffer[i] = i;
}
for (i = 0; i < NWRITES; i++)
{
ret = write(fd, buffer, WRITE_SIZE);
if (ret < 0 )
{
fprintf(stderr, "writer: write failed, errno=%d\n", errno);
return (void*)1;
}
else if (ret != WRITE_SIZE)
{
fprintf(stderr, "writer: Unexpected write size=%d\n", ret);
return (void*)2;
}
}
printf("writer: %d bytes written\n", NWRITE_BYTES);
return (void*)0;
}
/****************************************************************************
* Name: perform_test
****************************************************************************/
static int perform_test(int fdin, int fdout)
{
pthread_t readerid;
pthread_t writerid;
void *value;
int tmp;
int ret;
/* Start reader thread */
printf("perform_test: Starting reader thread\n");
ret = pthread_create(&readerid, NULL, reader, (pthread_addr_t)fdin);
if (ret != 0)
{
fprintf(stderr, "perform_test: Failed to create reader thread, error=%d\n", ret);
return -1;
}
/* Start writer thread */
printf("perform_test: Starting writer thread\n");
ret = pthread_create(&writerid, NULL, writer, (pthread_addr_t)fdout);
if (ret != 0)
{
fprintf(stderr, "perform_test: Failed to create writer thread, error=%d\n", ret);
ret = pthread_cancel(readerid);
if (ret != 0)
{
fprintf(stderr, "perform_test: Failed to cancel reader thread, error=%d\n", ret);
}
return -1;
}
/* Wait for writer thread to complete */
printf("perform_test: Waiting for writer thread\n");
ret = pthread_join(writerid, &value);
if (ret != 0)
{
fprintf(stderr, "perform_test: pthread_join failed, error=%d\n", ret);
}
else
{
ret = (int)value;
printf("perform_test: writer returned %d\n", ret);
}
/* Wait for reader thread to complete */
printf("perform_test: Waiting for reader thread\n");
tmp = pthread_join(readerid, &value);
if (tmp != 0)
{
fprintf(stderr, "perform_test: pthread_join failed, error=%d\n", ret);
}
else
{
tmp = (int)value;
printf("perform_test: reader returned %d\n", tmp);
}
if (ret == 0)
{
ret = tmp;
}
printf("perform_test: returning %d\n", ret);
return ret;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: user_initialize
****************************************************************************/
void user_initialize(void)
{
}
/****************************************************************************
* Name: user_start
****************************************************************************/
int user_start(int argc, char *argv[])
{
int fdin;
int fdout;
int ret;
/* Test FIFO logic */
printf("user_start: Performing FIFO test\n");
ret = mkfifo(CONFIG_EXAMPLES_FIFO_PATH, 0666);
if (ret < 0)
{
fprintf(stderr, "user_start: mkfifo failed with errno=%d\n", errno);
return 1;
}
fdin = open(CONFIG_EXAMPLES_FIFO_PATH, O_RDONLY);
if (fdin < 0)
{
fprintf(stderr, "user_start: Failed to open FIFO %s for reading, errno=%d\n",
CONFIG_EXAMPLES_FIFO_PATH, errno);
return 2;
}
fdout = open(CONFIG_EXAMPLES_FIFO_PATH, O_WRONLY);
if (fdout < 0)
{
fprintf(stderr, "user_start: Failed to open FIFO %s for writing, errno=%d\n",
CONFIG_EXAMPLES_FIFO_PATH, errno);
close(fdin);
return 3;
}
ret = perform_test(fdin, fdout);
close(fdin);
close(fdout);
unlink(CONFIG_EXAMPLES_FIFO_PATH);
if (ret != 0)
{
fprintf(stderr, "user_start: FIFO test FAILED\n");
return 4;
}
printf("user_start: FIFO test PASSED\n");
/* Test PIPE logic */
printf("user_start: Performing pipe test\n");
/* Not yet implemented */
fflush(stdout);
return 0;
}