1

私はまだオペレーティング システム コースの同じ宿題に取り組んでいます。さて、ここで私の質問です (まず、HW の内容を書き直しています): クライアント プログラム "get" と "iserv" と呼ばれるサーバー プログラムの 2 つのプログラムが実装されます。クライアントは、特定の範囲内のすべての整数を取得して送信するようにサーバーに要求します。サーバーはマルチプロセス プログラムになります。クライアントからのリクエストを処理する子プロセスを作成します。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 を使用してメッセージ キューを削除しても、これら 2 つのプログラムを実行したい場合、端末は次のように言うことです。「すべて」に対して何も行われません。手で何かを閉じたり削除したりせずに、これら2つのプログラムを再実行するには、何かを削除する必要があります。これは何ですか??

2 番目の質問はchar *queuename;、サーバーがメッセージ応答を名前として取得できないことです。ターミナルに何かばかげたことを出力します。このメッセージ応答で文字列 queuename をサーバーに渡すにはどうすればよいですか??

助けてください、宿題の期日が迫っています。これらの問題を解決する必要があります。助けてくれてありがとう!!

4

1 に答える 1