#include "shmfifo.h"
#include <unistd.h>
typedef struct Products{
int id;
char pro_name[10];
}Pro;
int main()
{
shmfifo_t* fifo = shmfifo_init(12345, 3, sizeof(Pro));
Pro p;
while( 1){
memset(&p, 0x00, sizeof(p));
shmfifo_get(fifo, &p);
printf("id:%d, 产品名:%s\n", p.id, p.pro_name);
sleep(1);
}
shmfifo_destroy(fifo);
}
put.c
#include "shmfifo.h"
typedef struct Product
{
int id;
char pro_name[10];
}Pro;
int main()
{
shmfifo_t *fifo = shmfifo_init(12345, 4, sizeof(Pro));
Pro p;
for (int i=0; i<20; ++i)
{
memset(&p, 0x00, sizeof(p));
sprintf(p.pro_name, "iphone%d", i);
p.id = i+1;
shmfifo_put(fifo, &p);
printf("put %d ok\n", i);
}
}
验证同步,当写进程结束,读数据进程便阻塞
验证互斥,进程1写第9个数据时,我另开启了一进程写数据,从右侧可见这两进程是交替进行写操作的
Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx