understanding how IPC goes in programming(python) -


the text i'm learning gives me relatively complex example, can't seem understand.

i've commented @ end of program file uses , provide link repl, yet, don't know how connect files on repl.

i understand how os.dup2() or opening or closing pipes work, but, ipc(inter process communication not easy understand.

it sets 2 pipes before fork, later, seems using arbitrary pipes, they're not using pipes associated them. so, basically, don't understand how child sending data parent , vice versa.

and in child process if exec command executed, entirely sets new program in current address space, what's use fo os.dup2?

#!/usr/bin/python3.6    import os, sys  def fork(prog, *args):     stdinfd = sys.stdin.fileno()     stdoutfd = sys.stdout.fileno()      parentstdin, childstdout = os.pipe()    #make 2 ipc pipe channels     childstdin, parentstdout = os.pipe()    #pipe returns(inputfd, outputfd)      pid = os.fork()      if pid:         os.close(childstdout)         os.close(childstdin)         os.dup2(parentstdin, stdinfd)         os.dup2(parentstdout, stdoutfd)      else:         os.close(parentstdin)         os.close(parentstdout)         os.dup2(childstdin, stdinfd)         os.dup2(childstdout, stdoutfd)          args = (prog, )+args         os.execvp(prog, args)         assert false, 'evecvp failed!'   if __name__ == '__main__':      pid = os.getpid()     fork('python3.6', 'pipes', 'spam')   #fork child program      print('parent process', pid)     sys.stdout.flush()     reply = input()  #confusion!!! child stdout     sys.stderr.write("parent got: '%s'\n"%reply)          print("hello 2 parent", mypid)     sys.stdout.flush()     reply = sys.stdin.readline()      sys.stderr.write('parent got: "%s"\n'%reply[:-1])     ##the contents of pipes.py ## ###!/usr/bin/python3.6 ## ##import os,time,sys ## ##mypid = os.getpid() ##parentpid = os.getppid()    #get parent process' id ## ##sys.stderr.write('child {} of {} got arg: "{}"\n'.format(mypid, parentpid, sys.argv[1])) ## ## ##for in range(2): ##    time.sleep(3)           #make parent process wait sleeping here ##    recv = input()          #stdin tied pipe: comes parent's stdout ##    time.sleep(3) ## ##    send = 'child {} got: [{}]'.format(mypid, recv) ##    print(send) ##    sys.stdout.flush() ## ##     ##     


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -