简体   繁体   中英

Running pipe of subprocess in ESP32 using ANSI C

I am getting into the ESP32 using esp-idf and I would like to make a program able to run subprocess while using C.

Banally, I have tried to call a function like the following:

...
  #include <stdio.h>

  char *cmd = "ls -l";    
  char buf[BUFSIZE];
  FILE *fp;
  if ((fp = popen(cmd, "r")) == NULL) {
      printf("############## Error opening pipe!\n");
      return -1;
  }
  while (fgets(buf, BUFSIZE, fp) != NULL) {
      printf("############## OUTPUT: %s", buf);
  }
  if(pclose(fp))  {
      printf("############## Command not found or exited with error status\n");
      return -1;
  }
  return 0;
...

but the application returns

c:\esp\../main/app_main.c:139: undefined reference to `popen`

Has somebody experience with this one?

popen() is a system call available on POSIX-compliant and similar systems (UNIX, Linux, FreeBSD). The software that the ESP32 runs is not remotely POSIX-compliant.

Pipes and subprocesses are meaningless on the ESP32 - they're not supported. There is no popen() call.

The ESP32 runs a lightweight operating system called FreeRTOS . FreeRTOS provides lightweight tasks and very simple memory management and not much else.

FreeRTOS tasks share an address space; they are generally non-preemptive, voluntarily yield the processor to allow other tasks to run, and execute a subroutine (C function). POSIX processes each have their own address space, are preemptive and run POSIX binary applications. They're totally different, incompatible things. The ESP32 does not have the resources or memory management hardware to properly support POSIX processes.

ESP-IDF ("IoT Development Framework") is a layer built above FreeRTOS that provides a thin API providing access to the ESP32's hardware (I2C, SPI, wifi, Bluetooth, timers and the like).

Other programming environments like the Arduino Core , Python and LUA are all built on top of ESP-IDF.

ESP-IDF runs one program at a time, unlike POSIX-style systems. That program is the one you compiled, linked, and stored in its flash memory. POSIX-style systems run hundreds or thousands of programs at once. The ESP32 and ESP-IDF do not have the ability to do that.

As you start building applications for the ESP32, not only can you not use POSIX-compatible system calls, you'll also need to learn to program in a very resource-limited environment. On Linux and similar systems programmers commonly write code as if there were no constraints on memory, filesystem size and durability. None of these things are true on the ESP32 - it's trivial to write a program that accidentally needs more memory than its 520KB available to it; it's trivial to write a program that expects more persistent storage than is available in its flash memory; it's trivial to write a program that writes to the flash memory so frequently that it destroys it. You'll need to learn how to program within the constraints of the ESP32, not just how to write code that calls the ESP-IDF instead of a POSIX system.

Take a read through the ESP-IDF documentation that you linked to. That's the environment you're working in. You may find starting out in the Arduino Core to be easier - it's a C/C++ environment, there are a ton of tutorials out there for it, and it's an easier build environment, and you can still easily call ESP-IDF functions from it if you need to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM