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

0yg35tkg  于 2022-12-18  发布在  Linux
关注(0)|答案(2)|浏览(126)

我还是个编程新手。我一直在尽我最大的努力完成这项任务。当我运行程序时,它显示:共发送0个字节,应显示总发送字节数,赋值提示问题:
发送方:
1.发送方应作为./sender调用,其中是要发送给接收方的文件的名称。例如,./sender file.txt将发送名为file.txt的文件。
1.调用时,发送方应打开名为cpsc 351 messagqueue的消息队列,如果不存在,发送方应终止并返回错误。
1.否则,发送方应打开命令行中指定的文件,并按以下步骤进行:
(a)从文件中读取最多4096个字节。
(b)使用mqsend()和消息优先级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;
}
vuv7lop3

vuv7lop31#

1.程序应该打开名为/cpsc 351 queue的消息队列,而不是/cpsc 351 messagqueue。
1.调用mq_send函数时应使用以下参数:缓冲区,字节读取,1。当前,它正在使用totalBytesRead、4096和1进行调用。
1.应使用以下参数调用read函数:fd、buff和4096。当前,它是用fd、buff和bytesRead调用的。

  1. totalBytesRead变量应该在调用read函数之后更新,而不是之前。
  2. bytesRead变量应该在调用mq_send函数之后更新,而不是之前。
    1.循环应一直持续到文件末尾,这可以通过检查bytesRead是否小于0来确定。当前,循环一直持续到bytesRead等于0。
    1.循环结束后,程序应该向接收方发送一个优先级为2的空消息,告诉接收方发送完成,这可以通过调用消息大小为0、优先级为2的mq_send函数来完成。
    通过这些更改,程序应该以4096字节的块读取文件,通过消息队列发送每个块,然后在到达文件末尾时向接收方发送一个空消息。
xqkwcwgp

xqkwcwgp2#

#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;
}

相关问题