• C

program using threads in C in linux environment.

Hi All,

  I am writing a program using threads in C in linux environment.

The main program creates a thread  "mythread"  and calls it.  .  the mythread should wait for event to occur and sleep till then,  giving the control back to the main. and when the even occurs mythread should wake and do some work and again sleep ,give the control back to main .

 

please provide with code.

thanks in advance.
mmadhusoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
See http://www.llnl.gov/computing/tutorials/pthreads/ ("POSIX Threads Programming"). You'll find all the code and samples that you need there (http://www.llnl.gov/computing/tutorials/pthreads/#Joining in particular)
Kent OlsenDBACommented:

Hi mmadhuso,

You description seem to be confusing threads and tasks.  Threads are designed for multiple simultaneous execution logic.  A great example is the brower you're using to read this which has one thread dedicated to monitoring human intervention (keypresses, mouse movement, etc) and another thread dedicated to rendering the page that you're reading.  (There may be many other threads, too.)  If one of the threads fails (aborts) the entire application fails.

Tasks are different.  They are completely separate work spaces that typically communicate with signals, wait for each other, etc.  Just as you described.

And this does sound very much like a homework assignment.  Therefore, you need to provide EE with the code, we'll help you to get it right.


Good Luck,
Kent
mmadhusoAuthor Commented:
hi Kent

Its not my Home work by the way   ,  the code  is
////////////////////////////////////////////////////////////////////////////////////////

void  *newwrites(void *mess)
      {
             char *pch;int avail;
             char str[32]=" ";
             int inhandle ;
             printf("In newwrite ");
           do
             {
             inhandle=open("/proc/meminfo",O_RDONLY);
             read(inhandle,str,26);
             read(inhandle,str,26);
             pch = strtok (str," "); pch = strtok (NULL," ");
             avail=atoi(pch);
             printf("%d \n",avail);
             close(inhandle);
             }
            while(avail>400000);

     }
int  main()
{
       char *message1 = "Thread 1";
        pid_t child_pID;
        int status,rc;
       pthread_t thread1 ;int s;
          rc =    pthread_create(&thread1, NULL, newwrites, (void *)message1);
         if (rc){
         printf("ERROR; return code from pthread_create() is %d\n", rc);
         exit(-1);
         printf(" created thread \n ");
    /*
   do some operation while the thread is running
  */  
      pthread_exit(NULL);

 return 0;
}

hope this helps
jkrCommented:
Any chance getting you interested in checking out the above tutorial?
Kent OlsenDBACommented:

There are a couple of ways to go about what you've asked.   :~}

Based on your code, you probably want to put a pthread_join() function call in place of the "do some operation while the thread is running".  This will cause the main thread to block until newwrites() completes and then end the newwrites() thread.


Kent
mmadhusoAuthor Commented:
hi JKR
i have gone thru the tutorials but without any success
mmadhusoAuthor Commented:
Hi Kent

But i dont want the main to be blocked till the newwrites is completed

i mean both should run in parallel ,

main should perform the work along with newwrites()  this is where the problem lies

thanks a lot for the discussion  
Kent OlsenDBACommented:

You can certainly do work before the call to pthread_join().  It just synchronizes the collection of the threads back into one execution point.

If you want the two threads to handshake as to status, you'll probably want to use a mutex.  Are you familiar with them?

Kent
mmadhusoAuthor Commented:
Hi Kent

So by using  pthread_join() will the controll be given back to the main ? thats a imp part here .

can u explain a bit more on usage of mutex   in my code


manish
jkrCommented:
>>i have gone thru the tutorials but without any success

In such a short time? Now, that's amazing. BTW, in my first comment, I added http://www.llnl.gov/computing/tutorials/pthreads/#Joining so that this is easier to find.
Kent OlsenDBACommented:

A mutex is a lot to explain in a few sentences.  Here's pretty good link to a C tutorial that covers threads and mutex topics, along with examples.

  http://riki-lb1.vet.ohio-state.edu/mqlin/computec/tutorials/C_Tutorial/

Kent
mmadhusoAuthor Commented:
hi jkr

I had read these doc before i i mailed to EE

can u help me acheiving my task
cupCommented:
Have you compiled the code as multi-threaded?  By default, the system will build it as single threaded even though you added the pthread library.
Infinity08Commented:
I could be misunderstanding something, but from your requirements :

>> The main program creates a thread  "mythread"  and calls it.  .  the mythread should wait for event to occur and sleep till then,  giving the control back to the main. and when the even occurs mythread should wake and do some work and again sleep ,give the control back to main .

something like this looks good (pseudo code) :

main :
   start thread
   do all other things main should do
   end thread
   return from main

thread :
   infinite loop
     sleep untill event
     do operations for event
   end loop

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.