客户端之前运行多个进程服务器

时间:2013-10-22 03:08:35

标签: c linux struct posix

我仍在从事与操作系统课程相同的作业。现在,这是我的问题(首先,我正在重写HW的内容): 将实施两个程序:客户端程序“get”和名为“iserv”的服务器程序。客户端将要求服务器检索并发送特定范围内的所有整数。服务器将是一个多进程程序。它将创建子进程来处理来自客户端的请求。将使用POSIX消息队列。 这是我的iserv.c(服务器程序)

struct item   /*struct for client requests to the server*/
{
    int maxvalue;
    int minvalue;
    char *queuename;
};

int main(int argc , char *argv[])
{
    pid_t apid1;
    FILE *fp;
    mqd_t mq;
    const char *msgqueue = "/serverqueue";
    int oflag = (O_RDWR|O_CREAT);
    mode_t mode = (S_IRUSR|S_IWUSR);


    if(argc != 3)
    {
        printf("wrong number of arguments");
        exit(1);
    }
    //create server message queue
    mq = mq_open(msgqueue ,oflag , mode , NULL);
    if(mq==-1)
    {
        perror("can not open msg queue\n");
        exit(1);
    }
    printf("mq opened , mq id = %d\n" , (int) mq);



    //******get the maxvalue and minvalue*******



    mq_getattr(mq , &attr);
    printf("mq maximum msgsize = %d\n" , (int) attr.mq_msgsize);
    /*allocate large enough space for the buffer*/
    buflen = attr.mq_msgsize;
    bufptr = (char *)malloc(buflen);
    n = mq_receive(mq , (char *)bufptr , buflen , NULL);

    if(n == -1)
    {
        perror("mq_receive failed\n");
        exit(1);
    }

    itemptr = (struct item *) bufptr;

    printf("min value = %d\n" , itemptr->minvalue);
    printf("max value = %d\n" , itemptr->maxvalue);
    fprintf(stderr , "queue name = %s\n" , itemptr->queuename);


    free(bufptr);
    mq_close(mq);
    mq_unlink(msgqueue);
    return 0;
}

和get.c(客户端程序)

struct item   /*struct for client requests to the server*/
{
    int maxvalue;
    int minvalue;
    char *queuename;
};



int main(int argc , char *argv[])
{

    FILE *file;
    mqd_t mq;
    mqd_t mq2;

    const char *msgqueue = "/serverqueue";
    int oflag = (O_RDWR|O_CREAT);
    mode_t mode = (S_IRUSR|S_IWUSR);
    struct mq_attr *attr = NULL;

    struct item item; //for serverqueue

    int n;

        if(argc != 5)
    {
        printf("wrong number of arguments");
        exit(1);

    mq = mq_open(msgqueue ,oflag , mode , attr);

    if(mq==-1)
    {
        perror("can not open msg queue\n");
        exit(1);
    }
    printf("mq opened , mq id = %d\n" , (int) mq);


    //send max - min values and the client message queue name to the serverqueue as a request
    while(1)
    {
        item.maxvalue = atoi(argv[3]);
        item.minvalue = atoi(argv[2]);
        item.queuename = clientqueue;

        n = mq_send(mq , (char *) &item , sizeof(item) , 0);
        if(n==-1)
        {
            perror("mq_send failed\n");
            exit(1);
        }
        else
        {
            printf("mq_send success , item size = %d\n" , sizeof(struct item));
            printf("%d" , item.maxvalue);
            printf("%d" , item.minvalue);
            printf("\n" , item.queuename);
        }

    }


    mq_close(mq);
    mq_unlink(msgqueue);
    return 0;

}

生成文件:

all: iserv get
iserv: iserv.c
    gcc -g -Wall -o iserv iserv.c -lrt
get: get.c
    gcc -g -Wall -o get get.c -lrt

clean:
    rm -fr *o iserv get

现在,我的第一个问题是,即使我使用mq_close和mq_unlink删除了消息队列,当我想运行这两个程序终端时说:没有为'all'做任何事情。我需要删除其他东西来重新运行这两个程序而不用手关闭或删除任何东西。这是什么??

第二个问题是服务器不能将消息响应中的char *queuename;作为名称。它在终端上打印出一些愚蠢的东西,如何在此消息响应中将字符串queuename传递给服务器?

请帮帮我,作业截止日期已经结束我需要解决这些问题,谢谢大家帮忙!!

1 个答案:

答案 0 :(得分:0)

这是你的主要问题:

struct item   /*struct for client requests to the server*/
{
    int maxvalue;
    int minvalue;
    char *queuename;  //<=== NO GOOD
};

您无法通过MQ向另一个进程发送指针 - 它在发送过程中指向的内容在接收过程中毫无意义。您需要将该指针更改为固定长度的字符数组。像

这样的东西
char queuename[50];

正如我在评论中所说,你需要将queuename复制到该数组中。

相关问题