Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1064851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T19:43:41+00:00 2026-05-16T19:43:41+00:00

I would like to know how I can execute a Python (or Lua etc)

  • 0

I would like to know how I can execute a Python (or Lua etc) script from my C code using execl (or similar)?

The following is some “parent / child” code showing how I am sending a STREAM of data to the child using PIPES. The code may not be perfect but you get the idea. Note the execl at the bottom:

#include <sys/types.h> 
#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
#include <unistd.h>  

#define STDIN_FILENO    0       /* Standard input.  */
#define STDOUT_FILENO   1       /* Standard output.  */
#define STDERR_FILENO   2       /* Standard error output.  */
#define MAXLINE 4096

int main(void){
 int  n, parent_child_pipe[2], child_parent_pipe[2];
 pid_t pid;
 char line[MAXLINE];

 if (pipe(parent_child_pipe) < 0 || pipe(child_parent_pipe) < 0)
  puts("Error creating pipes...\n");

 if ( (pid = fork()) < 0)
  puts("Error forking...\n");
 else if (pid > 0) { /* PARENT */
  close(parent_child_pipe[0]); 
  close(child_parent_pipe[1]);
  while (fgets(line, MAXLINE, stdin) != NULL) {
   n = strlen(line);
   if (write(parent_child_pipe[1], line, n) != n)
    puts("write error to pipe...\n");
   if ( (n = read(child_parent_pipe[0], line, MAXLINE)) < 0)
    puts("read error from pipe...\n");
   if (n == 0) {
    puts("child closed pipe...\n");
    break;
   }
   line[n] = 0; /* null terminate */
   if (fputs(line, stdout) == EOF)
    puts("fputs error...\n");
  }
  if (ferror(stdin))
   puts("fgets error on stdin...\n");
  exit(0);

 } else {  /* CHILD */
  close(parent_child_pipe[1]);
  close(child_parent_pipe[0]);
  if (parent_child_pipe[0] != STDIN_FILENO) {
   if (dup2(parent_child_pipe[0], STDIN_FILENO) != STDIN_FILENO)
    puts("dup2 error to stdin...\n");
   close(parent_child_pipe[0]);
  }
  if (child_parent_pipe[1] != STDOUT_FILENO) {
   if (dup2(child_parent_pipe[1], STDOUT_FILENO) != STDOUT_FILENO)
    puts("dup2 error to stdout...\n");
   close(child_parent_pipe[1]);
  }
  **if (execl("./child", "child", (char *) 0) < 0)**
   puts("execl error...\n");
 }
}

Now the “child” program above is written in C and simply receives the stream via STDIN, manipulates the stream, and sends it back out using STDOUT.

Something like:

#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
#include <unistd.h>  

#define STDIN_FILENO    0       /* Standard input.  */
#define STDOUT_FILENO   1       /* Standard output.  */
#define STDERR_FILENO   2       /* Standard error output.  */
#define MAXLINE 4096

int main(void){
 int  n;
 char line[MAXLINE];

 while ( (n = read(STDIN_FILENO, line, MAXLINE)) > 0) {
  line[n] = 0;  /* null terminate */  
  n = strlen(line);
  if (write(STDOUT_FILENO, line, n) != n)
   puts("write error");
 }
 exit(0);
}

So that is working fine, but now I want to be able to write the CHILD in any scripting language like Python / Lua etc. How can I do this? I have tried stuff like:

if (execl(“path to python”, “test.py”, (char *) 0) < 0)

But it just seems to HANG waiting to receive input?

Can someone please help me on this? I assume PIPES can talk to anything like Lua / Python that can read from STDIN and send back to STDOUT?

UPDATE:

I have made some small changes now, here is the Python file: “NullFilter.py” that simply ECHOS what it is sent:

#!/usr/bin/python
import sys

class NullFilter:

      def execute(self): 
            #Read data from STDIN...
            data = sys.stdin.read() 
            #Write data to STDOUT...
            sys.stdout.write(data) 
            exit(0) 
if __name__ == '__main__':
      nf = NullFilter()
      nf.execute()

And now the C code calls it using:

...
if (execl("/usr/bin/python","./NullFilter.py","./NullFilter.py",NULL, (char *) 0) < 0)
puts("execl error...\n");
...

When I run it now I can enter text into STDIN, but have to hit CRTL-C to see what has happened: Here is the result:

debian@debian:~/Desktop/pipe example$ ./parent
hello
hello again
^CTraceback (most recent call last):
  File "./NullFilter.py", line 17, in <module>

nf.execute()
  File "./NullFilter.py", line 10, in execute
debian@debian:~/Desktop/pipe example$     data = sys.stdin.read() 
KeyboardInterrupt

debian@debian:~/Desktop/pipe example$ 

UPDATE 2:

OK, so I have been playing around a little, and simply changed the Python code from “sys.stdin.read()” to “sys.stdin.readline()” and it seems to work to a certain degree, BUT NOT PERFECT at all…

#!/usr/bin/python
import sys

class NullFilter:

      def execute(self): 
            #Read data from STDIN...
            data = ""             
            for line in sys.stdin.readline():
                data = data + line 
            #Write data to STDOUT...
            sys.stdout.write(data) 
            exit(0) 
if __name__ == '__main__':
      nf = NullFilter()
      nf.execute()

This is a workaround BUT not perfect at all. Any other ideas how I can get the STDIN to read the stream UNBUFFERED? I have looked at the SELECT module in Python:

http://docs.python.org/library/select.html

I have also tried passing “-u” to Python to make it “unbuffered” but still no luck ;-(

But surely this cannot be so difficult?

Lynton

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T19:43:41+00:00Added an answer on May 16, 2026 at 7:43 pm

    After much playing around and trying to flush everything I realised I needed to CLOSE the STDOUT end of the PIPE going from the parent to the child (after writing to the pipe of course)…

    So the code is now:

    #include <sys/types.h>  
    #include <stdio.h>   
    #include <stdlib.h>   
    #include <string.h>   
    #include <unistd.h>   
    
    #define STDIN_FILENO    0       /* Standard input.  */ 
    #define STDOUT_FILENO   1       /* Standard output.  */ 
    #define STDERR_FILENO   2       /* Standard error output.  */ 
    #define MAXLINE 4096 
    
    int main(void){ 
     int  n, parent_child_pipe[2], child_parent_pipe[2]; 
     pid_t pid; 
     char line[MAXLINE]; 
     int rv;
    
     if (pipe(parent_child_pipe) < 0 || pipe(child_parent_pipe) < 0) 
      puts("Error creating pipes...\n"); 
    
     if ( (pid = fork()) < 0) 
      puts("Error forking...\n"); 
     else if (pid > 0) { /* PARENT */ 
      close(parent_child_pipe[0]);  
      close(child_parent_pipe[1]); 
      while (fgets(line, MAXLINE, stdin) != NULL) { 
       n = strlen(line); 
       if (write(parent_child_pipe[1], line, n) != n) 
        puts("write error to pipe...\n"); 
       close(parent_child_pipe[1]);
       wait(&rv); 
       if ( (n = read(child_parent_pipe[0], line, MAXLINE)) < 0) 
        puts("read error from pipe...\n"); 
       if (n == 0) { 
        puts("child closed pipe...\n"); 
        break; 
       } 
       line[n] = 0; /* null terminate */ 
       if (fputs(line, stdout) == EOF) 
        puts("fputs error...\n"); 
      } 
      if (ferror(stdin)) 
       puts("fgets error on stdin...\n"); 
      exit(0); 
    
     } else {  /* CHILD */ 
      close(parent_child_pipe[1]); 
      close(child_parent_pipe[0]); 
      if (parent_child_pipe[0] != STDIN_FILENO) { 
       if (dup2(parent_child_pipe[0], STDIN_FILENO) != STDIN_FILENO) 
        puts("dup2 error to stdin...\n"); 
       close(parent_child_pipe[0]); 
      } 
      if (child_parent_pipe[1] != STDOUT_FILENO) { 
       if (dup2(child_parent_pipe[1], STDOUT_FILENO) != STDOUT_FILENO) 
        puts("dup2 error to stdout...\n"); 
       close(child_parent_pipe[1]); 
      } 
      if (execl("./NullFilter.py", "./NullFilter.py", (char *) 0) < 0)
       puts("execl error...\n"); 
     } 
    } 
    

    You can see the “close(parent_child_pipe[1]);” just after writing to the PIPE above, that is the crucial piece I had to do. That will force the stream to be flushed to the Python script, Lua script , C code etc….

    In the above you will see I am executing a Python script “NullFilter.py”….

    NOTE: If you run the code above it will work for one iteration of input / output as the Python script closes the pipe after the first test, but the essentials are there for you to build on…

    Thanks for all the help though, I have learnt a lot from this exercise 😉

    Lynton

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know how can I determine if a python script is
I would like to know how I can execute Javascript before Page_Load event (if
I would like to know if, and how, I can call or execute what
i would like know some reference. I know i can googling it. but prefer
I would like to know how can I create a regexp to match the
I would like to know how can I write a pointer array: short* myArray
I would like to know how can I output a number with 2 decimal
I would like to know how can i read a value of input text
I would like to know how can I use tinyint in SQL Server 2005
I would like to know how can I use jQuery to select multiple items

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.