LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-19-2006, 08:45 AM   #1
nathan2225
LQ Newbie
 
Registered: Jun 2006
Posts: 27

Rep: Reputation: 0
Question avoid hipri thread waiting on lowpri


Hi folks !

I'm using pthreads for streaming audio, where i need to send a packet each 9.xxx ms.

I'm using 2 threads, one for command handling, and one 4 streaming.
This threads share a data structure which i locked against concurrency with a pthread mutex.

One thread is running with SCHED_FIFO which means it is uninterruptable by the scheduler, the other uses normal SCHED_OTHER.

So i think there is one problem:

If the lopri thread just gets scheduled out while within the critical section, it can block the hipri thread for quite some time (there are running quite a lot of other threads) ....

How can i avoid this behaviour ?

The only options i can imagine is using a kernel-level driver or just switching to SCHED_FIFO in the lopri thread b4 entering the crit. section, but both methods sounds rather harsh to me.

Does someone know betters solutions ?


Btw another question: If i use a malloc and afterwards do a fork and free the pointer in the child process, do i need to free it again in the parent ?


MfG
Robert
R&D AV Digital
 
Old 06-19-2006, 10:32 AM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
The problem is basically unsolvable if you want to satisfy these two conditions:

(1) the low-prio thread must finish writing into the data structure so the data is always valid when read by the hi-prio thread.
(2) the hi-prio thread may not be blocked by the the lo-prio thread

In other words, if you demand that the data structure is always fully valid, you MUST allow the low-prio thread to finish the write operation.

Therefore, you must give up one of the two. Since high priority is high priority, I assume you can accept that the data sttructure is not always up-to-date when you use it in te hi-prio thread.

You could therefor do this:

in the lo-prio thread:
Code:
set semaphore
write the data structure
unset semaphore
in the hi-prio thread
Code:
if semaphore is set
{
  do not use the data structure
  or use the old data
}
else
{  
  use the data from the data structure
}
It can be benefical if you make a copy of the data structure in your hi-prio thread so you have the old data when you cannot access it.

The important point here is that semaphore operations are atomic, and write operations can not be interrupted. It is never invalid, and you don't have to mutex the access to a semaphore.

jlinkels
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Hard Drive Problems: timeout waiting for DMA; error waiting for DMA mintee Linux - Hardware 10 09-21-2007 05:06 AM
avoid hipri thread waiting on lopri nathan2225 Linux - General 0 06-19-2006 08:37 AM
Main thread sending notification to child thread rajesh_b Programming 1 09-22-2004 09:15 AM
configure qt thread issue (just compiled qt w/ -thread option) cleff Linux - Software 8 05-07-2004 11:11 PM
sending signal to thread waiting on pselect exedx Programming 4 03-21-2004 11:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration