C stdin不起作用

时间:2013-10-17 21:16:08

标签: c linux

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

#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只是从队列标识msgkey中的另一个应用程序接收一些消息并打印一些文本。

问题是当我进入do while循环时,我需要获取一些用户输入,fgets失败...当我打印错误(使用errno)时,我看到了这是一个错误的文件描述符错误(EBADF)。

Process函数让命令a调用相应的函数,它基本上会通过套接字发送一些数据。 Process调用的这些函数来自用C ++编写的函数C extern-ed的lib(libSvr)。

我正在使用带有gcc 4.7.2的ubuntu 12.10机器。

我试图在预处理器(使用gcc -E)和stdin之后看到代码仍显示为stdin。我尝试打印fileno(stdin)的输出,它是0(我看到某个地方它的值正确),然后我尝试以这种方式调用fgetsfgets(cmd,100,0)。但没有任何作用。

任何人都知道发生了什么事?

谢谢!

修改

包含所有代码。

我尝试拨打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的手册页,因为我以前从未使用它。

欢迎任何帮助和提示。

再次感谢!

1 个答案:

答案 0 :(得分:1)

我让你的代码运行一些变化。我没有设法让System V-stuff工作,但是我让它在没有崩溃的情况下读取命令。

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

所有close(0) - s可能来自某个地方的服务器线程,它不会从错误的while循环中断开。

System V的初始化(我不知道如何使用它,真的,只是阅读man-pages),我确实喜欢这个:

    // 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
            }

顺便说一句:

当你将一个struct传递给一个附加了它的大小的命令时,你应该像我在这里一样使用sizeof(),而不是手动编写大小(导致问题)。

额外BTW:

你需要errno-header:

#include <errno.h>