Pipe of Subprocess in ANSI C

lcarnevale
Posts: 4
Joined: Mon Dec 07, 2020 9:46 am
Location: Messina
Contact:

Pipe of Subprocess in ANSI C

Postby lcarnevale » Mon Dec 07, 2020 10:25 am

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?
---
Lorenzo Carnevale

website: http://lorenzocarnevale.com

davidzuhn
Posts: 17
Joined: Fri Sep 20, 2019 1:50 am

Re: Pipe of Subprocess in ANSI C

Postby davidzuhn » Mon Dec 07, 2020 6:59 pm

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.

ESP_Sprite
Posts: 9766
Joined: Thu Nov 26, 2015 4:08 am

Re: Pipe of Subprocess in ANSI C

Postby ESP_Sprite » Tue Dec 08, 2020 1:29 am

ESP-IDF indeed does not support the notion of pipes. Suggest you look into using queues or (the ESP-IDF-specific) ringbuffers instead.

lcarnevale
Posts: 4
Joined: Mon Dec 07, 2020 9:46 am
Location: Messina
Contact:

Re: Pipe of Subprocess in ANSI C

Postby lcarnevale » Mon Dec 14, 2020 7:56 am

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.
---
Lorenzo Carnevale

website: http://lorenzocarnevale.com

Who is online

Users browsing this forum: No registered users and 159 guests