2009-06-29 01:41:58     Operate device or socket fd in two thread.

Document created by Aaronwu Employee on Aug 15, 2013
Version 1Show Document
  • View in full screen mode

2009-06-29 01:41:58     Operate device or socket fd in two thread.

DAVID ZHOU (CHINA)

Message: 76440   

 

Hi,

 

In my sample code, I want to operate /dev/sport0 in the followed mode:

 

void * thread1(void * arg)

 

{

 

     int  fd = *(arg);

 

     unsigned char buf[100];

 

     int size = 100;

 

     while(1)

 

     {

 

          write(fd,buf,size);

 

          sleep(1);

 

     }

 

}

 

void * thread2(void * arg)

 

{

 

     int  fd = *(arg);

 

     unsigned char buf[100];

 

     int size = 100;

 

     while(1)

 

     {

 

          read(fd,buf,size);

 

          sleep(1);

 

     }

 

}

 

void main()

 

{

 

     int fd;

 

     pthread_t  thread1,thread2;

 

     fd  = open("/dev/sport0",O_RDWR);

 

     pthread_create(&thread1_id,NULL,thread1,(void *)&fd);

 

     pthread_create(&thread2_id,NULL,thread1,(void *)&fd);

 

     pthread_join(thread1,null);

 

     pthread_join(thread2,null);

 

     return;

 

}

 

My question is that:

 

1> Can I use the fd which discribed in my code? one thread read and the other thread write?  Do I have to use pthead_mutex to synchronize the two threads' access to the fd?

 

2>If this is not a good manner, could I open /dev/sport0 in these two thread seperately and operate the /dev/sport0?

 

3>Can I use socket fd like what discribed in my code? one thread read and the other one write? If not, why and how should I do?

 

Thanks very much in advance!

QuoteReplyEditDelete

 

 

2009-06-29 01:52:21     Re: Operate device or socket fd in two thread.

Mike Frysinger (UNITED STATES)

Message: 76441   

 

the Blackfin SPORT driver operates in half-duplex mode, so while your code wont cause any crashes and will safely run, it may not do what you expect.  you really havent described what you're trying to do though.

 

sockets work just fine reading and writing simultaneously.

QuoteReplyEditDelete

 

 

2009-06-29 02:25:18     Re: Operate device or socket fd in two thread.

DAVID ZHOU (CHINA)

Message: 76446   

 

Thanks Mike,

 

I just want to run my program multithread for operating sport0 and one thread responsible for reading, the other thread just for writing.  But I just want the two threads using a same fd.   Also I want to make sure the driver won't make reading affected the writing and vice versa.

Attachments

    Outcomes