473,394 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Race condition on accept() system call using pthread on Red Hat ES 2.1 (kernel 2.4.9)

Server: IBM XSERIES 225 (Intel Xeon CPU 2.40GHz 1GB RAM)
Operating System: Linux RedHat ES 2.1 kernel 2.4.9
Languaje: C++
Compiler: gcc 2.96
Libs: pthread

We are in need of your help in order to solve the folowing problem:

We´re working on a server side application that implements threads
(pthread under linux Red Hat ES 2.1 kernel 2.4.9). The problem seems
to be a race condition which occurs during an accept() system call, in
which the file descriptor returned is still in use by another thread.

I think there is a confict between the accept() and close() system
calls, and I´m trying to find an alternative solution, changing the
thread based architecture for a fork based architecture.
Attached is a piece of code

Any light you can shed on this difficulty will be greatly appreciated.
Sincerely,
Fran
int main(void){
pthread_t thread_id;
pthread_attr_t attr;
struct protoent *ppe;
struct sockaddr_in sin;
int on=1;
struct linger ling;
int s;
fd_set rd_set,aux_set;

FD_ZERO(&rd_set);

memset(&sin,0,sizeof sin);
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(7000);
ppe = getprotobyname("tcp");
if((s = socket(PF_INET,SOCK_STREAM, ppe->o_proto))<0){
exit(1);
}

setsockopt(s,SOL_SOCKET,SO_REUSEADDR,&on,sizeof on);
ling.l_onoff = 1;
ling.l_linger = 10;
setsockopt(s,SOL_SOCKET,SO_LINGER,&ling,sizeof ling);

if(bind(s,(struct sockaddr *)&sin,sizeof sin)<0){
exit(1);
}

if(listen(s,5)<0){
exit(1);
}

FD_SET(s,&rd_set);
while(1){
memcpy(aux_set,rd_set,sizeof(rd_set));
if((ret=select(FD_SETSIZE,&aux_set,NULL,NULL,NULL) )>0){
if(FD_ISSET(s,&aux_set)){
if((ns = accept(s,0,0))==(-1)){
exit(1);
}
pthread_attr_init(&attr);
pthread_attr_setdetachstart(&attr,PTHREAD_CREATE_D ETACHED);
if(pthread_create(&thread_id,&attr,hijo,(void *)&ns)!=0){
exit(1);
}
}
}
}
}
void hijo(void *arg){
int ns,len;
char trx[1000];
char ans[1000];

ns = *((int*)arg);
memset(trx,0,1000);
memset(ans,0,1000);

// The file descriptor is being corrupted when concurrency level is
high

// At this point . . .
// FD = 7
// LOCALIP = 192.168.102.1
// LOCALPORT = 32333
// REMOTEIP = 192.168.102.10
// REMOTEPORT = 31252
read(ns,trx,1000)
len = generate_answer(trx,ans);

// and then . . .
// FD = 7
// LOCALIP = 40.50.255.255
// LOCALPORT = 0
// REMOTEIP = 30.0.0.0
// REMOTEPORT = 99999
write(s,buff,len);
shutdown(s);
close(s);
}
Nov 14 '05 #1
2 3587
fran <fj*****@yahoo.com.ar> spoke thus:
Server: IBM XSERIES 225 (Intel Xeon CPU 2.40GHz 1GB RAM)
Operating System: Linux RedHat ES 2.1 kernel 2.4.9
Languaje: C++
Compiler: gcc 2.96
Libs: pthread


(comp.unix.programmer, comp.programming.threads. It's way OT here,
and pretty OT in comp.lang.c++ as well.)

Your post is off-topic for comp.lang.c. Please visit

http://www.ungerhu.com/jxh/clc.welcome.txt
http://www.eskimo.com/~scs/C-faq/top.html
http://benpfaff.org/writings/clc/off-topic.html

for posting guidelines and frequently asked questions. Thank you.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #2
boa
fran wrote:

As others have mentioned, this is off topic here. Since you seem to be
in big trouble and I know the answer, I will post here with FUT to
comp.programming.threads. Hope that's OK for c.l.c the regulars...

The problem is the way you pass arguments to the new thread. main() and
hijo() shares the same variable, ns, which means that main() can
overwrite the variable before hijo() is able to read it. I guess that
even multiple instances of hijo() can work on the same file descriptor.

The solution? Allocate a pointer an int to the new threads argument(s)
and assign the fd to that int. Then free the allocated memory in hijo().
At least that is one solution. ;-)

HTH
boa@home

[snip]
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: GIMME | last post by:
One of my coworkers insists that one should never use static methods because race conditions exist. Thinking along the lines that all variable assignments are assignments to static variables. ...
9
by: Vai2000 | last post by:
Hi All, Here's a situation, I am trying to process some large Files via a Win Svc using FileMonitor. During the test process I tried copy and pasting a file in a folder and got an error saying...
5
by: anonymous | last post by:
I'm writing a program that deals extensively with the printer. For the most part my application runs fine, but occasionally I run into some Exceptions. The most common exceptions I run into are...
18
by: Urs Vogel | last post by:
Hi I wrote an application server (a remoting sinlgeton), where processes must be stopped in very rare cases, done thru a Thread.Abort(). Occasionally, and only after a Thread.Abort(), this...
10
by: noleander | last post by:
I've got an application that uses Pthread to do threading. Mostly Im using Condition Variables and the associated function calls: - pthread_cond_wait() - pthread_cond_signal() -...
8
by: Henrik | last post by:
Hi Is there any way to see what the System process is doing? We have developed an application running at a production site to measure and optimize the production. The application needs to be...
21
by: omkar pangarkar | last post by:
Hi all, I have two simple hello world programs one using printf() and other using write() --prog 1-- #include<stdio.h> #include<stdlib.h> int main() { printf("Hello"); /* up to here...
7
by: Berryl Hesh | last post by:
I wouldn't nomally post this here, as it has something to do with the ListView control usage I think, or maybe with a race condition or some windoes messaging. I'm just not sure. The test below...
0
ashitpro
by: ashitpro | last post by:
Writing System Call Wrappers in User Space. Recently I saw a post in Linux/Unix section to know the user who deleted the file in linux. Currently there is nothing in linux which could achieve...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.