QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 799|回复: 5

静态变量引起线程不安全,搞不懂

[复制链接]
发表于 2003-4-27 18:10:37 | 显示全部楼层 |阅读模式
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include&lt;sys/types.h&gt;
#include&lt;sys/socket.h&gt;
#include&lt;netinet/in.h&gt;
#include&lt;arpa/inet.h&gt;
#include&lt;pthread.h&gt;

#define PORT 1234
#define BACKLOG 5
#define MAXDATASIZE 1000

void process_cli(int connectfd,struct sockaddr_in client);
void savedata(char *recvbuf,int len,char *cli_data);
void *start_routine(void *arg);
struct ARG
{
int connfd;
struct sockaddr_in client;
};

main()
{
int listenfd,connectfd;
struct sockaddr_in server;
struct sockaddr_in client;
int sin_size;
pthread_t thread;
struct ARG *arg;


if((listenfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("Creating socket failed!\n");
exit(1);
}

int opt=SO_REUSEADDR;
setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&amp;opt,sizeof(opt));
bzero(&amp;server,sizeof(server));
server.sin_family=AF_INET;
server.sin_port=htons(PORT);
server.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(listenfd,(struct sockaddr *)&amp;server,sizeof(struct sockaddr))==-1)
{
perror("bind error\n");
exit(1);
}

if(listen(listenfd,BACKLOG)==-1)
{
perror("listen error\n");
exit(1);
}

sin_size=sizeof(struct sockaddr_in);

while(1)
{
if((connectfd=accept(listenfd,(struct sockaddr *)&amp;client,&amp;sin_size))==-1)
{
perror("accept error\n");
exit(1);
}

arg=(struct ARG *)malloc(sizeof(struct ARG));
arg-&gt;connfd=connectfd;
memcpy((void *)&amp;arg-&gt;client,&amp;client,sizeof(client));

if(pthread_create(&amp;thread,NULL,start_routine,(void *)arg))
{
perror("pthread_create() error\n");
exit(1);
}
}
close(listenfd);
}

void process_cli(int connectfd,struct sockaddr_in client)
{
char cli_data[5000];
int num,i;
char recvbuf[MAXDATASIZE],sendbuf[MAXDATASIZE],cli_name[MAXDATASIZE];

printf("you got a connection from %s. ",inet_ntoa(client.sin_addr));
num=recv(connectfd,cli_name,MAXDATASIZE,0);
if(num==0)
{
close(connectfd);
printf("client disconnected.\n");
return;
}
cli_name[num-1]='\0';
printf("client's name is %s\n",cli_name);

while(num=recv(connectfd,recvbuf,MAXDATASIZE,0))
{
recvbuf[num]='\0';

printf("Received client(%s) message:%s",cli_name,recvbuf);
savedata(recvbuf,num,cli_data);
for(i=0;i&lt;num-1;i++)
{
sendbuf=recvbuf[num-i-2];
}
sendbuf[num-1]='\0';
send(connectfd,sendbuf,strlen(sendbuf),0);
}
close(connectfd);
printf("Client(%s) closed connection. user'data:%s\n",cli_name,cli_data);
}

void *start_routine(void *arg)
{
struct ARG *info;
info=(struct ARG *)arg;
process_cli(info-&gt;connfd,info-&gt;client);
free(arg);
pthread_exit(NULL);
}

void savedata(char *recvbuf,int len,char *cli_data)
{
int i;
static int index =0;
for(i=0;i&lt;len-1;i++)
{
cli_data[index++]=recvbuf;
}
cli_data[index]='\0';
}

运行结果:
服务器:
#./multiserver
you got a connection from 127.0.0.1.
you got a connection from 127.0.0.1.
client's name is client1
client's name is client2
Received client(client1) message:1234
Received client(client2) message:abc
Received client(client1) message:a
Received client(client2) message:d
Client(client1) closed connection. user'data:1234
Client(client2) closed connection. user'data:

客户端1
$./itrclient 127.0.0.1
connected to server.
input name : client1
Input string to server:1234
server message: 4321
Input string to server:a
server message: a
Input string to server:^D
exit
$
客户2:
./itrclient 127.0.0.1
connected to server.
input name : client2
Input string to server:abc
server message: cba
Input string to server:d
server message: d
Input string to server:^D
exit

用户1关闭时显示用户数据“1234”
2关闭时显示“ ”。
我觉得应该输出:"1234a" 和”abcd “阿
原因时什么阿。分析一下好么。
 楼主| 发表于 2003-4-27 18:42:14 | 显示全部楼层
这是客户端程序
#include&lt;stdio.h&gt;
#include&lt;strings.h&gt;
#include<unistd.h>
#include&lt;sys/types.h&gt;
#include&lt;sys/socket.h&gt;
#include&lt;netinet/in.h&gt;
#include&lt;netdb.h&gt;
#define PORT 1234
#define MAXDATASIZE 1000

void process(FILE *fp,int sockfd);
char *getmessage(char *sendline,int len,FILE *fp);
int main(int argc,char *argv[])
{
int fd;
struct hostent *he;
struct sockaddr_in server;

if(argc!=2)
{
printf("Usage: %s <ip address>\n",argv[0]);
}

if((he=gethostbyname(argv[1]))==NULL)
{
printf("gethostname() error\n");
exit(1);
}

if((fd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
printf("socket() error\n");
exit(1);
}

bzero(&amp;server,sizeof(server));
server.sin_family=AF_INET;
server.sin_port=htons(PORT);
server.sin_addr=*((struct in_addr *)he-&gt;h_addr);

if(connect(fd,(struct sockaddr *)&amp;server,sizeof(struct sockaddr))==-1)
{
perror("connect() error");
exit(1);
}

process(stdin,fd);
close(fd);
}

void process(FILE *fp,int sockfd)
{
char sendline[MAXDATASIZE],recvline[MAXDATASIZE];
int numbytes;
printf("connected to server.\n");
printf("input name : ");
if(fgets(sendline,MAXDATASIZE,fp)==NULL)
{
printf("\nExit.\n");
return;
}
send(sockfd,sendline,strlen(sendline),0);




while(getmessage(sendline,MAXDATASIZE,fp)!=NULL)
{
send(sockfd,sendline,strlen(sendline),0);
if((numbytes=recv(sockfd,recvline,MAXDATASIZE,0))==0)
{
printf("server teiminated\n");
return;
}

recvline[numbytes]='\0';
printf("server message: %s\n",recvline);
}
printf("\nexit\n");
}

char *getmessage(char *sendline,int len,FILE *fp)
{
printf("Input string to server:");
return(fgets(sendline,MAXDATASIZE,fp));
}
回复

使用道具 举报

 楼主| 发表于 2003-4-27 19:01:49 | 显示全部楼层
这个程序的意图是,服务器加上了一个客户数据存储功能,用来存储每个连接客户发来的所有数据,当连接中止后,显示客户的名字,及相应的数据。

但是运行结果出现前面的问题
回复

使用道具 举报

发表于 2003-4-28 01:41:42 | 显示全部楼层
at least u server side possibly has problem in 'static int index =0;' all thread use this and since this is static, so all thread reference one variable. u had better change this.

a rule of thumb, never use static or global variable in thread. except using lock, which is expensive.
回复

使用道具 举报

发表于 2003-4-28 01:50:00 | 显示全部楼层
btw, u had better include u code in [code:1][/code:1] for easy read. it is difficult to read a code like this. this will limit the chance that people read u code and give u solution.
回复

使用道具 举报

 楼主| 发表于 2003-4-28 07:56:51 | 显示全部楼层
多谢,明白啦!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-11-16 02:54 , Processed in 0.046261 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表