The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How Can I use pthread_create ? shvalb High Level Programming 7 03-16-2009 05:00 AM
Pthread_create issue Hellboy High Level Programming 1 05-19-2008 07:58 AM
undefined reference to `pthread_create' netman High Level Programming 2 01-28-2008 12:31 PM
unresolve pthread_create etc zhshqzyc High Level Programming 3 02-13-2006 06:38 PM
pthread_create problem _rocky High Level Programming 5 03-11-2005 06:38 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-17-2006
jenmead jenmead is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 3
pthread_create and scope usage

I have a problem with a C multi-threaded program I am writing. I cannot figure out how to keep the unique key value at the thread level. I wrote a program in C that forked a bunch of processes and then decided to convert it to threads and I can't keep the key unique to each thread. In a nutshell it reads a file with a valid pathname on every line (approx 200) and starts a thread to watch over a unique directory (simple polling program). Error is when I create a new thread, then every pre-exisiting thread is looking at the last pathname read in. So I end up with 200+ threads watching the same directory.

Following is what I think is a big enough snippet from main and the pthread_create sub to give something for someone to point out what I am doing wrong. Thanks in advance, Jennifer


> MAIN
> --------
> pthread_t main_thr;
> pthread_t thr;
> pthread_attr_t attr;
>
> main_thr = pthread_self();
> p=malloc(2);
> p = buffer;void *watch_dir(void *arg)
>
> pthread_key_t key;
>
> pthread_key_create(&key, NULL);
> thread_setspecific(key, arg);
> arg=pthread_getspecific(key);
> pthread_attr_init(&attr);
> pthread_attr_setdetachstate(&attr, 1);
>
> if (pthread_create(&thr, &attr, watch_dir, p))
> {
> fprintf(fplog, "Can't create thr\n"), exit(1);
> }
>
> WATCH_DIR
> ------------------
> void *watch_dir(void *arg)
> {
>
> FILE *fplog;
> time_t curtime;
> struct tm *loctime;
> DIR *dir_p;
> struct dirent *dir_entry_p;
> pthread_key_t key;
> pthread_key_create(&key, NULL);
> pthread_setspecific(key, arg);
> arg=pthread_getspecific(key);
>
> for (;
> {
> fplog=fopen(FTP_LOG, "a");
>
> curtime = time (NULL); /* get current time */
> loctime = localtime (&curtime);
> fputs (asctime (loctime), fplog);
> sleep(3);
>
>
> dir_p = opendir(arg);
> while( NULL != (dir_entry_p = readdir(dir_p)))
> {
> if (!((strcmp(dir_entry_p->d_name, ".") == 0) ||
> (strcmp(dir_entry_p->d_name, "..") == 0)))
> {
> fprintf(fplog," %s \n", dir_entry_p->d_name);
> /*
> holder for FTP Control PERL script
> send: arg & dir_entry_p->d_name
> directory & filename
> */
> }
> /*
> printf("p is %s\n",p);
>
  #2 (permalink)  
Old 09-19-2006
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,965
my eyes, they burn. code tags please. Like {code} int main(); {/code} except with [ ]. This will use a monospace font, preserve indenting, and prevent syntax being converted to smilies.

What is buffer? What is the malloc(2) for, especially since the allocated memory immediately gets lost when you point it to buffer instead? I suspect you're using pointers to a common buffer, which gets overwritten every time a new string is read.
  #3 (permalink)  
Old 09-20-2006
jenmead jenmead is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 3
Got it fixed. It was more related to converting from Solarix to AIX than anything else. Not sure what is meant by putting in tags so the smiles don't creep in though ..... --Jennifer
  #4 (permalink)  
Old 09-20-2006
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,965
Without tags:

> for (;
> {
> something;
> }

With tags:

Code:
for(;;)
{
  something;
}

[ code ] for(; { ... } [ / code ]

Remove the spaces between the [ ] 's to make the tags work like the code block above it.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:47 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0