The most simple way is to use the shell to create a pipeline. But if you want to create any other shape you will have to do some programming. Some languages such as Python, TCL, purl … have built in functions to create task harnesses. Here is how it is done at the lowest level. Using Unix primitive system calls.
- Create a Task.
#!/usr/bin/python3
- Open Pipe
#!/usr/bin/python3 import os stdout_id=1 pipe_out,pipe_in = os.pipe() print ("pipe is: ",(pipe_in,pipe_out))
- Fork
#!/usr/bin/python3 import os stdout_id=1 pipe_out,pipe_in = os.pipe() print ("pipe is: ",(pipe_in,pipe_out)) pid=os.fork() #print("forked") if pid<0: print ("fork error") exit() elif pid == 0: #print ("I am child:", str(os.getpid())) else: #print ("I am parent:", str(os.getpid())) #print ("parent: child's pid="+str(pid))
- Trim Pipe
#!/usr/bin/python3 import os stdout_id=1 pipe_out,pipe_in = os.pipe() print ("pipe is: ",(pipe_in,pipe_out)) pid=os.fork() #print("forked") if pid<0: print ("fork error") exit() elif pid == 0: #print ("I am child:", str(os.getpid())) os.close(pipe_in) del pipe_in else: #print ("I am parent:", str(os.getpid())) #print ("parent: child's pid="+str(pid)) os.close(pipe_out) del pipe_out
- Plumb Pipe
#!/usr/bin/python3 import os stdout_id=1 stdin_id=0 pipe_out,pipe_in = os.pipe() print ("pipe is: ",(pipe_in,pipe_out)) pid=os.fork() print("forked") if pid<0: print ("fork error") exit() elif pid == 0: #print ("I am child:", str(os.getpid())) os.close(pipe_in) del pipe_in os.dup2(pipe_out, stdin_id) else: #print ("I am parent:", str(os.getpid())) #print ("parent: child's pid="+str(pid)) os.close(pipe_out) del pipe_out pipe=os.fdopen(pipe_in,"r") pipe.write("hello, child\n") pipe.write("goodbye, child\n")
- Exec
#!/usr/bin/python3 import os stdout_id=1 stdin_id=0 pipe_out,pipe_in = os.pipe() #print ("pipe is: ",(pipe_in,pipe_out)) pid=os.fork() #print("forked") if pid<0: #print ("fork error") exit() elif pid == 0: #print ("I am child:", str(os.getpid())) os.close(pipe_in) del pipe_in os.dup2(pipe_out, stdin_id) os.execvp("grep",("grep", "hello" )) else: #print ("I am parent:", str(os.getpid())) #print ("parent: child's pid="+str(pid)) os.close(pipe_out) del pipe_out pipe=os.fdopen(pipe_in,"w") pipe.write("hello, child\n") pipe.write("goodbye, child\n")
See creation-of-an-interprocess-pipe for how to do it in C.