linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Linux Kernel <linuxkrnl@yahoo.com>
To: Ron Michael Khu <ronkhu@ntsp.nec.co.jp>,
	linux prg <linux-c-programming@vger.kernel.org>
Subject: Re: Problem with pthreads on socket
Date: Sun, 7 Nov 2004 09:47:34 -0800 (PST)	[thread overview]
Message-ID: <20041107174734.88175.qmail@web90003.mail.scd.yahoo.com> (raw)
In-Reply-To: <418E1E83.4000905@hq.ntsp.nec.co.jp>

Hi,
Thks for you answer. :)
For sure you are right.The problem is in my mind.
I thinking like if i using fork().
The mutex variable is initialized with
PTHREAD_MUTEX_INITIALIZER.But is a global one and for
this reason is not showed in the code.
I will not use pthread_join() and i will try to
intialize a memory to be shared between threads.
Thks 
 :)
Zazoo

--- Ron Michael Khu <ronkhu@ntsp.nec.co.jp> wrote:

> perhaps  this is  a typical deadlock problem....
> where is the mutex variable defined?? r u using or
> accessing shared 
> resources??
> 
> Im confused here:
>
------------------------------------------------------------------
>               
> 
> if(th_res==0)
>         {
>         tCount++;
>         printf("TH COUNT : %d\n",tCount);
>         mThread[tCount-1].tIDX=tCount;
>         mThread[tCount-1].tID=thread;      
>         pthread_join(thread,&mRetTh);
>         //close(new_fd); if i close the socket ,
> accept will
> create the same hnd_new_sock like previous...bad
> ideea.
>           
>         }
>         else
>         {  
>             perror("CREATE_THREAD");  
>         }
>
--------------------------------------------------------------------------
> 
> why does the main thread (the one holding the server
> socket)
> perform a pthread_join() operation whenever it
> creates a child thread?
> 
> I thought ur goal was to create a multi-tasking
> server... with a server
> thread constantly waiting and accepting client
> connections while leaving 
> it to
> the client threads to handle the actual client
> communication?
> 
> U see, with ur code, the main/server is actually
> only capable of 
> handling one client at a time...
> pthread_join() causes the calling thread(in ur case,
> the main thread) to 
> wait or block until
> the child thread terminates(or is cancelled)...
> 
> And in case my assumption is wrong(that u want a
> multitasking server 
> app) and if that the
> desired behavior is to serve only one client at a
> time, then what's the 
> purpose of spawning
> threads? =(
> 
> hmmm... i think ur still designing/coding in terms
> of fork(), where u 
> are spawning whole
> processes instead of lightweight processes(or
> threads)
> 
> 
> 
> Linux Kernel wrote:
> 
>  > Hi to everybody,
>  > I'm not have too much exoerience eith pthread but
> i
>  > try to use this insted of fork() to have
> independent
>  > thread processes.
>  > I write a server/client application using socket
> and
>  > the main problem is that affter first client is
>  > accepted by accept(), the second accepted client
> broke
>  > the 1-st created thread an the socket remain
> blocked.
>  > I'm stuck and i don't understand what happening.
>  > A *slice* of my code :
>  > int main()  
>  >     struct sockaddr_in my_addr;    /* my address
>  > information */
>  >        struct sockaddr_in their_addr; /*
> connector's
>  > address information */
>  >          
>  >     static int sockfd;
>  >     int th_res=0;
>  >     pthread_t thread;
>  >     pthread_attr_t attr;
>  >     int sin_size,yes=1,new_fd=0;
>  >     char* mPath=NULL;
>  >     MyARGV* marg;
>  >     fd_set fd_read;
>  >     void* mRetTh=NULL;
>  >     int tCount=0,res_lock;
>  >     T_THREAD mThread[10000];
>  >    
>  >       
>  >     mPath=(char*)malloc(256);
>  >     marg=(MyARGV*)malloc(sizeof(MyARGV)+256);    
>        
> mPath=getenv((const char*)"MY_PATH");  
>  >    
>  >    
>  >     if ((sockfd = socket(AF_INET, SOCK_STREAM,
> 0)) == -1)
>  > {
>  >            perror("socket");
>  >            exit(1);
>  >        }
>  >    
>  >     if
>  >
>
(setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int))==-1)
>  >     {
>  >         perror("SET_SOCK_OPT : \n");
>  >     }
>  >    
>  >        my_addr.sin_family = AF_INET;         /*
> host
>  > byte order */
>  >        my_addr.sin_port = htons(MYPORT);     /*
>  > short, network byte order */
>  >        my_addr.sin_addr.s_addr = INADDR_ANY; /*
>  > auto-fill with my IP */
>  >        bzero(&(my_addr.sin_zero), 8);        /*
> zero
>  > the rest of the struct */
>  >
>  >        if (bind(sockfd, (struct sockaddr
> *)&my_addr,
>  > sizeof(struct sockaddr)) \
>  >                                                  
>                  == 
> -1) {
>  >            perror("bind");
>  >            exit(1);
>  >        }
>  >
>  >        if (listen(sockfd, 1) == -1) {
>  >            perror("listen");
>  >            exit(1);
>  >        }
>  >    
>  >       
>  >     FD_ZERO(&fd_read);
>  >     FD_CLR(sockfd,&fd_read);
>  >     FD_SET(sockfd,&fd_read);
>  >    
>  >        while(1) {  /* main accept() loop */
>  >            sin_size = sizeof(struct sockaddr_in);
>  >          
> res_lock=select(sockfd+1,&fd_read,NULL,NULL,NULL);
>  >                 if ((new_fd = accept(sockfd,
> (struct sockaddr
>  > *)&their_addr,&sin_size)) == -1) {
>  >                perror("accept");
>  >                continue;
>  >            }
>  >                      
> marg->s=fcntl(new_fd,F_DUPFD,0);
>  >         marg->mPath=mPath;
>  >         marg->their_addr=their_addr;
>  >
>  >                 pthread_attr_init(&attr);
>  >       
>
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
>  >       
>
th_res=pthread_create(&thread,&attr,(void*)MyProcess,(void*)marg);
>  >         pthread_attr_destroy(&attr);
>  >                 if(th_res==0)
>  >         {
>  >         tCount++;
>  >         printf("TH COUNT : %d\n",tCount);
>  >         mThread[tCount-1].tIDX=tCount;
>  >         mThread[tCount-1].tID=thread;      
>  >         pthread_join(thread,&mRetTh);
>  >         //close(new_fd); if i close the socket ,
> accept will
>  > create the same hnd_new_sock like previous...bad
>  > ideea.
>  >           
>  >         }
>  >         else
>  >         {
>  >             perror("CREATE_THREAD");  
>  >         }
>  >                }
>  >    }
>  >
>  > }
>  > void MyProcess(MyARGV* MArg){  
>  >     int maxtry=0,mTime=0;
>  >     int lenrecv=0,lenwrite=0,mCount=1,dCnt=0;
>  >     int MyWait=0;
>  >     char* mBuff=NULL;
>  >     char* realPath=NULL;
> 
=== message truncated ===



		
__________________________________ 
Do you Yahoo!? 
Check out the new Yahoo! Front Page. 
www.yahoo.com 
 


  reply	other threads:[~2004-11-07 17:47 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-11-07 13:09 Problem with pthreads on socket Ron Michael Khu
2004-11-07 17:47 ` Linux Kernel [this message]
  -- strict thread matches above, loose matches on Subject: below --
2004-11-06 10:45 Linux Kernel
2004-11-06 21:15 ` Jan-Benedict Glaw
2004-11-07  9:15   ` Linux Kernel
2004-11-07 17:05     ` Jan-Benedict Glaw

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20041107174734.88175.qmail@web90003.mail.scd.yahoo.com \
    --to=linuxkrnl@yahoo.com \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=ronkhu@ntsp.nec.co.jp \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).