linux fopen 段错误,对fgets的pthread和fopen64段错误

alk..

5

pthread_create()期待void * (*)(void *)作为线程函数,但你正在传递void (*)(void *).

更新:

您缺少原型fopen64(),因此编译器假定int哪个不相同FILE*.

更新1:

要使这个原型可用(并通过此修复您的初始问题),只需添加:

#define _LARGEFILE64_SOURCE

作为源文件中的第一行.

附加编辑:确切的说:_LARGEFILE64_SOURCE需要#define编辑之前#include荷兰国际集团stdio.h

更新2:

按照我用来制作suxxer work(main.c)的源代码:

#define _LARGEFILE64_SOURCE

#include

#include

typedef struct threadArgs

{

char* argsList;

int argc;

} threadArgs;

void *

threadRun(void *pArg);

int

main(int argc, char* argv[]) /* line 16 */

{

int err = 0;

threadArgs thrArgs;

pthread_t thrd;

if (argc > 1)

{

printf("creating thread \n");

err = pthread_create (&thrd, NULL, threadRun, &thrArgs);

printf("pthread_create returned: %d \n", err);

pthread_join(thrd, NULL);

}

else

{

printf("no thread - just calling func \n");

threadRun((void*)&thrArgs);

}

printf("Exiting main() \n");

return err;

}

void *

threadRun(void *pArg) /* line 40 */

{

printf("IN the Thread \n");

char* pStr;

FILE *pFile = NULL;

pFile = fopen64("test.txt","r");

if (pFile==NULL)

{

printf("pFile is NULL \n");

}

else

{

printf("pFile is NOT null \n");

char line[256];

pStr = fgets(line, sizeof(line),pFile);

if (pStr)

{

printf("line retrieved: %s \n", line);

}

else

{

printf("no line retrieved \n");

}

}

printf("End of pthread run func \n");

return 0;

}

建立者:

$gcc -Wall -g -o main main.c -pedantic -Wextra -std=c99 -pthread

main.c: In function ‘main’:

main.c:16: warning: unused parameter ‘argv’

main.c: In function ‘threadRun’:

main.c:40: warning: unused parameter ‘pArg’

(没有其他错误或警告)

环境:

$ uname -a

Linux debian-stable 2.6.32-5-amd64 #1 SMP Sun Sep 23 10:07:46 UTC 2012 x86_64 GNU/Linux

$ gcc --version

gcc (Debian 4.4.5-8) 4.4.5

[...]

$ ldd main

linux-vdso.so.1 => (0x00007fff466d6000)

libpthread.so.0 => /lib/libpthread.so.0 (0x00007f15ccd20000)

ibc.so.6 => /lib/libc.so.6 (0x00007f15cc9be000)

/lib64/ld-linux-x86-64.so.2 (0x00007f15ccf4b000)

输出(使用main.c没有\ns 的源代码test.txt):

$ valgrind ./main 1

==31827== Memcheck, a memory error detector

==31827== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.

==31827== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info

==31827== Command: ./main 1

==31827==

creating thread

pthread_create returned: 0

IN the Thread

pFile is NOT null

line retrieved: #define _LARGEFILE64_SOURCE #include #include typedef struct threadArgs { char* argsList; int argc; } threadArgs; void * threadRun(void *pArg); int main(int argc, char* argv[]) { int err = 0; threadArgs thrArgs; pthread_t thrd; if (a

End of pthread run func

Exiting main()

==31827==

==31827== HEAP SUMMARY:

==31827== in use at exit: 568 bytes in 1 blocks

==31827== total heap usage: 2 allocs, 1 frees, 840 bytes allocated

==31827==

==31827== LEAK SUMMARY:

==31827== definitely lost: 0 bytes in 0 blocks

==31827== indirectly lost: 0 bytes in 0 blocks

==31827== possibly lost: 0 bytes in 0 blocks

==31827== still reachable: 568 bytes in 1 blocks

==31827== suppressed: 0 bytes in 0 blocks

==31827== Rerun with --leak-check=full to see details of leaked memory

==31827==

==31827== For counts of detected and suppressed errors, rerun with: -v

==31827== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 4)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个可能的修改方案,使用了多线程来处理客户端的输入: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/socket.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <pthread.h> #define MAXLINE 1024 void *handle_connection(void *arg); void list_dir(int connfd); void download(int connfd, char *filename); int main(int argc, char *argv[]) { int listenfd, connfd, port; struct sockaddr_in servaddr, cliaddr; socklen_t clilen; pthread_t tid; if (argc != 2) { fprintf(stderr, "usage: %s <port>\n", argv[0]); exit(EXIT_FAILURE); } port = atoi(argv[1]); if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket error"); exit(EXIT_FAILURE); } memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(port); if (bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) { perror("bind error"); exit(EXIT_FAILURE); } if (listen(listenfd, 5) == -1) { perror("listen error"); exit(EXIT_FAILURE); } printf("服务器已启动,正在监听端口 %d...\n", port); while (1) { clilen = sizeof(cliaddr); if ((connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &clilen)) == -1) { perror("accept error"); continue; } printf("客户端 %s:%d 已连接\n", inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port)); if (pthread_create(&tid, NULL, handle_connection, (void *)&connfd) != 0) { perror("pthread_create error"); exit(EXIT_FAILURE); } pthread_detach(tid); } close(listenfd); return 0; } void *handle_connection(void *arg) { int connfd = *(int *)arg; char buffer[MAXLINE], str[MAXLINE]; char *pos; while (1) { memset(buffer, 0, sizeof(buffer)); if (recv(connfd, buffer, sizeof(buffer), 0) < 0) { perror("recv error"); break; } if (strcmp(buffer, "exit") == 0) { printf("客户端 %d 已断开连接\n", connfd); break; } else if (strcmp(buffer, "ls") == 0) { list_dir(connfd); } else if (strncmp(buffer, "download", 8) == 0) { memset(str, 0, sizeof(str)); if (recv(connfd, str, sizeof(str), 0) < 0) { perror("recv error"); break; } pos = strchr(str, ' '); if (pos != NULL) { printf("空格后的字符串为:%s\n", pos + 1); download(connfd, pos+1); } else { printf("请输入正确的文件格式\n"); } } else { printf("未知命令:%s\n", buffer); if (send(connfd, buffer, strlen(buffer), 0) < 0) { perror("send error"); break; } } } close(connfd); pthread_exit(NULL); } void list_dir(int connfd) { FILE *fp; char buffer[MAXLINE]; fp = popen("ls -l", "r"); if (fp == NULL) { perror("popen error"); return; } while (fgets(buffer, MAXLINE, fp) != NULL) { if (send(connfd, buffer, strlen(buffer), 0) < 0) { perror("send error"); break; } } pclose(fp); } void download(int connfd, char *filename) { FILE *fp; char buffer[MAXLINE]; int n; if ((fp = fopen(filename, "r")) == NULL) { perror("fopen error"); return; } while ((n = fread(buffer, 1, sizeof(buffer), fp)) > 0) { if (send(connfd, buffer, n, 0) < 0) { perror("send error"); break; } } fclose(fp); } ``` 这个修改后的代码使用了 `pthread_create()` 函数创建了一个新线程来处理每个客户端的连接,这样可以避免 `recv()` 函数的阻塞等待。在新线程中,我们使用 `recv()` 函数来接收客户端发来的命令和文件名,并根据不同的命令调用不同的函数来处理。在 `download()` 函数中,我们使用了标准的文件操作函数 `fopen()` 和 `fread()` 来读取文件内容,并使用 `send()` 函数将内容发送给客户端。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值