将结构指针元素传递给 FIFO - C
Pass struct pointer element to FIFO - C
我正在用 C 语言开发一个 client/server 应用程序,其中多个客户端通过一个公共 FIFO 发送请求。
发送到服务器的数据是一个包含多个参数的结构,包括指向整数数组 (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 发送数据,但是,在服务器端,指针指向到random/invalid地址(可以理解)。
我可以通过使 pref_seat_list 成为一个固定大小的数组来解决这个问题
int pref_seat_list[size]
,但它必须表现为在客户端初始化和分配的动态大小数组。
是否有解决方法可以让指针以某种方式通过 FIFO 而不必固定其大小?
-------------------------------------------- ---- 修复 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
大纲解决方案
您需要将首选座位列表与主结构分开处理。使用来自 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
个首选座位的请求分配了 space:
struct request *rp = malloc(sizeof(*rp) + num_prefs * sizeof(rp->pref_seat_list[0]));
请注意,sizeof(struct request)
(又名 sizeof(*rp)
)不包括数组的大小(尽管它可能包含一些填充,如果 FAM 不存在则不会存在,尽管那是这里不是问题)。
检查分配成功后,用所需信息填充结构和数组(将num_prefs
复制到rp->pref_seats_size
)。然后你可以一次写完:
fwrite(rp, sizeof(*rp) + rp->pref_seats_size * sizeof(rp->pref_seat_list[0])), 1, fp);
现在,阅读代码如何处理这个问题?它不知道要分配多大的space直到它读取了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…
第一次读取得到的是固定大小的数据,这也告诉接收进程接下来会有多少变长数据。它分配正确的 space,然后将可变长度数据读入分配的结构。
请注意,这确实假定接收端的进程具有与发送进程相同的大小特征。由于您使用的是 FIFO,因此您的 I/O 在一台机器上,但理论上,如果发送进程是 64 位而接收进程是 32 位,则其他类型可能会出现问题,或者反之亦然——除非你处理的是 int
类型(假设 pid_t
是 int
或 unsigned
的变相),这可能都是 32 位的,而不管 32-位与 64 位问题。对于其他类型或更复杂的结构,或者如果您使用的是网络连接而不是本地连接,您将不得不更加努力地工作才能在所有情况下准确发送数据。
POC 代码
此代码在我的 SOQ (Stack Overflow Questions) repository on GitHub as files send29.c
, recv29.c
, dumpreq.c
and request.h
in the src/so-5030-9324 子目录中可用。
此代码使用我的标准错误报告功能,这些功能也可以在 GitHub 上的我的 SOQ 存储库中作为文件 stderr.c
和 stderr.h
在 src/libsoq 子-目录。
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
也可以)。
对此没有解决方法:您需要开发能够正确序列化和反序列化 struct
的代码。两端必须就正在交换的数据的独立于编译器的统一表示达成一致。
您不能简单地将 struct
发送到另一个进程,因为接收端可能有不同的内存对齐和大小要求。
序列化数据的过程分为三个步骤:
- 遍历
struct
中的每个指针,然后将其大小要求相加,计算出您将需要多少内存。在您的情况下,只有一个指针,大小为 p->pref_seats_size * sizeof(*p->pref_seat_list)
- 分配
char
的缓冲区以适应您的数据
- 再次遍历
struct
个成员,并将数据存储到缓冲区中。
一旦获得对面的缓冲区,分配struct
,遍历数据,并将数据写回struct
。
我正在用 C 语言开发一个 client/server 应用程序,其中多个客户端通过一个公共 FIFO 发送请求。
发送到服务器的数据是一个包含多个参数的结构,包括指向整数数组 (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 发送数据,但是,在服务器端,指针指向到random/invalid地址(可以理解)。
我可以通过使 pref_seat_list 成为一个固定大小的数组来解决这个问题
int pref_seat_list[size]
,但它必须表现为在客户端初始化和分配的动态大小数组。
是否有解决方法可以让指针以某种方式通过 FIFO 而不必固定其大小?
-------------------------------------------- ---- 修复 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
大纲解决方案
您需要将首选座位列表与主结构分开处理。使用来自 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
个首选座位的请求分配了 space:
struct request *rp = malloc(sizeof(*rp) + num_prefs * sizeof(rp->pref_seat_list[0]));
请注意,sizeof(struct request)
(又名 sizeof(*rp)
)不包括数组的大小(尽管它可能包含一些填充,如果 FAM 不存在则不会存在,尽管那是这里不是问题)。
检查分配成功后,用所需信息填充结构和数组(将num_prefs
复制到rp->pref_seats_size
)。然后你可以一次写完:
fwrite(rp, sizeof(*rp) + rp->pref_seats_size * sizeof(rp->pref_seat_list[0])), 1, fp);
现在,阅读代码如何处理这个问题?它不知道要分配多大的space直到它读取了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…
第一次读取得到的是固定大小的数据,这也告诉接收进程接下来会有多少变长数据。它分配正确的 space,然后将可变长度数据读入分配的结构。
请注意,这确实假定接收端的进程具有与发送进程相同的大小特征。由于您使用的是 FIFO,因此您的 I/O 在一台机器上,但理论上,如果发送进程是 64 位而接收进程是 32 位,则其他类型可能会出现问题,或者反之亦然——除非你处理的是 int
类型(假设 pid_t
是 int
或 unsigned
的变相),这可能都是 32 位的,而不管 32-位与 64 位问题。对于其他类型或更复杂的结构,或者如果您使用的是网络连接而不是本地连接,您将不得不更加努力地工作才能在所有情况下准确发送数据。
POC 代码
此代码在我的 SOQ (Stack Overflow Questions) repository on GitHub as files send29.c
, recv29.c
, dumpreq.c
and request.h
in the src/so-5030-9324 子目录中可用。
此代码使用我的标准错误报告功能,这些功能也可以在 GitHub 上的我的 SOQ 存储库中作为文件 stderr.c
和 stderr.h
在 src/libsoq 子-目录。
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
也可以)。
对此没有解决方法:您需要开发能够正确序列化和反序列化 struct
的代码。两端必须就正在交换的数据的独立于编译器的统一表示达成一致。
您不能简单地将 struct
发送到另一个进程,因为接收端可能有不同的内存对齐和大小要求。
序列化数据的过程分为三个步骤:
- 遍历
struct
中的每个指针,然后将其大小要求相加,计算出您将需要多少内存。在您的情况下,只有一个指针,大小为p->pref_seats_size * sizeof(*p->pref_seat_list)
- 分配
char
的缓冲区以适应您的数据 - 再次遍历
struct
个成员,并将数据存储到缓冲区中。
一旦获得对面的缓冲区,分配struct
,遍历数据,并将数据写回struct
。