1

我试图修复遗留代码中的一些错误。这是代码:

#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*)&param,&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(我在某处看到它是正确的值),然后我尝试这样调用fgetsfgets(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的手册页,因为我以前从未使用过它。

欢迎任何帮助和提示。

再次感谢!

4

1 回答 1

1

我让您的代码通过一些更改即可运行。我没有设法让 System V-stuff 工作,但我让它读取命令而不会崩溃。

我认为您的程序中 System V-stiff 的初始化是错误的,并且会产生未定义的行为。

所有的close(0)-s 可能来自某处的服务器线程,它不会因错误而中断 while 循环。

System V的初始化(我不知道如何使用它,真的,只是阅读手册页),我是这样的:

    // msg_key is declared above the server-code (used by both) like `key_t msg_key`
    msg_key = ftok ("/home/atle/prog/test.c", 'A');
    if (msg_key == -1) {
            printf ("ftok error: %s\n", strerror(errno));
            exit(1);
    }
    msgkey = msgget(msg_key, IPC_CREAT);

    if ( msgkey == -1) {
            printf ("msgget error: %s\n", strerror(errno));
            exit(1);
    }

在服务器中,我这样做了:

    int msgkey_server = msgget(msg_key, 0);
    if ( msgkey_server == -1) {
            printf ("Server msgget error: %s\n", strerror(errno));
            exit(1);
    }

    while (1) {
            printf ("Server loop\n");
            if ( msgrcv(msgkey_server,&client,sizeof(client),0,0) == -1 ) {
                    printf ("Server read error: %s\n", strerror(errno));
                    exit(1);
            }
            else {
                   // MSG READ OK
            }

顺便提一句:

当您将结构传递给附加大小的命令时,您应该始终sizeof()像我在这里所做的那样使用,而不是手动编写大小(会导致问题)。

额外的顺便说一句:

您需要 errno 标头:

#include <errno.h>
于 2013-10-18T14:17:42.897 回答