From c11d577349b2b3c11d7e75bcd4767189b2bc796d Mon Sep 17 00:00:00 2001 From: cody burkard Date: Wed, 27 Aug 2014 08:44:08 -0700 Subject: [PATCH 1/2] parse pid printed when backgrounding a process --- mininet/net.py | 2 +- mininet/node.py | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/mininet/net.py b/mininet/net.py index a4477d3..8a6acdc 100755 --- a/mininet/net.py +++ b/mininet/net.py @@ -740,7 +740,7 @@ class Mininet( object ): sleep( 1 ) outputs.append( quietRun( cmd ).strip() ) for h in hosts: - h.cmd( 'kill %1' ) + h.cmd( 'kill $!' ) cpu_fractions = [] for test_output in outputs: # Split by line. Ignore first line, which looks like this: diff --git a/mininet/node.py b/mininet/node.py index 19f26d7..4461a40 100644 --- a/mininet/node.py +++ b/mininet/node.py @@ -160,6 +160,7 @@ class Node( object ): self.pollOut.poll() self.waiting = False self.cmd( 'stty -echo' ) + self.cmd( 'set +m' ) def cleanup( self ): "Help python collect its garbage." @@ -241,9 +242,13 @@ class Node( object ): if printPid and not isShellBuiltin( cmd ): if len( cmd ) > 0 and cmd[ -1 ] == '&': # print ^A{pid}\n so monitor() can set lastPid - cmd += ' printf "\\001%d\n" $! \n' + cmd += ' printf "\\001%d\\012" $! ' else: cmd = 'mnexec -p ' + cmd + # if a builtin command is backgrounded, it yields a PID + elif isShellBuiltin( cmd ): + if len( cmd ) > 0 and cmd[ -1 ] == '&': + cmd += ' printf "\\001%d\\012" $! ' self.write( cmd + '\n' ) self.lastPid = None self.waiting = True @@ -258,9 +263,13 @@ class Node( object ): timeoutms: timeout in ms or None to wait indefinitely.""" self.waitReadable( timeoutms ) data = self.read( 1024 ) + pidre = r'\[\d+\] \d+\r\n' # Look for PID marker = chr( 1 ) + r'\d+\r\n' if findPid and chr( 1 ) in data: + # suppress the job and PID of a backgrounded command + if re.findall( pidre, data ): + data = re.sub( pidre, '', data ) # Marker can be read in chunks; continue until all of it is read while not re.findall( marker, data ): data += self.read( 1024 ) @@ -1262,7 +1271,7 @@ class Controller( Node ): if self.cdir is not None: self.cmd( 'cd ' + self.cdir ) self.cmd( self.command + ' ' + self.cargs % self.port + - ' 1>' + cout + ' 2>' + cout + '&' ) + ' 1>' + cout + ' 2>' + cout + ' &' ) self.execed = False def stop( self ): From ce1673803f274fe87814dea15bc10f3647489110 Mon Sep 17 00:00:00 2001 From: cody burkard Date: Wed, 27 Aug 2014 11:07:22 -0700 Subject: [PATCH 2/2] clean up logic for backgrounded processes --- mininet/node.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/mininet/node.py b/mininet/node.py index 4461a40..c6ad614 100644 --- a/mininet/node.py +++ b/mininet/node.py @@ -239,16 +239,12 @@ class Node( object ): # Replace empty commands with something harmless cmd = 'echo -n' self.lastCmd = cmd - if printPid and not isShellBuiltin( cmd ): - if len( cmd ) > 0 and cmd[ -1 ] == '&': - # print ^A{pid}\n so monitor() can set lastPid - cmd += ' printf "\\001%d\\012" $! ' - else: - cmd = 'mnexec -p ' + cmd - # if a builtin command is backgrounded, it yields a PID - elif isShellBuiltin( cmd ): - if len( cmd ) > 0 and cmd[ -1 ] == '&': - cmd += ' printf "\\001%d\\012" $! ' + # if a builtin command is backgrounded, it still yields a PID + if len( cmd ) > 0 and cmd[ -1 ] == '&': + # print ^A{pid}\n so monitor() can set lastPid + cmd += ' printf "\\001%d\\012" $! ' + elif printPid and not isShellBuiltin( cmd ): + cmd = 'mnexec -p ' + cmd self.write( cmd + '\n' ) self.lastPid = None self.waiting = True