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>&nbsp;</div>  <div>&nbsp;&nbsp; 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&nbsp;F=
edora and redhat linux. Can you help&nbsp;me find the&nbsp;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>&nbsp;</div>  <div>code :</div>  <div>&nbs=
p;</div>  <div>&nbsp;#include &lt;unistd.h&gt;<BR>#include &lt;stdio.h&gt=
;<BR>#include &lt;wait.h&gt;<BR>#include &lt;pthread.h&gt;<BR>#include &l=
t;errno.h&gt;<BR>#include &lt;string.h&gt;<BR>#include &lt;signal.h&gt;<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>&nbsp;{</div>  <div>&nbsp;pid_t pid =3D waitpid(0,NULL,W=
UNTRACED); //will hang<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //pi=
d_t pid;<BR>//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;!
 &nbsp;
 pid =3D waitpid(-1,NULL,WUNTRACED); <BR>&nbsp;printf("wait : %d",(int)pt=
hread_self());<BR>&nbsp;if (pid &lt; 0)<BR>&nbsp;{<BR>//&nbsp;&nbsp;perro=
r("waitpid");<BR>&nbsp;}<BR>&nbsp;else if (pid &gt; 0)<BR>&nbsp;{<BR>&nbs=
p;&nbsp;waitDetect++;<BR>&nbsp;&nbsp;printf("The child pid is %d, wait de=
tected %d children",<BR>&nbsp;&nbsp;&nbsp;&nbsp; pid, waitDetect);<BR>&nb=
sp;&nbsp;printf("The no of popens is %d , pclose : %d ", noOfPopen,noPclo=
se);<BR>&nbsp;&nbsp;flag =3D 1;<BR>&nbsp;}&nbsp;<BR>&nbsp;}<BR>}</div>  <=
div>&nbsp;</div>  <div>bool print(char* cmd)<BR>{<BR>&nbsp;&nbsp; printf(=
"print : %d", pthread_self());<BR>&nbsp;&nbsp; char buf1[1000], array[100=
];<BR>&nbsp;&nbsp; float cpu,mem;<BR>&nbsp;&nbsp; int bufsize=3D 1000;<BR=
>&nbsp;&nbsp; char *buf =3D buf1;<BR>&nbsp;&nbsp; FILE *fptr =3D popen(cm=
d, "r");<BR>&nbsp;&nbsp; noOfPopen++;</div>  <div>//&nbsp;printf("waitpid=
 interfered \n");<BR>&nbsp;//sleep(5);</div>  <div>&nbsp;&nbsp; fgets(buf=
, bufsize, fptr) ;<BR>&nbsp;&nbsp;
 printf("start\n");<BR>&nbsp;&nbsp; SIM_LOG("*********START**************=
*");<BR>&nbsp;<BR>&nbsp;&nbsp; while(feof(fptr) =3D=3D0)<BR>&nbsp;&nbsp; =
{<BR>&nbsp;printf("buf is\n");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SIM_LOG(=
" checkProcess :: The buffer is %s", buf);</div>  <div>&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp; if((*buf =3D=3D '\t') || (*buf =3D=3D ' ') || (*buf =3D=3D '=
\0'))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp; while (*buf !=3D ' ' &amp;&amp; *buf !=3D '\t' &amp;=
&amp; *buf !=3D '\0') buf++;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp; while (*buf =3D=3D ' ' || *buf =3D=3D '\t') buf++;<BR>&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp; }<BR>&nbsp; sscanf(buf, "%f", &amp;cpu);<BR>&nbsp;pri=
ntf("cpu \n");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SIM_LOG(" getCpuMem :: T=
he CPU Utilization is&nbsp; %f\n", cpu);</div>  <div>&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp; while (*buf !=3D ' ' &amp;&amp; *buf !=3D '\t' &amp;&amp; *buf=
 !=3D '\0') buf++;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (*buf =3D=3D '=
 ' || *!
 buf =3D=3D
 '\t') buf++;</div>  <div><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sscanf(buf, =
"%f", &amp;mem);<BR>&nbsp;printf("mem\n");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp; SIM_LOG(" getCpuMem :: The mem utilization is %f\n", mem);</div>  <di=
v>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (*buf !=3D ' ' &amp;&amp; *buf !=3D=
 '\t' &amp;&amp; *buf !=3D '\0') buf++;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
 while (*buf =3D=3D ' ' || *buf =3D=3D '\t') buf++;</div>  <div>&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp; sscanf(buf, "%s", array);<BR>&nbsp;printf("array \n=
");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SIM_LOG(" getCpuMem :: The process =
name is %s\n", array);</div>  <div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (str=
cmp("bash", array) =3D=3D0)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<BR>&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fgets(buf, bufsize, fptr);=
<BR>&nbsp; }<BR>&nbsp;printf("Before PCLOSE\n");<BR>&nbsp;//while(flag =3D=
=3D 0);<BR>&nbsp;//flag =3D
 0;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //sleep (1);<BR>&nbsp;&=
nbsp; pclose(fptr);<BR>//&nbsp;&nbsp;&nbsp;&nbsp; fclose(fptr);<BR>&nbsp;=
printf("After PCLOSE\n");<BR>&nbsp;&nbsp; noPclose++;<BR>&nbsp;&nbsp; SIM=
_LOG("*********END***************");<BR>&nbsp;&nbsp; return true;<BR>}</d=
iv>  <div>main()<BR>{<BR>&nbsp;char *cmd =3D "ps -eo %cpu,%mem,cmd | grep=
 \"bash\"";<BR>&nbsp;pthread_t tid;<BR>&nbsp;&nbsp; &nbsp;//sim_log_init(=
"test",".");<BR>&nbsp;if ((errno =3D pthread_create(&amp;tid,NULL, &amp;w=
aitThread, NULL)) !=3D 0)<BR>&nbsp;&nbsp;perror("pthread_create");</div> =
 <div><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pid_t pid =3D fork()=
;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(pid =3D=3D 0)<BR>&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sleep(20000);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp; }<BR>&nbsp;while(true)<BR>&nbsp;{<BR>&nbsp;&nbsp;print(=
cmd);<BR>&nbsp;&nbsp;//sleep(5);<BR>&nbsp;}</div>=20
 <div><BR>}</div>  <div>&nbsp;</div>  <div>&nbsp;</div>  <div>&nbsp;</div=
>  <div>&nbsp;2 Thread -161203280 (LWP 3205)&nbsp; 0x0014d7a2 in _dl_sysi=
nfo_int80 ()<BR>&nbsp;&nbsp; from /lib/ld-linux.so.2<BR>&nbsp; 1 Thread -=
153901376 (LWP 3201)&nbsp; 0x0807e415 in _p1922static ()</div>  <div><BR>=
bt on thread 1 gave :</div>  <div>#0&nbsp; 0x0807e415 in _p1922static ()<=
BR>#1&nbsp; 0x0805b834 in __waitpid_nocancel ()<BR>#2&nbsp; 0xf6dfc830 in=
 <A href=3D"mailto:_IO_proc_close@@GLIBC_2.1">_IO_proc_close@@GLIBC_2.1</=
A> ()<BR>&nbsp;&nbsp; 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&nbsp; 0xf6e128ef in _IO_new_file_close_it ()<BR>&nbsp;&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&nbsp=
; 0xf6df730c in <A href=3D"mailto:fclose@@GLIBC_2.1">fclose@@GLIBC_2.1</A=
> ()<BR>&nbsp;&nbsp; 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&nbsp; 0x080=
8099a in print (cmd=3D0x808d888 "ps -eo %cpu,%mem,cmd | grep \"bash\"")<B=
R>&nbsp;&nbsp;&nbsp; at test2.c:98<BR>#6&nbsp; 0x08080bd9 in main () at t=
est2.c:121</div>  <div>&nbsp;</div>  <div>&nbsp;</div>  <div>bt on thread=
 2 gave :</div>  <div><BR>#0&nbsp; 0x0014d7a2 in _dl_sysinfo_int80 () fro=
m /lib/ld-linux.so.2<BR>#1&nbsp; 0xf6e853e6 in __wait4 ()</div>  <div>&nb=
sp;</div>  <div>&nbsp;</div>  <div>&nbsp;</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==--