![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| 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 |
|
|
LinkBack | Thread Tools | 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.... |
| Forum Sponsor | ||
|
|
|
|||
|
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)) { ... }
}
}
|
|||
| Google UNIX.COM |