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 05:52 PM
urgent......Serial port arunchaudhary19 Linux 16 11-02-2007 06:42 AM
serial port signal ppass SUN Solaris 0 02-04-2005 11:11 AM
Telling apart serial from // port starless High Level Programming 2 09-29-2003 03:01 PM
serial port configuration Henrik UNIX for Dummies Questions & Answers 1 03-10-2003 12:42 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-12-2007
arunchaudhary19 arunchaudhary19 is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 72
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....
  #2 (permalink)  
Old 11-12-2007
porter porter is offline Forum Advisor  
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.
  #3 (permalink)  
Old 11-12-2007
arunchaudhary19 arunchaudhary19 is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 72
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...
  #4 (permalink)  
Old 11-13-2007
porter porter is offline Forum Advisor  
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,
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:42 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0