[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Problem with waiting for child processes within loop
From: |
mhagger |
Subject: |
Problem with waiting for child processes within loop |
Date: |
19 Apr 2001 19:58:57 -0000 |
Configuration Information [Automatically generated, do not change]:
Machine: i386
OS: Linux
Compiler: gcc
Compilation CFLAGS: -DHAVE_VFPRINTF -DHAVE_UNISTD_H -DHAVE_STDLIB_H
-DHAVE_LIMITS_H -DHAVE_GETGROUPS -DHAVE_RESOURCE -DHAVE_SYS_PARAM
-DVOID_SIGHANDLER -DHAVE_GETDTABLESIZE -DHAVE_BCOPY -DHAVE_GETPW_DECLS
-DHAVE_GETHOSTNAME -DHAVE_WAIT_H -DHAVE_DUP2 -DHAVE_STRERROR
-DHAVE_DIRENT_H -DHAVE_STRING_H -DHAVE_VARARGS_H -DHAVE_STRCHR
-DHAVE_DEV_FD -Di386 -DLinux -DSHELL -DHAVE_ALLOCA -DHAVE_ALLOCA_H
-O2 -march=i386 -mcpu=i686
uname output: Linux freak 2.2.14-12-custom #3 Mon May 15 00:12:59 EDT 2000 i686
unknown
Bash Version: 1.14
Patch Level: 7
(I believe this is the copy of bash installed with RedHat Linux 6.2.)
Description:
I start multiple background processes, then try to wait for them in a
bash for loop. One of the wait statements fails with the message
"wait: pid 2551 is not a child of this shell" when it should actually
succeed.
Repeat-By:
The following script produces the error on my machine:
#! /bin/bash
sleep 2 &
pid1=$!
echo "pid of task 1 is $pid1"
sleep 1 &
pid2=$!
echo "pid of task 2 is $pid2"
for pid in $pid1 $pid2
do
echo "waiting for $pid to finish"
wait $pid
done
I get the following output:
$ ./wait-test
pid of task 1 is 2550
pid of task 2 is 2551
waiting for 2550 to finish
waiting for 2551 to finish
./wait-test: wait: pid 2551 is not a child of this shell
If I unroll the for loop (i.e., "wait $pid1; wait $pid2") or wait for
the two children in the same wait statement (i.e., "wait $pid1 $pid2")
then the error goes away. If I change the second sleep statement to
"sleep 3" (i.e., so that the first child process finishes first), the
problem goes away.
Yours,
Michael
--
Michael Haggerty
mhagger@alum.mit.edu
- Problem with waiting for child processes within loop,
mhagger <=