Solved

Unix threads and Sleep as a block process

Posted on 2003-11-18
11
821 Views
Last Modified: 2010-04-21
Hi everyone ... thanks for taking your time to look at this ...

I am trying to create a process that creates a needed number of threads per single proces. Also, within this main process I have a 2 global vars that are accessed within threads as well as main process. Those 2 vars I am controling using the POSIX semaphores. I am shure that those semaphores are working and positioned right.

My problem is that I have to use the sleep within my main process to let those threads kick in to read / change the global vars. Since sleep blocks the entire process my program kind of run VERY slow.

My main loop looks like

while(time)
{
   sem_wait(&currentime)
   currenttime++
   sem_post(&currentime)
   while(true)
  {
     create my thread etc etc
     sleep(1)
  }

}

I was wondering if anyone is out there that is aware of any system call that would not block the entire process that I could use instead of sleep ....

Again ... thanks a loot for looking at this.
L.
0
Comment
Question by:ledeni
  • 3
  • 3
  • 2
  • +2
11 Comments
 
LVL 38

Expert Comment

by:yuzh
ID: 9776486
I think you can create a milisecond timer to WAST miliseconds, and let the threads to kick off.

have a look at the following page, to see if it make sense:
http://oldlook.experts-exchange.com/Programming/Programming_Platforms/Linux_Programming/Q_20577083.html
0
 
LVL 38

Expert Comment

by:yuzh
ID: 9776531
Hi ledeni,

    I think if you put (move/link) this question to UNIX programming, you might get better
response.
    http://oldlook.experts-exchange.com/Programming/Programming_Platforms/Unix_Programming/

    You can ask the CS (post a 0 point question in CS TA), and the Mods will do it for you:
    http://oldlook.experts-exchange.com/Community_Support/
   
0
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 100 total points
ID: 9776879
try pthread_yield()

man pthread_yield

I assumed that you are using the pthreads library
0
 
LVL 1

Accepted Solution

by:
ChiefEngineer earned 100 total points
ID: 9877883
I guess you need the threads to initialize in a certain order to set the global variables correctly and that a random order initialization is what you want to avoid.

If that's the case I suggest you to use another semaphore or mutex to synchronize the threads creation within the main loop (because in fact you have a kind of resource access coordination issue here) so you block your main loop until the new thread releas the lock and then you have no timer waste.

Just modify your code so it looks like this:


my thread ( )
{
   // Initialization part of the thread
    ...
    release mutex for initializing thread

    // Rest of the thread code
    ...
}

...

while(time)
{
   sem_wait(&currentime)
   currenttime++
   sem_post(&currentime)
 
  while(true)
  {
     lock mutex for initializing thread
     create my thread etc etc
     // I suggest to add a timer for this operation so you don't hang in case of problems
     // with thread's initialization
     wait mutex for initializing thread
  }
}

Hope it helps.
0
 

Author Comment

by:ledeni
ID: 9878203
Sorry I should have posted earlier ... but I got that solved with thr_yield() ... but certanly your effort will be awarded !!

Thanks for looking at this anyhow
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 45

Expert Comment

by:sunnycoder
ID: 9881259
ledeni,

I had suggested using pthread_yield more than 2 weeks ago !!! I assumed that you were using pthreads which was wrong ... so you had to use thr_yield in place of pthread_yield ... I feel that you awarded the question wrongly
0
 

Author Comment

by:ledeni
ID: 9898775
Hey Pasha

fare is fare ... you can move or split those points between the both guys .. I appreciate the sunny's response though that it was not quite the right reposnse .. since I used the thr_yield() and I also appreciate the other guy response for giving in his time .. .I will leave it to you to distribute those points ...

thx
L .
0
 

Author Comment

by:ledeni
ID: 9898785
for a future reference ,let me know if there is anyhow possible for me to reasign points or split them my self bw different people .
thanks
L.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9902983
>I appreciate the sunny's response though that it was not quite the right reposnse
My idea was correct .... making the threads yield
I had clearly mentioned in my post

>>try pthread_yield()

>>man pthread_yield

>>I assumed that you are using the pthreads library

you never returned clarifying that you were not using pthreads nor was any such thing specified in your original question !! All that was needed was using the corresponding thread yielding function of the library/pltform that you  were using.

>for a future reference ,let me know if there is anyhow possible for me to reasign points or split them my self bw different
>people .
you cannot reassign points ... for help with splitting,
http://www.experts-exchange.com/help/closing.jsp
0
 

Expert Comment

by:YensidMod
ID: 9923744
I split the points per asker request.

Yensidmod
EE Moderator
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now