//msgq_send.c
#include <stdio.h>
#include <string.h>
#include <sys/msg.h>
#define MAXSIZE 256
int main(int argc, char *argv[])
{
if (argc <2)
{
printf("Error args\n"漏旦兆)
return -1
}
int msgid
msgid = msgget((key_t)2000, IPC_CREAT | 0644)
if (msgid == -1)
{
printf("msgget error\n")
return -1
}
if (msgsnd(msgid, (void *)argv[1], MAXSIZE, 0) == -1)
{
printf("msgsnd error\n")
return -1
}
return 0
}
//msgq_recv.c
#include <stdio.h>
#include <string.h>
#include <sys/msg.h>
#define MAXSIZE 256
int main(void)
{
int msgid
int msgsize
char buff[MAXSIZE]
msgid = msgget((key_t)2000, IPC_CREAT | 0644)
if (msgid == -1)
{
printf("msgget error\n")
return -1
}
msgsize = msgrcv(msgid, (void *)&buff, MAXSIZE, 0, 0)
if (msgsize == -1)
{
printf("返租迟拿msgrcv error\n")
return -1
}
printf("%s\n", buff)
return 0
}
//Makefile
TARGET := msgq_send msgq_recv
CC := gcc
CFLAGS := -Wall -g
all: msgq_send msgq_recv
msgq_send: msgq_send.o
$(CC) $(CFLAGS) $^ -o $@
msgq_recv: msgq_recv.o
$(CC) $(CFLAGS) $^ -o $@
clean:
rm -fr *.o $(TARGET)
.PHONY :clean
我提供的代野拍码如下,自己补充坦脊键main函数哈,希望能够帮到你:)//相关头文件:
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
发送方让巧send:
void fifo_pro()
{
char s[128]
int fd
FILE *fp
fp = fopen("./a.txt", "r")
mkfifo("/tmp/fifo.tst", 0644)
fd = open("/tmp/fifo.tst", O_WRONLY)
while(fgets(s, 127, fp) != NULL) {
write(fd, s, strlen(s))
//printf("%s",s)
}
close(fd)
fclose(fp)
unlink("/tmp/fifo.tst")
}
接收方get:
char s[128]
int fd = open("/tmp/fifo.tst", O_RDONLY)
int fd2 = open("./b.txt", O_WRONLY)
memset(s, 0, 128)
while(read(fd, s, 128) >0) {
printf("%s", s)
write(fd2, s, 128)
}
close(fd2)
close(fd)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)