繁体   English   中英

需要有关消息传递队列的帮助

[英]Need help on message passing queue

我还是编程新手。 我一直在努力完成这项任务。 当我运行它显示的程序时:总共发送了 0 个字节。 它应该显示发出的总字节数。 作业提示问题:

发送方:

  1. 发送方应被调用为./sender 其中是要发送给接收方的文件的名称。 例如,./sender file.txt 将发送名为 file.txt 的文件。

  2. 调用时,发送方应打开名为 cpsc351messagequeue 的消息队列。 如果不存在,发送方将以错误终止。

  3. 否则,发件人应打开命令行指定的文件并进行如下操作:

(a) 从文件中读取最多 4096 个字节。

(b) 使用 mq send() 和 1 的消息优先级发送通过消息队列读取的字节。

(c) 重复前面的步骤,直到到达文件末尾。

(d) 当到达文件末尾时,向接收方发送一条优先级为 2 的空消息,告诉接收方发送已完成。

  1. 终止。

我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <mqueue.h>
#include <unistd.h>
#include <signal.h>

#define MSQ_NAME "/cpsc351queue"


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

    // The file size
    int fileSize = -1;

    // The buffer used to store the message copied
    // copied from the shared memory
    char buff[4096];

    // The variable to hold the message queue ID
    int msqid = -1;

    // The total number of bytes written
    int totalBytesRead = 0;

    // The number of bytes 
    int bytesRead = 0;

    // Whether we are done reading  
    bool finishedReading = false;
    
    // TODO: Define a data structure
    // of type mq_attr to specify a
    // queue that can hold up to 10
    // messages with the maximum message
    // size being 4096 bytes
    mq_attr attr;

    attr.mq_flags = 0;
    attr.mq_maxmsg = 10;
    attr.mq_msgsize = 4096;
    attr.mq_curmsgs = 0;

    // Sanity checks -- make sure the user has provided a file
    if(argc < 2)
    {
        fprintf(stderr, "USAGE: %s <FILE NAME>\n", argv[0]);
        exit(1);
    }

    // Open the file for reading
    int fd = open(argv[1], O_RDONLY);
    
    // Make sure the file was opened
    if(fd < 0)
    {
        perror("open");
        exit(1);
    }
    
    
    // TODO: Gain access to the message queue
    // whose name is defined by the macro
    // MSQ_NAME macro above. We assume that
    // the receiver has allocated the message queue.
    mqd_t qid = mq_open("/cpsc351queue", O_RDWR, 0600, &attr);

    
    //TODO: Loop below attempts to read the 
    // file 4096 bytes at a time.
    // Modify the loop as necessary to send
    // each chunk of data read as message
    // through the message queue. You can use
    // 1 for the priority of the message.



    // Keep writing until all data has been written
    while((totalBytesRead < fileSize) && !finishedReading)
    {
        
        totalBytesRead = read(fd, buff, bytesRead);

        
        bytesRead = mq_send(qid, totalBytesRead, 4096, 1);
    

        // Something went wrong
        if(bytesRead < 0)
        {
            perror("read");
            exit(1);
        }
        // We are at the end of file
        else if(bytesRead == 0)
        {
            // We are at the end of file
            finishedReading = true;     
        }
        
        
        totalBytesRead += bytesRead;
    }
    
    // TODO: Send a message with size of 0
    // to the receiver to tell it that the
    // transmission is done
        

    fprintf(stderr, "Sent a total of %d bytes\n", totalBytesRead);
    
    // TODO: Close the file     
    close(fd);
    return 0;
}

  1. 该程序应该打开名为/cpsc351queue的消息队列,而不是/cpsc351messagequeue

  2. 应使用以下参数调用mq_send函数: qidbuffbytesRead1 目前,正在使用totalBytesRead40961调用它。

  3. 应使用以下参数调用read函数: fdbuff4096 目前,正在使用fdbuffbytesRead它。

  4. totalBytesRead变量应该在read函数被调用之后更新,而不是之前。

  5. bytesRead变量应该在调用mq_send函数之后更新,而不是之前。

  6. 循环应该一直持续到文件末尾,这可以通过检查bytesRead是否小于 0 来确定。目前,循环一直持续到bytesRead等于 0。

  7. 循环结束后,程序应该向接收者发送一条优先级为 2 的空消息,告诉接收者发送已完成。 这可以通过调用消息大小为 0 且优先级为 2 的mq_send函数来完成。

通过这些更改,程序应以 4096 字节块的形式读取文件,通过消息队列发送每个块,然后在到达文件末尾时向接收方发送一条空消息。

#include <fcntl.h>
#include <mqueue.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>

#define QUEUE_NAME "/cpsc351messagequeue"
#define MAX_MESSAGE_SIZE 4096
#define MAX_QUEUE_SIZE 8192

int main(int argc, char *argv[]) {
  if (argc < 2) {
    fprintf(stderr, "Usage: %s <file-name>\n", argv[0]);
    return 1;
  }

  // Open the message queue
  mqd_t queue = mq_open(QUEUE_NAME, O_WRONLY);
  if (queue == (mqd_t)-1) {
    perror("Error opening message queue");
    return 1;
  }

  // Open the file
  FILE *file = fopen(argv[1], "r");
  if (file == NULL) {
    perror("Error opening file");
    return 1;
  }

  // Read at most 4096 bytes from the file and send them through the message queue
  char buffer[MAX_MESSAGE_SIZE];
  size_t bytes_read;
  while ((bytes_read = fread(buffer, 1, MAX_MESSAGE_SIZE, file)) > 0) {
    if (mq_send(queue, buffer, bytes_read, 1) == -1) {
      perror("Error sending message");
      return 1;
    }
  }

  // Send an empty message to the receiver with a priority of 2 to indicate that the sending is done
  if (mq_send(queue, "", 0, 2) == -1) {
    perror("Error sending message");
    return 1;
  }

  // Close the file and message queue
  fclose(file);
  mq_close(queue);

  return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM