473,408 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Problem with threading and execv

Motoma
3,237 Expert 2GB
Hello everyone,

I am having a tiny problem putting together a small script. I tried throwing together a multi-threaded ping utility, but the threads are not behaving the way I anticipated...Perhaps one of you could look and see why this is happening.

Python 2.5.2, Ubuntu 8.04

Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/env python                                                               
  2.  
  3. import os, sys, threading
  4.  
  5. class PingThread(threading.Thread):
  6.     host = ''
  7.  
  8.     def __init__(self, host):
  9.         self.host = host
  10.         threading.Thread.__init__(self)
  11.  
  12.     def run (self):
  13.         os.execv('/bin/ping', ['/bin/ping', self.host])
  14.  
  15. print "PingThread('google.com').start()"
  16. PingThread('google.com').start()
  17. print "PingThread('yahoo.com').start()"
  18. PingThread('yahoo.com').start()
  19. print "Main thread ended."
  20.  
If someone could point out my misuse of the Thread class, I would appreciate it.

Thanks,
Motoma
Sep 8 '08 #1
2 4131
Motoma
3,237 Expert 2GB
It appears that my problem is not with the utilization of the Thread class; rather, my error was with my misunderstanding of the way python's exec*() functions work.

In C, one way to spawn a new process is to fork() and exec*() in the new thread. However, when running exec*() commands in python, it appears that the entire python process is taken over by the exec*() command, not just that particular thread. My solution to this issue is to utilize the os.system() call, rather than the os.exec*() call.

Working example:

Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/env python                                                               
  2.  
  3. import os, sys, threading
  4.  
  5. class PingThread(threading.Thread):
  6.     host = ''
  7.  
  8.     def __init__(self, host):
  9.         self.host = host
  10.         threading.Thread.__init__(self)
  11.  
  12.     def run(self):
  13.         os.system('/bin/ping %s' %(self.host))
  14.  
  15. print "PingThread('google.com').start()"
  16. PingThread('google.com').start()
  17. print "PingThread('yahoo.com').start()"
  18. PingThread('yahoo.com').start()
  19. print "Main thread ended."
  20.  
Sep 8 '08 #2
It appears that my problem is not with the utilization of the Thread class; rather, my error was with my misunderstanding of the way python's exec*() functions work.

In C, one way to spawn a new process is to fork() and exec*() in the new thread. However, when running exec*() commands in python, it appears that the entire python process is taken over by the exec*() command, not just that particular thread. My solution to this issue is to utilize the os.system() call, rather than the os.exec*() call.
[/code]
Actually, these work the same in both C and Python. You are confusing threads and processes. The fork() system call creates a new process, you then must determine which process is the child, and explicitly replace the current program using one of the exec* functions. If you simply launch a new thread, the thread is running as part of the same process, so it will naturally take over the entire process when you call an exec* function. The important thing to understand here is that a Thread is a different construct than a process.

The os.system() call is nice and easy, however, if you want to set up special inter-process communication, you should really be using the fork(), exec*() and potentially dup2(). dup2() allows you to map a file descriptor to another. For example, if you want the new process to read from the parent process instead of stdin, you need to remap its stdin file descriptor to a pipe from the parent process using dup2() before launching the program.

Example (grep data in parent):

Expand|Select|Wrap|Line Numbers
  1. #include os
  2. #include sys
  3.  
  4. (pipe_read, pipe_write) = os.pipe()
  5.  
  6. pid = os.fork()
  7. if pid: #parent process
  8.     os.close( pipe_write )
  9.     os.dup2( pipe_read, sys.stdin.fileno() )
  10.     os.execv('/bin/grep', ['/bin/grep', expression])
  11. else: #parent process
  12.     os.close( pipe_read )
  13.     os.write( pipe_write, text )
  14.  
The above example is not complete, you would need a way to retrieve the data from the child process, potentially via another pipe that redirects its stdout.
Sep 18 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Daniel Bernhardt | last post by:
Hello, my thread calls a program with os.system(). > os.system("/usr/bin/wine /path/to/ultima/online.exe") Ultima Online is starting and i can enter commands and navigate through the game with...
3
by: M Herriott | last post by:
Hello to all - #1: I would have rather used an AND with the choice of newsgroups. I would like to talk to those who subscribe to comp.lang.c++ AND comp.unix.solaris - but I'm stuck with OR....
2
by: New | last post by:
How can I uses a pipe to get the output from a execv() and redirect it to a parent process? in ansi c if possible Thank-you
3
by: Douglas Dillon | last post by:
I am trying to use tiffcp to reconfigure tif files (if they are tiled) to nontiled so I can use existing code to handle them. However, I am having issues providing the arguments. The app is run...
0
by: Nico Kruger | last post by:
I want to execute a command (in this case, and it seems to be significant, a Java program) in a thread in Python. When I execute the java binary in the main python thread, everything runs...
3
by: martin paul | last post by:
Sir please consider the following.... I would like to copy contents from one file (mart1.c) to another file (mart2.c) using execv() function (using command line arguments)..... The command...
2
by: lgomez8801 | last post by:
lgomez8801 Newbie 1 Posts Today 07:37 PM #1 C language "execv" question --------------------------------------------------------------------------------
3
by: CMorgan | last post by:
Hi everybody, I am experiencing an annoying problem with fork() and execv(). In my program I need to launch the "pppd" from a thread, so, I create a new process with fork and then in the child...
1
by: dudeja.rajat | last post by:
Hi, I'm facing problem with the execv command: my command is : os.execv(' C:\Program Files\Subversion\bin\svn ', ( 'list', ' \" http://subversion.stv.abc.com/svn/Eng \" ' ) ) The error I'm...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.