Re: PThreads and Sockets

1999-11-27 Thread FengYue


On Fri, 26 Nov 1999, Dan Moschuk wrote:

 
 | int sd2;
 | if((sd2=accept(sd, (struct sockaddr*)cad, alen))  0) {
 | pthread_create(thread1, pthread_attr_default,
 |serverstart, sd2);
 | }
 | 
 | Then the serverstart function:
 | 
 | void *serverstart(void *ptr)
 | {
 | int *sd2;
 | sd2 = (int*)ptr;
 | 
 | dowhatever(sd2);
 | }
 |
 | Any ideas as to what I'm doing wrong? Also, thanks for your help.
 | 
 | Rob
 
 Try this.
 
 void *serverstart(void *ptr)
 {
   int sd2;
 
   sd2 = *((int *) ptr);
   ...
 }

There is a race condition.  You're passing sd2's address to serverstart()
and inside serverstart() you def' the pointer.  What if
"sd2=accept(sd, (struct sockaddr*)cad, alen)" gets
executed before your previous serverstart() finishs "sd2 = *((int*)ptr)"?

btw, IMHO, creating threads per connection is a very bad design.



To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message



Re: PThreads and Sockets

1999-11-27 Thread Rob King

 There is a race condition.  You're passing sd2's address to serverstart()
 and inside serverstart() you def' the pointer.  What if
 "sd2=accept(sd, (struct sockaddr*)cad, alen)" gets
 executed before your previous serverstart() finishs "sd2 = *((int*)ptr)"?
 
 btw, IMHO, creating threads per connection is a very bad design.
 
 
 
 To Unsubscribe: send mail to [EMAIL PROTECTED]
 with "unsubscribe freebsd-hackers" in the body of the message
 

How would you recommend I do it? Please remember, I have no experience
with pthreads, and any advice you give would be greatly appreciated.

I tried doing a pool of threads created at startup, and I think that may
be a better approach...That would allow tighter control of resource limits
- do something like Apache, have a "maximum number" of processes running.

Anyway, thanks for the help.

Rob

-- 

Rob King
Network Administrator - PERnet Communications, Inc.
[EMAIL PROTECTED]  - http://www.pernet.net/



To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message



Re: PThreads and Sockets

1999-11-27 Thread Dan Moschuk


|  Try this.
|  
|  void *serverstart(void *ptr)
|  {
|  int sd2;
|  
|  sd2 = *((int *) ptr);
|  ...
|  }
| 
| There is a race condition.  You're passing sd2's address to serverstart()
| and inside serverstart() you def' the pointer.  What if
| "sd2=accept(sd, (struct sockaddr*)cad, alen)" gets
| executed before your previous serverstart() finishs "sd2 = *((int*)ptr)"?

Since accept isn't atomic, it would be best to enclose the whole sha-bang in
a mutex up until the sd2 = *((int *) ptr) call finishes.
--
Dan Moschuk ([EMAIL PROTECTED])
"Try not.  Do, or do not.  There is no try."
-- Yoda


To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message



Re: PThreads and Sockets

1999-11-27 Thread FengYue


On Sat, 27 Nov 1999, Rob King wrote:

 
 How would you recommend I do it? Please remember, I have no experience
 with pthreads, and any advice you give would be greatly appreciated.

There are couple of ways you could do this:

1) pass sd2's value instead of its memory address to serverstart()
pthread_create(thread1, pthread_attr_default,serverstart, sd2);
   then inside serverstart:
void serverstart (void *p)
{
int sockfd = (int) p;
blah...blah
}

if you worry about void * being not as the same size as int on
some platforms, then u should use 2)

2) Use a mutex lock:
pthread_mutex_lock (mp_lock);
_do_the_accept_
_create_thread_
inside serverstart(), right after u def' the pointer, do this
pthread_mutex_unlock (mp_lock);

 
 I tried doing a pool of threads created at startup, and I think that may
 be a better approach...That would allow tighter control of resource limits
 - do something like Apache, have a "maximum number" of processes running.

Yes, that's a better approach in many cases.  Creating threads itself
is a quiet expensive operation.



To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message



PThreads and Sockets

1999-11-26 Thread Rob King


Hi all,
  I'm writing a server that multiplexes a MySQL connection to several
clients. Since the connection has to be shared, I can't fork(), I have to
thread. This isn't a problem, except that when I accept() and assign a
socket descriptor, the first thread runs fine, but the next thread simply
grabs the descriptor (since it's shared) from the previous thread and
starts doing all its writing to that socket. Any idea how to get around
this? If there's any good example code, please let me know.

Thanks,
Rob

-- 
[EMAIL PROTECTED]
http://ota.pernet.net/




To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message



Re: PThreads and Sockets

1999-11-26 Thread Dan Moschuk


| int sd2;
| if((sd2=accept(sd, (struct sockaddr*)cad, alen))  0) {
|   pthread_create(thread1, pthread_attr_default,
|  serverstart, sd2);
| }
| 
| Then the serverstart function:
| 
| void *serverstart(void *ptr)
| {
|   int *sd2;
|   sd2 = (int*)ptr;
| 
| dowhatever(sd2);
| }
|
| Any ideas as to what I'm doing wrong? Also, thanks for your help.
| 
| Rob

Try this.

void *serverstart(void *ptr)
{
int sd2;

sd2 = *((int *) ptr);
...
}

-- 
Dan Moschuk ([EMAIL PROTECTED])
"Cure for global warming: One giant heatsink and dual fans!"


To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message