diff --git a/examples/shm_test/CMakeLists.txt b/examples/shm_test/CMakeLists.txt new file mode 100644 index 00000000000..1985986d56a --- /dev/null +++ b/examples/shm_test/CMakeLists.txt @@ -0,0 +1,31 @@ +# ############################################################################## +# apps/examples/shm_test/CMakeLists.txt +# +# Licensed to the Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with this work for +# additional information regarding copyright ownership. The ASF licenses this +# file to you under the Apache License, Version 2.0 (the "License"); you may not +# use this file except in compliance with the License. You may obtain a copy of +# the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations under +# the License. +# +# ############################################################################## + +if(CONFIG_EXAMPLES_SHM) + nuttx_add_application( + NAME + ${CONFIG_EXAMPLES_SHM_PROGNAME} + SRCS + shm_main.c + STACKSIZE + ${CONFIG_EXAMPLES_SHM_STACKSIZE} + PRIORITY + ${CONFIG_EXAMPLES_SHM_PRIORITY}) +endif() diff --git a/examples/shm_test/Kconfig b/examples/shm_test/Kconfig new file mode 100644 index 00000000000..85c9957ec73 --- /dev/null +++ b/examples/shm_test/Kconfig @@ -0,0 +1,30 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config EXAMPLES_SHM + tristate "SHM example" + default n + depends on FS_SHMFS + ---help--- + Enable the SHM example which has an shm producer and consumer + +if EXAMPLES_SHM + +config EXAMPLES_SHM_PROGNAME + string "Program name" + default "shm_test" + ---help--- + This is the name of the program that will be used when the NSH ELF + program is installed. + +config EXAMPLES_SHM_PRIORITY + int "SHM task priority" + default 100 + +config EXAMPLES_SHM_STACKSIZE + int "SHM stack size" + default DEFAULT_TASK_STACKSIZE + +endif diff --git a/examples/shm_test/Make.defs b/examples/shm_test/Make.defs new file mode 100644 index 00000000000..5248c3f6eb1 --- /dev/null +++ b/examples/shm_test/Make.defs @@ -0,0 +1,23 @@ +############################################################################ +# apps/examples/shm_test/Make.defs +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +ifneq ($(CONFIG_EXAMPLES_SHM),) +CONFIGURED_APPS += $(APPDIR)/examples/shm_test +endif diff --git a/examples/shm_test/Makefile b/examples/shm_test/Makefile new file mode 100644 index 00000000000..1f3346868d7 --- /dev/null +++ b/examples/shm_test/Makefile @@ -0,0 +1,42 @@ +############################################################################ +# apps/examples/shm_test/Makefile +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +include $(APPDIR)/Make.defs + +# SHM test built-in application info + +PROGNAME = $(CONFIG_EXAMPLES_SHM_PROGNAME) +PRIORITY = $(CONFIG_EXAMPLES_SHM_PRIORITY) +STACKSIZE = $(CONFIG_EXAMPLES_SHM_STACKSIZE) +MODULE = $(CONFIG_EXAMPLES_SHM) + +# SHM test Example + +MAINSRC = shm_main.c + +# Build with WebAssembly when CONFIG_INTERPRETERS_WAMR is enabled + +WASM_BUILD = both + +# Mode of WebAssembly Micro Runtime + +WAMR_MODE = AOT + +include $(APPDIR)/Application.mk diff --git a/examples/shm_test/shm_main.c b/examples/shm_test/shm_main.c new file mode 100644 index 00000000000..1fb1d0b0d15 --- /dev/null +++ b/examples/shm_test/shm_main.c @@ -0,0 +1,116 @@ +/**************************************************************************** + * apps/examples/shm_test/shm_main.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +void shm_producer(const int shm_size, const char *shm_name) +{ + /* Write Hello World! to shared memory */ + + const char *message_0 = "Hello"; + const char *message_1 = "World!"; + + /* Shared memory file descriptor */ + + int shm_fd; + + /* Pointer to shared memory object */ + + void *ptr; + + /* Create the shared memory object */ + + shm_fd = shm_open(shm_name, O_CREAT | O_RDWR, 0666); + + /* Configure the size of the shared memory object */ + + ftruncate(shm_fd, shm_size); + + /* Memory map the shared memory object */ + + ptr = mmap(0, shm_size, PROT_WRITE, MAP_SHARED, shm_fd, 0); + + /* Write to the shared memory object */ + + sprintf(ptr, "%s", message_0); + + ptr += strlen(message_0); + sprintf(ptr, "%s", message_1); + ptr += strlen(message_1); + + printf("Producer Wrote to SHM: %s%s\n", message_0, message_1); +} + +void shm_consumer(const int shm_size, const char *shm_name) +{ + /* Shared memory file descriptor */ + + int shm_fd; + + /* Pointer to shared memory object */ + + void *ptr; + + /* Open the shared memory object */ + + shm_fd = shm_open(shm_name, O_RDONLY, 0666); + + /* Memory map the shared memory object */ + + ptr = mmap(0, shm_size, PROT_READ, MAP_SHARED, shm_fd, 0); + + /* Read from the shared memory object */ + + printf("Consumer Read from SHM: %s\n", (char *)ptr); + + /* Remove the shared memory object */ + + shm_unlink(shm_name); +} + +/**************************************************************************** + * shm_main + ****************************************************************************/ + +int main(int argc, FAR char *argv[]) +{ + const int shm_size = 2048; + const char *shm_name = "OS"; + + shm_producer(shm_size, shm_name); + shm_consumer(shm_size, shm_name); + return 0; +}