Problem with plose or with waitpid !!!
hpranava varna <[email protected]> Tue, 21 Mar 2006 21:47:25 -0800 (PST)
| Newsgroups | gmane.linux.redhat.devel |
|---|---|
| Message-ID | <[email protected]> |
--===============1680731304==
Content-Type: multipart/alternative; boundary="0-1854838988-1143006445=:42590"
Content-Transfer-Encoding: 7bit
--0-1854838988-1143006445=:42590
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
Hello,
=20
I executed the following code, but it generally hangs pclose(), The =
problem occured with fclose as well. I am clueless abt the error in the c=
ode. I am using Fedora and redhat linux. Can you help me find the error ?=
. It doesnt hang if the main thread waits for the flag to be set to 1 bef=
ore continuing to pclose.
=20
code :
=20
#include <unistd.h>
#include <stdio.h>
#include <wait.h>
#include <pthread.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#define SIM_LOG printf
int waitDetect =3D 0, noOfPopen =3D 0, noPclose =3D 0,flag =3D 0;
void* waitThread(void*)
{
while(true)
{
pid_t pid =3D waitpid(0,NULL,WUNTRACED); //will hang
//pid_t pid;
// pid =3D waitpid(-1,NULL,WUNTRACED);=20
printf("wait : %d",(int)pthread_self());
if (pid < 0)
{
// perror("waitpid");
}
else if (pid > 0)
{
waitDetect++;
printf("The child pid is %d, wait detected %d children",
pid, waitDetect);
printf("The no of popens is %d , pclose : %d ", noOfPopen,noPclose);
flag =3D 1;
}=20
}
}
=20
bool print(char* cmd)
{
printf("print : %d", pthread_self());
char buf1[1000], array[100];
float cpu,mem;
int bufsize=3D 1000;
char *buf =3D buf1;
FILE *fptr =3D popen(cmd, "r");
noOfPopen++;
// printf("waitpid interfered \n");
//sleep(5);
fgets(buf, bufsize, fptr) ;
printf("start\n");
SIM_LOG("*********START***************");
=20
while(feof(fptr) =3D=3D0)
{
printf("buf is\n");
SIM_LOG(" checkProcess :: The buffer is %s", buf);
if((*buf =3D=3D '\t') || (*buf =3D=3D ' ') || (*buf =3D=3D '\0'))
{
while (*buf !=3D ' ' && *buf !=3D '\t' && *buf !=3D '\0') buf++;
while (*buf =3D=3D ' ' || *buf =3D=3D '\t') buf++;
}
sscanf(buf, "%f", &cpu);
printf("cpu \n");
SIM_LOG(" getCpuMem :: The CPU Utilization is %f\n", cpu);
while (*buf !=3D ' ' && *buf !=3D '\t' && *buf !=3D '\0') buf++;
while (*buf =3D=3D ' ' || *buf =3D=3D '\t') buf++;
=20
sscanf(buf, "%f", &mem);
printf("mem\n");
SIM_LOG(" getCpuMem :: The mem utilization is %f\n", mem);
while (*buf !=3D ' ' && *buf !=3D '\t' && *buf !=3D '\0') buf++;
while (*buf =3D=3D ' ' || *buf =3D=3D '\t') buf++;
sscanf(buf, "%s", array);
printf("array \n");
SIM_LOG(" getCpuMem :: The process name is %s\n", array);
if (strcmp("bash", array) =3D=3D0)
{
break;
}
fgets(buf, bufsize, fptr);
}
printf("Before PCLOSE\n");
//while(flag =3D=3D 0);
//flag =3D 0;
//sleep (1);
pclose(fptr);
// fclose(fptr);
printf("After PCLOSE\n");
noPclose++;
SIM_LOG("*********END***************");
return true;
}
main()
{
char *cmd =3D "ps -eo %cpu,%mem,cmd | grep \"bash\"";
pthread_t tid;
//sim_log_init("test",".");
if ((errno =3D pthread_create(&tid,NULL, &waitThread, NULL)) !=3D 0)
perror("pthread_create");
=20
pid_t pid =3D fork();
if(pid =3D=3D 0)
{
sleep(20000);
}
while(true)
{
print(cmd);
//sleep(5);
}
=20
}
=20
=20
=20
2 Thread -161203280 (LWP 3205) 0x0014d7a2 in _dl_sysinfo_int80 ()
from /lib/ld-linux.so.2
1 Thread -153901376 (LWP 3201) 0x0807e415 in _p1922static ()
=20
bt on thread 1 gave :
#0 0x0807e415 in _p1922static ()
#1 0x0805b834 in __waitpid_nocancel ()
#2 0xf6dfc830 in _IO_proc_close@@GLIBC_2.1 ()
from /opt/rational/releases/purify.i386_linux2.2003a.06.15/cache/modte=
stserver/lib/tls/libc.so.6_pure_p3_c0_104011552_269-1667smp_32
#3 0xf6e128ef in _IO_new_file_close_it ()
from /opt/rational/releases/purify.i386_linux2.2003a.06.15/cache/modte=
stserver/lib/tls/libc.so.6_pure_p3_c0_104011552_269-1667smp_32
#4 0xf6df730c in fclose@@GLIBC_2.1 ()
from /opt/rational/releases/purify.i386_linux2.2003a.06.15/cache/modte=
stserver/lib/tls/libc.so.6_pure_p3_c0_104011552_269-1667smp_32
#5 0x0808099a in print (cmd=3D0x808d888 "ps -eo %cpu,%mem,cmd | grep \"b=
ash\"")
at test2.c:98
#6 0x08080bd9 in main () at test2.c:121
=20
=20
bt on thread 2 gave :
=20
#0 0x0014d7a2 in _dl_sysinfo_int80 () from /lib/ld-linux.so.2
#1 0xf6e853e6 in __wait4 ()
=20
=20
=20
=09
---------------------------------
Yahoo! Travel
Find great deals to the top 10 hottest destinations!
--0-1854838988-1143006445=:42590
Content-Type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
<div>Hello,</div> <div> </div> <div> I executed the fo=
llowing code, but it generally hangs pclose(), The problem occured with f=
close as well. I am clueless abt the error in the code. I am using F=
edora and redhat linux. Can you help me find the error ?. It do=
esnt hang if the main thread waits for the flag to be set to 1 before con=
tinuing to pclose.</div> <div> </div> <div>code :</div> <div>&nbs=
p;</div> <div> #include <unistd.h><BR>#include <stdio.h>=
;<BR>#include <wait.h><BR>#include <pthread.h><BR>#include &l=
t;errno.h><BR>#include <string.h><BR>#include <signal.h><B=
R>#define SIM_LOG printf<BR><BR>int waitDetect =3D 0, noOfPopen =3D 0, no=
Pclose =3D 0,flag =3D 0;</div> <div>void* waitThread(void*)<BR>{<BR>&nbs=
p;while(true)<BR> {</div> <div> pid_t pid =3D waitpid(0,NULL,W=
UNTRACED); //will hang<BR> //pi=
d_t pid;<BR>// !
pid =3D waitpid(-1,NULL,WUNTRACED); <BR> printf("wait : %d",(int)pt=
hread_self());<BR> if (pid < 0)<BR> {<BR>// perro=
r("waitpid");<BR> }<BR> else if (pid > 0)<BR> {<BR>&nbs=
p; waitDetect++;<BR> printf("The child pid is %d, wait de=
tected %d children",<BR> pid, waitDetect);<BR>&nb=
sp; printf("The no of popens is %d , pclose : %d ", noOfPopen,noPclo=
se);<BR> flag =3D 1;<BR> } <BR> }<BR>}</div> <=
div> </div> <div>bool print(char* cmd)<BR>{<BR> printf(=
"print : %d", pthread_self());<BR> char buf1[1000], array[100=
];<BR> float cpu,mem;<BR> int bufsize=3D 1000;<BR=
> char *buf =3D buf1;<BR> FILE *fptr =3D popen(cm=
d, "r");<BR> noOfPopen++;</div> <div>// printf("waitpid=
interfered \n");<BR> //sleep(5);</div> <div> fgets(buf=
, bufsize, fptr) ;<BR>
printf("start\n");<BR> SIM_LOG("*********START**************=
*");<BR> <BR> while(feof(fptr) =3D=3D0)<BR> =
{<BR> printf("buf is\n");<BR> SIM_LOG(=
" checkProcess :: The buffer is %s", buf);</div> <div> =
if((*buf =3D=3D '\t') || (*buf =3D=3D ' ') || (*buf =3D=3D '=
\0'))<BR> {<BR> &nbs=
p; while (*buf !=3D ' ' && *buf !=3D '\t' &=
& *buf !=3D '\0') buf++;<BR>  =
; while (*buf =3D=3D ' ' || *buf =3D=3D '\t') buf++;<BR>  =
; }<BR> sscanf(buf, "%f", &cpu);<BR> pri=
ntf("cpu \n");<BR> SIM_LOG(" getCpuMem :: T=
he CPU Utilization is %f\n", cpu);</div> <div> &n=
bsp; while (*buf !=3D ' ' && *buf !=3D '\t' && *buf=
!=3D '\0') buf++;<BR> while (*buf =3D=3D '=
' || *!
buf =3D=3D
'\t') buf++;</div> <div><BR> sscanf(buf, =
"%f", &mem);<BR> printf("mem\n");<BR> &nb=
sp; SIM_LOG(" getCpuMem :: The mem utilization is %f\n", mem);</div> <di=
v> while (*buf !=3D ' ' && *buf !=3D=
'\t' && *buf !=3D '\0') buf++;<BR> =
while (*buf =3D=3D ' ' || *buf =3D=3D '\t') buf++;</div> <div> &nb=
sp; sscanf(buf, "%s", array);<BR> printf("array \n=
");<BR> SIM_LOG(" getCpuMem :: The process =
name is %s\n", array);</div> <div> if (str=
cmp("bash", array) =3D=3D0)<BR> {<BR> =
break;<BR> &n=
bsp; }<BR> fgets(buf, bufsize, fptr);=
<BR> }<BR> printf("Before PCLOSE\n");<BR> //while(flag =3D=
=3D 0);<BR> //flag =3D
0;<BR> //sleep (1);<BR> &=
nbsp; pclose(fptr);<BR>// fclose(fptr);<BR> =
printf("After PCLOSE\n");<BR> noPclose++;<BR> SIM=
_LOG("*********END***************");<BR> return true;<BR>}</d=
iv> <div>main()<BR>{<BR> char *cmd =3D "ps -eo %cpu,%mem,cmd | grep=
\"bash\"";<BR> pthread_t tid;<BR> //sim_log_init(=
"test",".");<BR> if ((errno =3D pthread_create(&tid,NULL, &w=
aitThread, NULL)) !=3D 0)<BR> perror("pthread_create");</div> =
<div><BR> pid_t pid =3D fork()=
;<BR> if(pid =3D=3D 0)<BR> =
; {<BR> =
sleep(20000);<BR> &=
nbsp; }<BR> while(true)<BR> {<BR> print(=
cmd);<BR> //sleep(5);<BR> }</div>=20
<div><BR>}</div> <div> </div> <div> </div> <div> </div=
> <div> 2 Thread -161203280 (LWP 3205) 0x0014d7a2 in _dl_sysi=
nfo_int80 ()<BR> from /lib/ld-linux.so.2<BR> 1 Thread -=
153901376 (LWP 3201) 0x0807e415 in _p1922static ()</div> <div><BR>=
bt on thread 1 gave :</div> <div>#0 0x0807e415 in _p1922static ()<=
BR>#1 0x0805b834 in __waitpid_nocancel ()<BR>#2 0xf6dfc830 in=
<A href=3D"mailto:_IO_proc_close@@GLIBC_2.1">_IO_proc_close@@GLIBC_2.1</=
A> ()<BR> from /opt/rational/releases/purify.i386_linux2.2003=
a.06.15/cache/modtestserver/lib/tls/libc.so.6_pure_p3_c0_104011552_269-16=
67smp_32<BR>#3 0xf6e128ef in _IO_new_file_close_it ()<BR> &nbs=
p; from /opt/rational/releases/purify.i386_linux2.2003a.06.15/cache/modte=
stserver/lib/tls/libc.so.6_pure_p3_c0_104011552_269-1667smp_32<BR>#4 =
; 0xf6df730c in <A href=3D"mailto:fclose@@GLIBC_2.1">fclose@@GLIBC_2.1</A=
> ()<BR> from
/opt/rational/releases/purify.i386_linux2.2003a.06.15/cache/modtestserve=
r/lib/tls/libc.so.6_pure_p3_c0_104011552_269-1667smp_32<BR>#5 0x080=
8099a in print (cmd=3D0x808d888 "ps -eo %cpu,%mem,cmd | grep \"bash\"")<B=
R> at test2.c:98<BR>#6 0x08080bd9 in main () at t=
est2.c:121</div> <div> </div> <div> </div> <div>bt on thread=
2 gave :</div> <div><BR>#0 0x0014d7a2 in _dl_sysinfo_int80 () fro=
m /lib/ld-linux.so.2<BR>#1 0xf6e853e6 in __wait4 ()</div> <div>&nb=
sp;</div> <div> </div> <div> </div><p>
<hr size=3D1>Yahoo! Travel<br>=20
<a href=3D"http://us.lrd.yahoo.com/_ylc=3DX3oDMTFscDlocTFiBF9TAzMyOTc1MDI=
EX3MDMjcxOTQ4MQRwb3MDMgRzZWMDbWFpbC1mb290ZXIEc2xrA3l0LXR0/SIG=3D12hqieud9=
/**http%3a//leisure.travelocity.com/Promotions/0,,YHOE%7c1381%7cvacs_main=
,00.html">Find =20
great deals</a> to the top 10 hottest destinations!
--0-1854838988-1143006445=:42590--
--===============1680731304==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
Redhat-devel-list mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/redhat-devel-list
--===============1680731304==--