![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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.... |
|
||||
|
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. |
|
||||
|
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... |
|
||||
|
Quote:
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, |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|