我试图修复遗留代码中的一些错误。这是代码:
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <sys/msg.h>
#include "libSvr.h"
#define CONEC 3
#define CONFIG 4
typedef struct {
long type;
int cmd;
int id;
char data[7000];
} msg;
typedef struct {
int id[100];
unsigned long ip[100];
int type[100];
char name[100][20];
int num;
} conec;
typedef struct {
char ip;
char svr;
} param_cfg;
typedef struct {
unsigned long my_ip;
unsigned long svr_ip;
} tcp_cfg;
msg client, server;
conec conec_tab;
param_cfg param;
tcp_cfg setup;
void *Receive_MSG(void *arg) {
int i;
while (1) {
if ( msgrcv(msgkey,&client,7008,2,0) != -1 ) {
switch (client.cmd) {
case CONEC:
memmove((char*)&conec_tab,client.data,sizeof(conec_tab));
printf("\n%d\n",conec_tab.num);
break;
case CONFIG:
switch (conec_tab.type[client.id]) {
case 0:
memmove((char*)¶m,&client.data[sizeof(param_cfg)],sizeof(param_cfg));
printf("------------PARAM Config------------\n");
printf("%c\n%c\n\n",param.ip,param.svr);
break;
case 1:
memmove((char*)&setup,client.data,sizeof(tcp_cfg));
printf("\n------------Setup------------\n");
printf("%s\n",Inet_ntoa(setup.my_ip));
break;
}
}
}
}
}
void send_msg() {
msgsnd(msgkey,&(server.type),7008,0);
}
void Process(char *command) {
server.type = 1;
if ( !strncmp(command,"text",8) ) {
server.cmd = 1;
send_text();
send_msg();
}
else if ( !strncmp(command,"image",9) ) {
server.cmd = 2;
send_image();
send_msg();
}
else
printf("\nCommand not found.\n");
}
int main()
{
int i;
char cmd[100];
msgkey=msgget(100,0);
if ( msgkey == -1) {
printf("ERROR msgkey!!");
exit(1);
}
pthread_t receive;
pthread_create(&receive,NULL,Receive_MSG,NULL);
do {
printf("Enter a command -> ");
if (fgets(cmd,100,stdin) == NULL) {
printf("error");
exit(1);
}
cmd[strlen(cmd)-1]='\0';
if ( (strcmp(cmd,"exit")) && (strcmp(cmd,"")) )
Process(cmd);
} while (strcmp(cmd,"exit"));
return 0;
}
libSvr.h
代码:
#ifndef LIBSVR_H
#define LIBSVR_H
#pragma pack(push,1)
#pragma pack(1)
extern "C" void send_text();
extern "C" void send_image();
#pragma pack(pop)
#endif
该线程receive
只是从队列 id 中的另一个应用程序接收一些消息msgkey
并打印一些文本。
问题是当我进入 do while 循环时,我需要获取一些用户输入,但fgets
失败了......当我打印错误(带有errno
)时,我看到它是一个错误的文件描述符错误(EBADF
)。
函数获取命令 a 调用适当的函数,该Process
函数基本上将通过套接字发送一些数据。并且这些被调用的函数来自一个用 C++ 编写的带有 C extern-ed 函数Process
的 lib ( )。libSvr
我正在使用带有 gcc 4.7.2 的 ubuntu 12.10 机器。
我试图在预处理器(使用 gcc -E)之后查看代码,stdin
但仍然显示为stdin
. 我试图打印输出,fileno(stdin)
它是 0(我在某处看到它是正确的值),然后我尝试这样调用fgets
:fgets(cmd,100,0)
. 但没有任何效果。
有人知道发生了什么吗?
谢谢!
编辑:
包括所有代码。
我尝试了 call gets()
,scanf()
并且read()
还评论了线程创建(pthread_create
)但没有任何改变。
我运行了命令strace -s2000 -etrace=open,close <MYPROGRAM>
,这是输出:
open("/mnt/hgfs/projects/tls/i686/sse2/cmov/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/mnt/hgfs/projects/tls/i686/sse2/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/mnt/hgfs/projects/tls/i686/cmov/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/mnt/hgfs/projects/tls/i686/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/mnt/hgfs/projects/tls/sse2/cmov/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/mnt/hgfs/projects/tls/sse2/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/mnt/hgfs/projects/tls/cmov/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/mnt/hgfs/projects/tls/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/mnt/hgfs/projects/i686/sse2/cmov/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/mnt/hgfs/projects/i686/sse2/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/mnt/hgfs/projects/i686/cmov/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/mnt/hgfs/projects/i686/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/mnt/hgfs/projects/sse2/cmov/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/mnt/hgfs/projects/sse2/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/mnt/hgfs/projects/cmov/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/mnt/hgfs/projects/libSvr.so.1", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
open("/mnt/hgfs/projects/libpthread.so.0", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
open("/lib/i386-linux-gnu/libpthread.so.0", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
open("/mnt/hgfs/projects/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/lib/i386-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
open("/mnt/hgfs/projects/libstdc++.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/usr/lib/i386-linux-gnu/libstdc++.so.6", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
open("/mnt/hgfs/projects/libgcc_s.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/lib/i386-linux-gnu/libgcc_s.so.1", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
open("/mnt/hgfs/projects/libm.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/lib/i386-linux-gnu/libm.so.6", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
close(0) = 0
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
Enter a command ->
errorEnter a command ->
errorEnter a command ->
errorEnter a command ->
所以,stdin
文件描述符正在关闭,但现在我需要知道它在哪里关闭。
我现在正在研究一些howto和strace的手册页,因为我以前从未使用过它。
欢迎任何帮助和提示。
再次感谢!