【发布时间】:2016-04-07 09:22:16
【问题描述】:
我有以下代码。
此代码适用于 TFTP 服务器,它为接收到的每个请求创建一个分支或线程。我的问题在于线程方法。
例如,我从服务器请求 30 个文件,应该创建 30 个线程并将请求的文件提供给客户端,每个线程将发送每个文件。
如果我使用pthread_join(已注释),代码可以完美运行,但如果我没有pthread_join,它会正确提供一些文件,但其中一些文件已损坏或为空。
我相信这是一个同步问题,所以我尝试为每个客户端文件描述符malloc 一块内存,这样一个线程只能修改它自己的文件描述符,但没有运气。以下代码按原样工作并提供一些服务
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <dirent.h>
#include <pthread.h>
#define BUFSIZE 8096
#define OperationMode 1
#if OperationMode
typedef struct {
int * fd;
int hit;
} THREAD_ARGS;
void *attendFTP(void *);
#endif
int ftp(int fd, int hit);
void getFunction(int fd, char * fileName);
void putFunction(int fd, char * fileName);
char * listFilesDir(char * dirName);
void lsFunction(int fd, char * dirName);
void mgetFunction(int fd, char *dirName);
/* just checks command line arguments, setup a listening socket and block on accept waiting for clients */
int main(int argc, char **argv) {
int i, port, pid, listenfd, socketfd, hit;
socklen_t length;
static struct sockaddr_in cli_addr; /* static = initialised to zeros */
static struct sockaddr_in serv_addr; /* static = initialised to zeros */
if (argc < 3 || argc > 3 || !strcmp(argv[1], "-?")) {
printf("\n\nhint: ./tftps Port-Number Top-Directory\n\n""\ttftps is a small and very safe mini ftp server\n""\tExample: ./tftps 8181 ./fileDir \n\n");
exit(0);
}
if (chdir(argv[2]) == -1) {
printf("ERROR: Can't Change to directory %s\n", argv[2]);
exit(4);
}
printf("LOG tftps starting %s - pid %d\n", argv[1], getpid());
/* setup the network socket */
if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
printf("ERROR system call - setup the socket\n");
port = atoi(argv[1]);
if (port < 0 || port > 60000)
printf("ERROR Invalid port number (try 1->60000)\n");
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(port);
if (bind(listenfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
printf("ERROR system call - bind error\n");
if (listen(listenfd, 64) < 0)
printf("ERROR system call - listen error\n");
// Main LOOP
for (hit = 1 ;; hit++) {
length = sizeof(cli_addr);
/* block waiting for clients */
socketfd = accept(listenfd, (struct sockaddr *) &cli_addr, &length);
if (socketfd < 0)
printf("ERROR system call - accept error\n");
else
{
#if OperationMode
pthread_t thread_id;
THREAD_ARGS *args = malloc(sizeof(THREAD_ARGS));
int * sockAUX = (int *) malloc(sizeof(int *));
*sockAUX = socketfd;
args->fd = sockAUX;
args->hit = hit;
if (args != NULL) {
if (pthread_create(&thread_id, NULL, &attendFTP, args)) {
perror("could not create thread");
return 1;
}
}
//pthread_join(thread_id,NULL);
#else
pid = fork();
if(pid==0) {
ftp(socketfd, hit);
} else {
//Temos de fechar o socketfd para que seja apenas a child a tratar dos pedidos, caso contrário iria ficar aqui pendurado
close(socketfd);
kill(pid, SIGCHLD);
}
#endif
}
}
}
#if OperationMode
void *attendFTP(void *argp) {
THREAD_ARGS *args = argp;
int sock = *args->fd;
printf("FD SOCK: %d\n\n", sock);
ftp(sock, args->hit);
free(args);
//printf("Thread executou\n\n");
pthread_exit(NULL);
return NULL;
}
#endif
/* this is the ftp server function */
int ftp(int fd, int hit) {
int j, file_fd, filedesc;
long i, ret, len;
char * fstr;
static char buffer[BUFSIZE + 1]; /* static so zero filled */
printf("FD: %d\n\n", fd);
ret = read(fd, buffer, BUFSIZE); // read FTP request
if (ret == 0 || ret == -1) { /* read failure stop now */
close(fd);
return 1;
}
if (ret > 0 && ret < BUFSIZE) /* return code is valid chars */
buffer[ret] = 0; /* terminate the buffer */
else
buffer[0] = 0;
for (i = 0; i < ret; i++) /* remove CF and LF characters */
if (buffer[i] == '\r' || buffer[i] == '\n')
buffer[i] = '*';
printf("LOG request %s - hit %d\n", buffer, hit);
/* null terminate after the second space to ignore extra stuff */
for (i = 4; i < BUFSIZE; i++) {
if (buffer[i] == ' ') { /* string is "GET URL " +lots of other stuff */
buffer[i] = 0;
break;
}
}
if (!strncmp(buffer, "get ", 4)) {
// GET
getFunction(fd, &buffer[5]);
} else if (!strncmp(buffer, "ls ", 3)) {
// LS
lsFunction(fd,&buffer[3]);
} else if (!strncmp(buffer, "mget ", 4)) {
// MGET
mgetFunction(fd, &buffer[5]);
}
sleep(1); /* allow socket to drain before signalling the socket is closed */
close(fd);
return 0;
}
void getFunction(int fd, char * fileName){
int file_fd;
long ret;
printf("FD GET: %d\n\n", fd);
static char buffer[BUFSIZE + 1]; /* static so zero filled */
if ((file_fd = open(fileName, O_RDONLY)) == -1) { /* open the file for reading */
printf("ERROR failed to open file %s\n", fileName);
printf("Err: %d\n\n",errno);
sprintf(buffer, "%s", "erro");
write(fd,buffer,BUFSIZE);
close(fd);
return;
}
printf("GET -> LOG SEND %s \n", fileName);
/* send file in 8KB block - last block may be smaller */
while ((ret = read(file_fd, buffer, BUFSIZE)) > 0) {
write(fd, buffer, ret);
}
}
void lsFunction(int fd, char * dirName){
printf("LS -> LOG Header %s \n", dirName);
static char buffer[BUFSIZE + 1];
sprintf(buffer, "%s", listFilesDir(dirName));
write(fd,buffer,BUFSIZE);
}
void mgetFunction(int fd, char *dirName)
{
FILE *fp;
char path[255];
static char buffer[BUFSIZE + 1];
printf("MGET COUNT -> LOG Header %s \n", dirName);
sprintf(buffer, "%s", listFilesDir(dirName));
write(fd,buffer,BUFSIZE);
}
char * listFilesDir(char * dirName)
{
DIR *midir;
struct dirent* info_archivo;
struct stat fileStat;
char fullpath[256];
char *files = malloc (sizeof (char) * BUFSIZE);
if ((midir=opendir(dirName)) == NULL)
{
return "\nO directorio pedido não existe.\n";
}
while ((info_archivo = readdir(midir)) != 0)
{
strcpy (fullpath, dirName);
strcat (fullpath, "/");
strcat (fullpath, info_archivo->d_name);
if (!stat(fullpath, &fileStat))
{
if(!S_ISDIR(fileStat.st_mode))
{
strcat (files, info_archivo->d_name);
strcat (files, "$$");
}
} else {
return "\nErro ao ler o directório.\n";
}
}
closedir(midir);
return files;
}
这是来自服务器的日志示例
LOG tftps 从 8181 - pid 15416 FD SOCK: 4
FD:4
LOG 请求 ls 。 - 点击 1 LS -> LOG Header。 FD 袜子:5
FD:5
LOG 请求 mget 。 - 点击 2 MGET COUNT -> LOG Header。 FD袜子:4
FD:4
FD 袜子:5
FD:5
LOG 请求获取 /.gitconfig - 点击 4 FD GET: 5
GET -> LOG SEND .gitconfig LOG 请求获取 /
错误打开文件失败
FD 袜子:4
FD:4
LOG 请求获取 /ghostdriver.log - 点击 6 FD GET: 4
GET -> LOG SEND ghostdriver.log FD SOCK: 8
FD:8
LOG 请求获取 /.bash_history - 点击 7 FD GET: 8
GET -> LOG SEND .bash_history FD SOCK: 6
FD:6
LOG 请求获取 /.profile - 点击 5 FD GET: 6
GET -> LOG SEND .profile FD SOCK: 10
FD:10
LOG 请求获取 /.ICEauthority - 点击 8 FD GET: 10
GET -> LOG SEND .ICEauthority FD SOCK: 13
FD:13
FD 袜子:14
FD 袜子:22
FD:22
FD 袜子:16
FD 袜子:27
FD:27
FD 袜子:18
FD:18
FD 袜子:19
LOG 请求获取 /.gtk-bookmarks - 命中 14 FD SOCK: 25
FD:25
FD 袜子:15
LOG 请求获取 /.bashrc - 命中 20 LOG 请求获取 /.bashrc - 命中 9 FD 得到:18
FD GET:25
FD GET:13
GET -> LOG SEND .bashrc GET -> LOG SEND .bashrc FD SOCK: 23
FD:23
LOG 请求获取 /.zshrc - 命中 18 FD SOCK: 26
FD:26
FD GET:23
FD 袜子:29
FD:29
GET -> LOG SEND .bash_logoutks GET -> LOG SEND .bash_logout FD SOCK: 17
FD:17
LOG 请求获取 /.zsh-update - 命中 13 LOG 请求获取 /.zsh-update - 命中 22 FD SOCK: 28
FD GET:27
FD:14
LOG 请求获取 /.nano_history - 点击 10 LOG 请求获取 /.nano_history - 命中 24 LOG 请求获取 /.nano_history - 命中 21 FD: 16
LOG 请求获取 /.zsh_history - 命中 12 FD SOCK: 21
FD:21
FD GET:16
FD:19
LOG 请求获取 /.selected_editor - 命中 15 FD SOCK: 24
FD:24
FD:15
FD GET:14
FD GET:17
FD GET:29
GET -> LOG SEND .zcompdump-MacPearl-5.0.2 LOG 请求获取 /.zcompdump-MacPearl-5.0.2 - 命中 17 FD:28
LOG 请求获取 /.Xauthority - 点击 23 GET -> LOG SEND .Xauthority GET -> LOG SEND .Xauthority FD GET: 28
LOG 请求获取 /.Xauthority - 点击 11 GET -> LOG SEND .Xauthority FD 得到:15
GET -> LOG SEND .Xauthority GET -> LOG SEND .Xauthority FD GET: 19
FD GET:26
GET -> LOG SEND .Xauthority LOG 请求获取 /.Xauthority - 命中 16 FD 得到:21
GET -> LOG SEND .Xauthority FD GET: 22
LOG 请求获取 /.Xauthority - 点击 19 GET -> LOG SEND .Xauthority GET -> LOG SEND .Xauthority GET -> LOG SEND .Xauthority FD GET: 24
GET -> LOG SEND .Xauthority FD SOCK: 30
FD:30
FD 袜子:31
FD:31
LOG 请求获取 /.zcompdump - 命中 25 LOG 请求获取 /.zcompdump - 命中 26 FD GET:30
FD GET:31
GET -> 日志发送 .zcompdump GET -> 日志发送 .zcompdump
我可以看到有时他在多个线程中使用相同的文件描述符。我只是不知道如何解决这个同步问题。
【问题讨论】:
-
这是什么:'static char buffer[BUFSIZE + 1];' ?请解释多线程代码中的“静态”:((
-
换一种说法,对于多线程函数,'static' 是死亡之吻。
-
给出了基本代码,我们打算将代码从单处理改进为多进程和多线程。静态的那部分已经制作好了
-
摆脱它!讨厌的,邪恶的静电!仔细检查其余代码 - 任何愚蠢到在库中使用静态的人都可能搞砸了更多的东西:(
-
去除静电就够了?
标签: c pthreads pthread-join