Hello,
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 code. 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 before continuing to pclose.
code :
#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 = 0, noOfPopen = 0, noPclose = 0,flag = 0;
#include <stdio.h>
#include <wait.h>
#include <pthread.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#define SIM_LOG printf
int waitDetect = 0, noOfPopen = 0, noPclose = 0,flag = 0;
void* waitThread(void*)
{
while(true)
{
{
while(true)
{
pid_t pid = waitpid(0,NULL,WUNTRACED); //will hang
//pid_t pid;
// ! pid = waitpid(-1,NULL,WUNTRACED);
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 = 1;
}
}
}
//pid_t pid;
// ! pid = waitpid(-1,NULL,WUNTRACED);
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 = 1;
}
}
}
bool print(char* cmd)
{
printf("print : %d", pthread_self());
char buf1[1000], array[100];
float cpu,mem;
int bufsize= 1000;
char *buf = buf1;
FILE *fptr = popen(cmd, "r");
noOfPopen++;
{
printf("print : %d", pthread_self());
char buf1[1000], array[100];
float cpu,mem;
int bufsize= 1000;
char *buf = buf1;
FILE *fptr = popen(cmd, "r");
noOfPopen++;
// printf("waitpid interfered \n");
//sleep(5);
//sleep(5);
fgets(buf, bufsize, fptr) ;
printf("start\n");
SIM_LOG("*********START***************");
while(feof(fptr) ==0)
{
printf("buf is\n");
SIM_LOG(" checkProcess :: The buffer is %s", buf);
printf("start\n");
SIM_LOG("*********START***************");
while(feof(fptr) ==0)
{
printf("buf is\n");
SIM_LOG(" checkProcess :: The buffer is %s", buf);
if((*buf == '\t') || (*buf == ' ') || (*buf == '\0'))
{
while (*buf != ' ' && *buf != '\t' && *buf != '\0') buf++;
while (*buf == ' ' || *buf == '\t') buf++;
}
sscanf(buf, "%f", &cpu);
printf("cpu \n");
SIM_LOG(" getCpuMem :: The CPU Utilization is %f\n", cpu);
{
while (*buf != ' ' && *buf != '\t' && *buf != '\0') buf++;
while (*buf == ' ' || *buf == '\t') buf++;
}
sscanf(buf, "%f", &cpu);
printf("cpu \n");
SIM_LOG(" getCpuMem :: The CPU Utilization is %f\n", cpu);
while (*buf != ' ' && *buf != '\t' && *buf != '\0') buf++;
while (*buf == ' ' || *! buf == '\t') buf++;
while (*buf == ' ' || *! buf == '\t') buf++;
sscanf(buf, "%f", &mem);
printf("mem\n");
SIM_LOG(" getCpuMem :: The mem utilization is %f\n", mem);
while (*buf != ' ' && *buf != '\t' && *buf != '\0') buf++;
while (*buf == ' ' || *buf == '\t') buf++;
while (*buf == ' ' || *buf == '\t') buf++;
sscanf(buf, "%s", array);
printf("array \n");
SIM_LOG(" getCpuMem :: The process name is %s\n", array);
printf("array \n");
SIM_LOG(" getCpuMem :: The process name is %s\n", array);
if (strcmp("bash", array) ==0)
{
break;
}
fgets(buf, bufsize, fptr);
}
printf("Before PCLOSE\n");
//while(flag == 0);
//flag = 0;
//sleep (1);
pclose(fptr);
// fclose(fptr);
printf("After PCLOSE\n");
noPclose++;
SIM_LOG("*********END***************");
return true;
}
{
break;
}
fgets(buf, bufsize, fptr);
}
printf("Before PCLOSE\n");
//while(flag == 0);
//flag = 0;
//sleep (1);
pclose(fptr);
// fclose(fptr);
printf("After PCLOSE\n");
noPclose++;
SIM_LOG("*********END***************");
return true;
}
main()
{
char *cmd = "ps -eo %cpu,%mem,cmd | grep \"bash\"";
pthread_t tid;
//sim_log_init("test",".");
if ((errno = pthread_create(&tid,NULL, &waitThread, NULL)) != 0)
perror("pthread_create");
{
char *cmd = "ps -eo %cpu,%mem,cmd | grep \"bash\"";
pthread_t tid;
//sim_log_init("test",".");
if ((errno = pthread_create(&tid,NULL, &waitThread, NULL)) != 0)
perror("pthread_create");
pid_t pid = fork();
if(pid == 0)
{
sleep(20000);
}
while(true)
{
print(cmd);
//sleep(5);
}
}
2 Thread -161203280 (LWP 3205) 0x0014d7a2 in _dl_sysinfo_int80 ()
from /lib/ld-linux.so.2
1 Thread -153901376 (LWP 3201) 0x0807e415 in _p1922static ()
from /lib/ld-linux.so.2
1 Thread -153901376 (LWP 3201) 0x0807e415 in _p1922static ()
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/modtestserver/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/modtestserver/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/modtestserver/lib/tls/libc.so.6_pure_p3_c0_104011552_269-1667smp_32
#5 0x0808099a in print (cmd=0x808d888 "ps -eo %cpu,%mem,cmd | grep \"bash\"")
at test2.c:98
#6 0x08080bd9 in main () at test2.c:121
#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/modtestserver/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/modtestserver/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/modtestserver/lib/tls/libc.so.6_pure_p3_c0_104011552_269-1667smp_32
#5 0x0808099a in print (cmd=0x808d888 "ps -eo %cpu,%mem,cmd | grep \"bash\"")
at test2.c:98
#6 0x08080bd9 in main () at test2.c:121
bt on thread 2 gave :
#0 0x0014d7a2 in _dl_sysinfo_int80 () from /lib/ld-linux.so.2
#1 0xf6e853e6 in __wait4 ()
Yahoo! Travel
Find great deals to the top 10 hottest destinations!
_______________________________________________ Redhat-devel-list mailing list Redhat-devel-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/redhat-devel-list