os.setpgid(pid, gid) problems

Isaac To Kar Keung kkto at csis.hku.hk
Tue May 29 21:57:56 EDT 2001


>>>>> "Bragi" == Bragi Baldursson <bragi at hks.com> writes:

    Bragi> The problem comes when I try to set the process group leader.  As
    Bragi> a sample I have created 3 shell scripts which do nothing but
    Bragi> sleep.  I try to set the process leader but it does not work!.

Because the pgid cannot be set after the sub-process execute an
exec... function.  So in essense you'll need to do something in the
following line:

  import os
  if (os.fork()==0):
    os.setpgid(0, 0)
    os.execl("./csh1")
  os.wait()

If I run this I get:

This is csh1 PID =  8774
This is csh2 PID =  8775
This is csh3 PID =  8777

Now I can say "kill -9 -8774" to kill everything.  On the other hand, if the
parent needs to assume that the setpgid is always done correctly before
continuing, it need to do the following instead:

  import os
  result = os.fork()
  if (result==0):
    os.setpgid(0, 0)
    os.execl("./csh1")
  if (result<0):
    raise ...
  os.setpgid(result, 0)
  # other operations including os.wait()

    Bragi> Then I finally kill the first shell process and that works
    Bragi> fine..........

It didn't.  The other processes are not killed.

Regards,
Isaac.



More information about the Python-list mailing list