将结构指针元素传递给FIFO - C

时间:2018-05-12 18:14:23

标签: c arrays pointers struct fifo

我正在用C开发一个客户端/服务器应用程序,其中多个客户端通过公共FIFO发送请求。

发送到服务器的数据是一个包含多个参数的结构,包括一个指向int数组的指针(pref_seat_list)。

struct request {
  pid_t pid;
  int num_wanted_seats;
  int *pref_seat_list; // <-- where the issue lies
  int pref_seats_size;
};

客户端通过FIFO发送数据,但在服务器端,指针指向到随机/无效地址(可理解)。 / p>

我可以通过将pref_seat_list设为固定大小的数组来解决问题

int pref_seat_list[size]

,但它必须表现为在客户端初始化和分配的动态大小数组。

是否有指针以某种方式通过FIFO而不必修复其大小?

----------------------------------------------- - FIX 1 ---------------------------------------------- ---------

  • 包括FAM概念

结构现在看起来像这个

struct request {
  pid_t pid;
  int num_wanted_seats;
  int pref_seats_size;
  int pref_seat_list[];
};

结构的初始化已经完成: //获取pref_seat_list的大小,以便初始化和分配数组

  // CLIENT.C
  int pref_seats_size = count_seats_list(arglist[3]);

  // Allocating space for request struct's pref_seat_list array (flexible array member)
  struct request *req = malloc(sizeof(struct request) + sizeof(int[pref_seats_size]));
  if (req == NULL)
  {
    fprintf(stderr, "Fatal: failed to allocate memory for the struct request");
    exit(0);
  }

填写结构字段

  // CLIENT.C
  // Getting client's PID
  req->pid = getpid();

  // Getting client's number of wanted seats
  char *end;
  req->num_wanted_seats = strtol(arglist[2], &end, 10);

  // Assigning size of the struct's pref_seat_list array
  req->pref_seats_size = pref_seats_size;

  // Assigning list of seats to struct's pref_seat_list dynamic array
  int i = 0;
  char *token = strtok(arglist[3], " ");
  while (token != NULL)
  {
    req->pref_seat_list[i++] = strtol(token, &end, 10);
    token = strtok(NULL, " ");
  }

预期输出(SERVER.C)

1, 2, 3, 5, 8, 10, 12

实际输出(SERVER.C) - 随机值

3250, 0, 0, 123131, 1, 345691, 1

2 个答案:

答案 0 :(得分:2)

大纲解决方案

您需要与主结构分开处理首选座位列表。使用C99及更高版本的flexible array member(FAM)可能是最简单的。 FAM必须是结构的最后一个成员。

struct request
{
    pid_t pid;
    int num_wanted_seats;
    int pref_seats_size;
    int pref_seat_list[];
};

您为具有num_prefs首选席位的请求分配空间:

struct request *rp = malloc(sizeof(*rp) + num_prefs * sizeof(rp->pref_seat_list[0]));

请注意,sizeof(struct request)(又名sizeof(*rp))不包含数组的大小(尽管它可能包含一些填充,如果FAM不存在则不会出现这种填充,尽管如此#39;这不是问题。)

在检查分配成功后,使用所需信息填写结构和数组(将num_prefs复制到rp->pref_seats_size)。然后,您可以使用以下命令一次性写入:

fwrite(rp, sizeof(*rp) + rp->pref_seats_size * sizeof(rp->pref_seat_list[0])), 1, fp);

现在,阅读代码如何处理这个问题?在读取struct record主要信息之前,它还不知道要分配多大的空间,所以你必须在樱桃中吃两口:

struct request req_hdr;   // No space for the FAM
struct request *rp;

if (fread(&req_hdr, sizeof(req_hdr), 1, fp) != 1)
    …EOF or other problems…
rp = malloc(sizeof(*rp) + req_hdr->pref_seats_size * sizeof(rp->pref_seat_list[0]));
// … error check allocation …
*rp = req_hdr;
if (fread(rp->pref_seat_list, sizeof(rp->pref_seat_list[0]) * rp->pref_seats_size, 1, fp) != 1)
    …Protocol error…

第一次读取获取固定大小的数据,这也告诉接收过程将跟随多少可变长度数据。它分配正确的空间,然后将可变长度数据读入分配的结构。

请注意,这确实假设接收端的进程具有与发送进程相同的大小特征。由于您使用的是FIFO,因此您的I / O位于一台机器上,但理论上如果发送过程为64位且接收过程为32位,则可能存在其他类型的问题,或者反之亦然 - 除了你正在处理int类型(假设pid_t伪装成intunsigned),这可能都是32位而不管32位与64位问题。对于其他类型或更复杂的结构,或者如果您使用的是网络连接而不是本地连接,则必须更加努力地在所有情况下准确地发送数据。

POC代码

此代码在GitHub上的SOQ(Stack Overflow Questions)存储库中可用send29.crecv29.cdumpreq.crequest.h在{ {3}}子目录。

此代码使用我的标准错误报告功能,这些功能也可以在我的GitHub上的SOQ存储库中作为src/so-5030-9324子目录中的文件stderr.cstderr.h使用。

request.h

#ifndef REQUEST_H_INCLUDED
#define REQUEST_H_INCLUDED

#define FIFO_NAME "seat-request.fifo"

struct request
{
    int pid;
    int num_wanted_seats;
    int pref_seats_size;
    int pref_seat_list[];
};

extern void dump_request(const char *tag, const struct request *rp);

#endif /* REQUEST_H_INCLUDED */

send29.c

#include "request.h"
#include "stderr.h"     /* See https://github.com/jleffler/soq/tree/master/src/libsoq */
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>   /* mkfifo() */
#include <unistd.h>

int main(int argc, char **argv)
{
    if (argc > 0)               // Use argc - avoid unused argument warning
        err_setarg0(argv[0]);

    /* Maybe the other program already created it? */
    if (mkfifo(FIFO_NAME, 0666) != 0 && errno != EEXIST)
        err_syserr("failed to create FIFO %s: ", FIFO_NAME);

    FILE *fp = fopen(FIFO_NAME, "w");
    if (fp == NULL)
        err_syserr("failed to open FIFO %s for writing: ", FIFO_NAME);

    printf("Send: PID %d at work with FIFO %s open for writing\n", (int)getpid(), FIFO_NAME);

    struct request *rp = 0;
    int num_prefs = 10;
    size_t req_size = sizeof(*rp) + num_prefs * sizeof(rp->pref_seat_list[0]);
    rp = malloc(req_size);
    if (rp == 0)
        err_syserr("failed to allocate %zu bytes memory: ", req_size);

    rp->pid = getpid();
    rp->num_wanted_seats = 3;
    rp->pref_seats_size = num_prefs;
    for (int i = 0; i < num_prefs; i++)
        rp->pref_seat_list[i] = 123 + i;

    dump_request("Sender", rp);

    if (fwrite(rp, req_size, 1, fp) != 1)
        err_syserr("failed to write request (%zu bytes) to FIFO %s: ", req_size, FIFO_NAME);

    free(rp);
    fclose(fp);
    unlink(FIFO_NAME);
    printf("Send: PID %d finished writing %zu bytes to FIFO %s\n", (int)getpid(), req_size, FIFO_NAME);
    return 0;
}

recv29.c

#include "request.h"
#include "stderr.h"     /* See https://github.com/jleffler/soq/tree/master/src/libsoq */
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>   /* mkfifo() */
#include <unistd.h>

int main(int argc, char **argv)
{
    if (argc > 0)               // Use argc - avoid unused argument warning
        err_setarg0(argv[0]);

    /* Maybe the other program already created it? */
    if (mkfifo(FIFO_NAME, 0666) != 0 && errno != EEXIST)
        err_syserr("failed to create FIFO %s: ", FIFO_NAME);

    int fd = open(FIFO_NAME, O_RDONLY);
    if (fd < 0)
        err_syserr("failed to open FIFO %s for reading: ", FIFO_NAME);

    printf("Recv: PID %d at work with FIFO %s open for reading\n", (int)getpid(), FIFO_NAME);

    struct request req;
    struct request *rp = 0;
    if (read(fd, &req, sizeof(req)) != sizeof(req))
    {
        /* Marginally dubious error reporting; if the return value is
        ** positive but small, errno has no useful information in it.
        */
        err_syserr("failed to read %zu bytes for head from FIFO %s: ", sizeof(req), FIFO_NAME);
    }
    size_t req_size = sizeof(*rp) + req.pref_seats_size * sizeof(rp->pref_seat_list[0]);
    rp = malloc(req_size);
    if (rp == 0)
        err_syserr("failed to allocate %zu bytes memory: ", req_size);

    *rp = req;

    int nbytes = rp->pref_seats_size * sizeof(rp->pref_seat_list[0]);
    //if (read(fd, &rp->pref_seat_list[0], nbytes) != nbytes)
    if (read(fd, rp->pref_seat_list, nbytes) != nbytes)
        err_syserr("failed to read %d bytes for body from FIFO %s: ", nbytes, FIFO_NAME);

    dump_request("Receiver", rp);

    free(rp);
    close(fd);
    unlink(FIFO_NAME);
    printf("Recv: PID %d finished reading request from FIFO %s\n", (int)getpid(), FIFO_NAME);
    return 0;
}

dumpreq.c

#include "request.h"
#include <stdio.h>

void dump_request(const char *tag, const struct request *rp)
{
    printf("%s:\n", tag);
    printf("- PID requesting seats: %d\n", rp->pid);
    printf("- Number of seats wanted: %d\n", rp->num_wanted_seats);
    printf("- Number of seats in preferred list: %d\n", rp->pref_seats_size);
    for (int i = 0; i < rp->pref_seats_size; i++)
        printf("  %d is seat %d\n", i, rp->pref_seat_list[i]);
    fflush(stdout);
}

样品运行

$ send29 & recv29
[1] 55896
Send: PID 55896 at work with FIFO seat-request.fifo open for writing
Sender:
- PID requesting seats: 55896
- Number of seats wanted: 3
- Number of seats in preferred list: 10
  0 is seat 123
  1 is seat 124
  2 is seat 125
  3 is seat 126
  4 is seat 127
  5 is seat 128
  6 is seat 129
  7 is seat 130
  8 is seat 131
Recv: PID 55897 at work with FIFO seat-request.fifo open for reading
  9 is seat 132
Receiver:
- PID requesting seats: 55896
- Number of seats wanted: 3
- Number of seats in preferred list: 10
  0 is seat 123
  1 is seat 124
  2 is seat 125
  3 is seat 126
  4 is seat 127
  5 is seat 128
  6 is seat 129
  7 is seat 130
  8 is seat 131
  9 is seat 132
Send: PID 55896 finished writing 52 bytes to FIFO seat-request.fifo
Recv: PID 55897 finished reading request from FIFO seat-request.fifo
[1]+  Done                    send29
$

您可以按任意顺序运行程序(因此recv29 & send29也有效)。

答案 1 :(得分:2)

没有解决方法:您需要开发能够正确序列化和反序列化struct的代码。两端必须就正在交换的数据的统一编译器无关表示达成一致。

您不能简单地将struct发送到另一个进程,因为接收端可能具有不同的内存对齐和大小调整要求。

序列化数据的过程包括三个步骤:

  • 通过遍历struct中的每个指针,并计算其大小要求,计算出需要多少内存。在您的情况下,只有一个指针,大小为p->pref_seats_size * sizeof(*p->pref_seat_list)
  • 分配char s的缓冲区以适合您的数据
  • 再次遍历struct成员,并将数据存储到缓冲区中。

在对方获得缓冲区后,分配struct,遍历数据,然后将数据写回struct