0
此代碼在Python中創建pty(僞終端)。我曾評論說,我不明白有人可以爲我解釋這個Python代碼嗎?
import os,select
pid, master_fd =os.forkpty() #I guess this function return the next available pid and fd
args=['/bin/bash']
if pid == 0:#I have no I idea what this if statement does, however I have noticed that it get executed twice
os.execlp('/bin/bash',*args)
while 1:
r,w,e=select.select([master_fd,0], [], [])
for i in r:
if i==master_fd:
data=os.read(master_fd, 1024)
"""Why I cannot do something like
f=open('/dev/pts/'+master_fd,'r')
data=f.read()"""
os.write(1, data) # What does 1 mean???
elif i==0:
data = os.read(0, 1024)
while data!='':
n = os.write(master_fd, data)
data = data[n:]
很好的解釋,但父母和孩子並行運行? – Gray 2014-09-02 11:02:03
那麼孩子的標準輸出和標準輸入會發生什麼,他們會被重定向到父母? – Gray 2014-09-02 11:25:01
這兩個過程是*相同*。這意味着它們具有相同的'stdout'等。如果你想重定向,那麼你必須自己設置它。 – Adam 2014-09-02 16:10:04