The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
problem while having a communication with serial port????? arunchaudhary19 High Level Programming 9 04-17-2008 01:52 PM
urgent......Serial port arunchaudhary19 Linux 16 11-02-2007 02:42 AM
serial port signal ppass SUN Solaris 0 02-04-2005 07:11 AM
Telling apart serial from // port starless High Level Programming 2 09-29-2003 11:01 AM
serial port configuration Henrik UNIX for Dummies Questions & Answers 1 03-10-2003 08:42 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-11-2007
Registered User
 

Join Date: Sep 2007
Posts: 62
Problem regarding serial port and multithreading

hello,

I am creating a application in which I first open the serial port and then create the thread..for reading the data comming through that serial port. from that same thread I create another thread that is write thread means for writing on the serial port...
I am continously polling again and again to check the data comming through that thread
the read thread is working correctly but due to write thread I am getting some different bahavior of the code...sometime segmentation fault...sometimes unwanted result etc....

can any body guide me where I am wrong...why unwanted bahavior of the code....
can Anybody guide me how many threads I can create in a program and where to create Should I use any precuations while writing multithreaded application....
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 11-12-2007
Registered User
 

Join Date: Jan 2007
Posts: 2,965
The most reliable way to used a file descriptor is not to have some functions handled in different threads, but use the same thread to do all IO for a file descriptor, use select or poll and non-blocking IO just like in a single threaded application.

If you take the multithreaded approach having one thread to read, and one to write, how do you stop the reading, how do you close the file descriptor?

And yes, if writing multithreaded code you must take precautions, eg mutexs, condition variables as appropriate.
Reply With Quote
  #3 (permalink)  
Old 11-12-2007
Registered User
 

Join Date: Sep 2007
Posts: 62
As you suggested me to use select or poll ....
Here in my code I am using select to select the file descriptor when there is data to read....
and using usleep to free the cpu and da poll after some time..

moreover I am taking care of locking and unlocking mutexes...
Reply With Quote
  #4 (permalink)  
Old 11-13-2007
Registered User
 

Join Date: Jan 2007
Posts: 2,965
Quote:
Originally Posted by arunchaudhary19 View Post
and using usleep to free the cpu and da poll after some time..
Not sure what you mean by usleep. If you are using select or poll the thread will block until something is ready, (use a timeout pointer NULL).

I basically use a loop with the file-descriptor to read and write from, and another pipe() created pair.

Then the read set is the fd and the read end of the pipe.
THe write set is either emtpy, or if have data to send, put in the fd.

Code:
while (1)
{
    fd_set fdr,fdw;
    FD_ZERO(&fdr);
    FD_ZERO(&fdw);
    FD_SET(fd,&fdr);
    FD_SET(pipe_read,&fdr);
    if (dataToWrite) FD_SET(fd,&fdw);

    if (select(1+max(fd,pipe_read),&fdr,&fdw,NULL,NULL)>0)
    {
         if (FD_ISSET(pipe_read,&fdr)) read(pipe_read,buf,1);
         if (FD_ISSET(fd,&fdr)) { ... }
         if (FD_ISSET(fd,&fdw)) { ... }
    }
}
Then if need to write to serial port set the dataToWrite flag, write a byte to the pipe write end, that will unblock the select, it will loop round and reselect the fd for writing,
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:11 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0