我正在尝试使用命名管道在进程之间传递结构。我被困在试图打开管道非阻塞模式。这是我写入fifo的代码:
void writeUpdate() {
// Create fifo for writing updates:
strcpy(fifo_write, routing_table->routerName);
// Check if fifo exists:
if(access(fifo_write, F_OK) == -1 )
fd_write = mkfifo(fifo_write, 0777);
else if(access(fifo_write, F_OK) == 0) {
printf("writeUpdate: FIFO %s already exists\n", fifo_write);
//fd_write = open(fifo_write, O_WRONLY|O_NONBLOCK);
}
fd_write = open(fifo_write, O_WRONLY|O_NONBLOCK);
if(fd_write < 0)
perror("Create fifo error");
else {
int num_bytes = write(fd_write, routing_table, sizeof(routing_table));
if(num_bytes == 0)
printf("Nothing was written to FIFO %s\n", fifo_write);
printf("Wrote %d bytes. Sizeof struct: %d\n", num_bytes,sizeof(routing_table)+1);
}
close(fd_write);
}
routing_table 是一个指向我的结构的指针,它已被分配,所以不存在 fifo 的名称或类似的问题。如果我在没有 O_NONBLOCK 选项的情况下打开 fifo,它会第一次写入 smth,但随后它会阻塞,因为我也无法读取结构。并且在第一次之后,创建了初始fifo,但出现了其他fifo,命名为'.','..'。设置了 O_NONBLOCK 选项后,它会创建 fifo,但总是会抛出错误:“没有这样的设备或地址”。知道为什么会这样吗?谢谢。
编辑:好的,所以我现在很清楚打开 fifo,但我还有另一个问题,事实上,将结构读/写到 fifo 是我开始的问题。我读取结构的代码:
void readUpdate() {
struct rttable *updateData;
allocate();
strcpy(fifo_read, routing_table->table[0].router);
// Check if fifo exists:
if(access(fifo_read, F_OK) == -1 )
fd_read = mkfifo(fifo_read, 777);
else if(access(fifo_read, F_OK) == 0) {
printf("ReadUpdate: FIFO %s already exists\n Reading from %s\n", fifo_read, fifo_read);
}
fd_read = open(fifo_read, O_RDONLY|O_NONBLOCK);
int num_bytes = read(fd_read, updateData, sizeof(updateData));
close(fd_read);
if(num_bytes > 0) {
if(updateData == NULL)
printf("Read data is null: yes");
else
printf("Read from fifo: %s %d\n", updateData->routerName, num_bytes);
int result = unlink(fifo_read);
if(result < 0)
perror("Unlink fifo error\n");
else {
printf("Unlinking successful for fifo %s\n", fifo_read);
printf("Updating table..\n");
//update(updateData);
print_table_update(updateData);
}
} else
printf("Nothing was read from FIFO %s\n", fifo_read);
}
它打开fifo并尝试读取,但fifo中似乎没有任何内容,尽管在第一次writeUpdate中它说它写了4个字节(这似乎也是错误的)。在阅读时,第一次打印'a'然后num_bytes总是<=0。我环顾四周,只发现这个例子,简单的写/读,写一个结构时还需要什么吗?
我的结构如下所示:
typedef struct distance_table {
char dest[20]; //destination network
char router[20]; // via router..
int distance;
} distance_table;
typedef struct rttable {
char routerName[10];
char networkName[20];
struct distance_table table[50];
int nrRouters;
} rttable;
struct rttable *routing_table;