Page 1 of 1

Pipe of Subprocess in ANSI C

Posted: Mon Dec 07, 2020 10:25 am
by lcarnevale
Hello there,

I am interested in running a pipe of subprocesses written in ANSI C using the ESP32.

The following code is just a sample I have tried to run.
  1. int parse_output(void) {
  2.     char *cmd = "ls -l";    
  3.  
  4.     char buf[BUFSIZE];
  5.     FILE *fp;
  6.  
  7.     if ((fp = popen(cmd, "r")) == NULL) {
  8.         printf("############## Error opening pipe!\n");
  9.         return -1;
  10.     }
  11.  
  12.     while (fgets(buf, BUFSIZE, fp) != NULL) {
  13.         printf("############## OUTPUT: %s", buf);
  14.     }
  15.  
  16.     if(pclose(fp))  {
  17.         printf("############## Command not found or exited with error status\n");
  18.         return -1;
  19.     }
  20.  
  21.     return 0;
  22. }

And the following is the error I receive

Code: Select all

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

Is then possible to do it?

Re: Pipe of Subprocess in ANSI C

Posted: Mon Dec 07, 2020 6:59 pm
by davidzuhn
The code running on the ESP32 is more like the kernel than an Unix-like process. There are no processes, just tasks within a single address space. So I'm not in any way surprised that popen isn't part of the available libraries.

Also, note that popen isn't part of ANSI C (it's probably standardized within POSIX), as it's very much an OS specific function, as opposed to a language / data structure library call.

You should look at FreeRTOS tasks if you want to run something "in parallel" with the existing code. It'll be a function within the SAME program that serves as the thing to run within the other task.

Re: Pipe of Subprocess in ANSI C

Posted: Tue Dec 08, 2020 1:29 am
by ESP_Sprite
ESP-IDF indeed does not support the notion of pipes. Suggest you look into using queues or (the ESP-IDF-specific) ringbuffers instead.

Re: Pipe of Subprocess in ANSI C

Posted: Mon Dec 14, 2020 7:56 am
by lcarnevale
Thanks for your reply.
Considering my intent was changing the ESP32's behavior in run-time, I was able to use the OTA for this purpose.