Link to home
Start Free TrialLog in
Avatar of mmadhuso
mmadhuso

asked on

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.
Avatar of jkr
jkr
Flag of Germany image

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)

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
Avatar of mmadhuso
mmadhuso

ASKER

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
Any chance getting you interested in checking out the above tutorial?

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
hi JKR
i have gone thru the tutorials but without any success
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  

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
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
>>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.

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
hi jkr

I had read these doc before i i mailed to EE

can u help me acheiving my task
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.
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial