pthread_create and scope usage


 
Thread Tools Search this Thread
Top Forums Programming pthread_create and scope usage
# 1  
Old 09-17-2006
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 (;Smilie
> {
> 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  
Old 09-19-2006
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  
Old 09-20-2006
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  
Old 09-20-2006
Without tags:

> for (;Smilie
> {
> something;
> }

With tags:
Code:
for(;;)
{
  something;
}

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

Remove the spaces between the [ ] 's to make the tags work like the code block above it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

fork vs pthread_create

Suppose I have a simple program main() with a global varibale int x=0. int x = 0; main() { print("%d\n",x); } I want to create two threads/process which must access this variable x in sync. Which one will be better threads( pthread_create ) or process( fork )? If I go with fork() then... (1 Reply)
Discussion started by: rupeshkp728
1 Replies

2. UNIX for Dummies Questions & Answers

Pthread_create problem

Hi, I'm trying to do my homework assignment but I am having trouble using the pthread_create fucntion. Here is my code________________ //Alicia Johnson //sum_pid program //creates n number of threads. These threads create a random number //then adds the number to a global array. Then... (1 Reply)
Discussion started by: ajohns38
1 Replies

3. Programming

pthread_create

The prototype for pthread_create function is like this:- int pthread_create(pthread_t *thread,pthread_attr_t *attr,void *(*start routine),void *arg); Q.1 .Why the return type of the start_routine must be void*?? Q.2. Why should we pass arg by converting into void * only ?? Thank You (3 Replies)
Discussion started by: sunil_abhay
3 Replies

4. Programming

undefined reference to `pthread_create'

Hi guys. H was learning posix threads in C with anjuta IDE. it gives me undefined reference to `pthread_create' I know i should compile it like: gcc -lpthread main.c how should i import this configuration in anjuta so i can compile inside it? (2 Replies)
Discussion started by: majid.merkava
2 Replies

5. AIX

How to monitor the IBM AIX server for I/O usage,memory usage,CPU usage,network..?

How to monitor the IBM AIX server for I/O usage, memory usage, CPU usage, network usage, storage usage? (3 Replies)
Discussion started by: laknar
3 Replies

6. Programming

How Can I use pthread_create ?

Hi. I use C++ and I wishes to create a thread with the pthread_create function, my question is, how can I do this if I wish that the function will be a member of the class ?? I know from windows programming that I can declare a static function like this static unsigned int __stdcall... (7 Replies)
Discussion started by: shvalb
7 Replies

7. HP-UX

how can I find cpu usage memory usage swap usage and logical volume usage

how can I find cpu usage memory usage swap usage and I want to know CPU usage above X% and contiue Y times and memory usage above X % and contiue Y times my final destination is monitor process logical volume usage above X % and number of Logical voluage above can I not to... (3 Replies)
Discussion started by: alert0919
3 Replies

8. Programming

Pthread_create issue

Hello My problem goes like this: I have used Pthread_create, and I have tryed to create 2 proccess but nothing happens! It does not even matter what the function im trying to create do. It is if im trying to activate an empty function. This is my code. Any help will be highly appreciated.... (1 Reply)
Discussion started by: Hellboy
1 Replies

9. Programming

unresolve pthread_create etc

how to do with that? after cc -o xxxx xxxx.c ld: Unresolved: _pthread_create _pthread_deteach _pthread_exit Thanks (3 Replies)
Discussion started by: zhshqzyc
3 Replies

10. Programming

pthread_create problem

Here is simple code for multithreading in POSIX: void* simplethread(void* arg) { printf("Hello World\n"); } int main(void) { pthread_t id; pthread_create(&id, NULL, simplethread, NULL); return 0; } Whether the new thread will run or not depends on the OS. Tricky ... (5 Replies)
Discussion started by: _rocky
5 Replies
Login or Register to Ask a Question