mutux with popen

T

tntelle

I have the following code:

snprintf(VARIABLES_2, sizeof VARIABLES_2, "/usr/bin/cksum
'%s'",ARGV1);
FILE * f_2 = popen(VARIABLES_2, "r");
size_t r_2;
while((r_2 = fread(RETURN_CODE_2, sizeof(char), BUFSIZE_2, f_2)) >
1) {
RETURN_CODE_2[r_2-1] = '\0';
}
pclose(f_2);


but I am worried that this is not thread safe and this is for a multi-
threaded application on AIX 5.3 - compiling with XLC.

I figured i could put a mutex around it, but having not done that
before wasnt 100% sure if i have this correct. Here is what another
friend and I came up with:

if(pthread_mutex_init(&mutex, NULL)){
printf("Unable to init a mutex\n");
return -1;
}
pthread_mutex_lock(&mutex);

snprintf(VARIABLES_2, sizeof VARIABLES_2, "/usr/bin/cksum
'%s'",ARGV1);
FILE * f_2 = popen(VARIABLES_2, "r");
size_t r_2;
while((r_2 = fread(RETURN_CODE_2, sizeof(char), BUFSIZE_2, f_2)) >
1) {
RETURN_CODE_2[r_2-1] = '\0';
}
pclose(f_2);

pthread_mutex_unlock(&mutex);


I would appreciate any comments on this or suggestions for making that
code better... or work =-)

Thank you in advance!
 
K

Keith Thompson

tntelle said:
I have the following code:

snprintf(VARIABLES_2, sizeof VARIABLES_2, "/usr/bin/cksum
'%s'",ARGV1);
FILE * f_2 = popen(VARIABLES_2, "r");
size_t r_2;
while((r_2 = fread(RETURN_CODE_2, sizeof(char), BUFSIZE_2, f_2)) >
1) {
RETURN_CODE_2[r_2-1] = '\0';
}
pclose(f_2);


but I am worried that this is not thread safe and this is for a multi-
threaded application on AIX 5.3 - compiling with XLC.
[...]

Standard C doesn't support threading. You might try posting in
comp.programming.threads.
 
J

jacob navia

tntelle a écrit :
I have the following code:

snprintf(VARIABLES_2, sizeof VARIABLES_2, "/usr/bin/cksum
'%s'",ARGV1);
FILE * f_2 = popen(VARIABLES_2, "r");
size_t r_2;
while((r_2 = fread(RETURN_CODE_2, sizeof(char), BUFSIZE_2, f_2)) >
1) {
RETURN_CODE_2[r_2-1] = '\0';
}
pclose(f_2);


but I am worried that this is not thread safe and this is for a multi-
threaded application on AIX 5.3 - compiling with XLC.

I figured i could put a mutex around it, but having not done that
before wasnt 100% sure if i have this correct. Here is what another
friend and I came up with:

if(pthread_mutex_init(&mutex, NULL)){
printf("Unable to init a mutex\n");
return -1;
}
pthread_mutex_lock(&mutex);

snprintf(VARIABLES_2, sizeof VARIABLES_2, "/usr/bin/cksum
'%s'",ARGV1);
FILE * f_2 = popen(VARIABLES_2, "r");
size_t r_2;
while((r_2 = fread(RETURN_CODE_2, sizeof(char), BUFSIZE_2, f_2)) >
1) {
RETURN_CODE_2[r_2-1] = '\0';
}
pclose(f_2);

pthread_mutex_unlock(&mutex);


I would appreciate any comments on this or suggestions for making that
code better... or work =-)

Thank you in advance!

Do not worry about the performance hits. You are going to make
a popen anyway what means a call to the OS, then doing an fread
several times... Those calls are so expensive ANYWAY that the small
amount of nanoseconds of the mutex do not make any difference.

But (more important) what is "VARIABLES_2"???

If they are globals I would surely INCLUDE them within the
mutex protected part!
 
T

tntelle

tntelle a écrit :






I have the following code:
  snprintf(VARIABLES_2, sizeof VARIABLES_2, "/usr/bin/cksum
'%s'",ARGV1);
  FILE * f_2 = popen(VARIABLES_2, "r");
  size_t r_2;
  while((r_2 = fread(RETURN_CODE_2, sizeof(char), BUFSIZE_2, f_2)) >
1) {
        RETURN_CODE_2[r_2-1] = '\0';
}
pclose(f_2);
but I am worried that this is not thread safe and this is for a multi-
threaded application on AIX 5.3 - compiling with XLC.
I figured i could put a mutex around it, but having not done that
before wasnt 100% sure if i have this correct. Here is what another
friend and I came up with:
if(pthread_mutex_init(&mutex, NULL)){
        printf("Unable to init a mutex\n");
        return -1;
   }
 pthread_mutex_lock(&mutex);
  snprintf(VARIABLES_2, sizeof VARIABLES_2, "/usr/bin/cksum
'%s'",ARGV1);
  FILE * f_2 = popen(VARIABLES_2, "r");
  size_t r_2;
  while((r_2 = fread(RETURN_CODE_2, sizeof(char), BUFSIZE_2, f_2)) >
1) {
        RETURN_CODE_2[r_2-1] = '\0';
}
pclose(f_2);
 pthread_mutex_unlock(&mutex);

I would appreciate any comments on this or suggestions for making that
code better... or work =-)
Thank you in advance!

Do not worry about the performance hits. You are going to make
a popen anyway what means a call to the OS, then doing an fread
several times... Those calls are so expensive ANYWAY that the small
amount of nanoseconds of the mutex do not make any difference.

But (more important) what is "VARIABLES_2"???

If they are globals I would surely INCLUDE them within the
mutex protected part!- Hide quoted text -

- Show quoted text -


Thanks for the advice. Though.. this particular section of code is a
plugin for a multi-threaded application. I am worried that popen is
not thread safe. I've read various things that it depends on the
platform, version of popen etc etc. So basically , i wanted to protect
this section of the code somehow using a thread. I can't seem to find
any good tutorials on how to implement mutexes or threads, if anybody
has a link to a good resource, that would be great. Thanks again!
 
K

Keith Thompson

tntelle said:
Thanks for the advice. Though.. this particular section of code is a
plugin for a multi-threaded application. I am worried that popen is
not thread safe. I've read various things that it depends on the
platform, version of popen etc etc. So basically , i wanted to protect
this section of the code somehow using a thread. I can't seem to find
any good tutorials on how to implement mutexes or threads, if anybody
has a link to a good resource, that would be great. Thanks again!

Try asking in comp.programming.threads.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top