c-为什么我的fifo读取中间有奇怪字符的返回字符串



我正试图通过命名管道将日志记录语句传递给守护进程。这些语句包含时间戳、发送方和消息。消息是按预期创建的,但当我从管道的另一端读取它们时,它们有时会在字符串中间包含奇怪的字符,如Úèþ。我尝试将''附加到每个日志记录语句的末尾,但没有任何效果。以下是我创建管道的代码和阅读部分:

char * fdfifo = "./fifo/myfifo";
mkfifo(fdfifo, 0666);
int fd = open(fdfifo,O_RDONLY);
struct timeval timeout;
timeout.tv_sec = 5;
timeout.tv_usec = 0;
logger(getpid(), pthread_self(), "logger started");
while(1 && keepRunning){
fd_set fds;
FD_ZERO(&fds);
FD_SET(fd, &fds);
if(select(fd + 1, &fds, NULL, NULL, &timeout)<=0){
logger(getpid(), pthread_self(), "stopping logger");
close(fd);
exit(EXIT_SUCCESS);
break;
}
else{
pthread_mutex_lock(&lock2);
FILE *f = fopen("./log/log.log", "a");
char concat_str[200];
read(fd, concat_str, 200);
fprintf(f, "%sn", concat_str);
fclose(f);
pthread_mutex_unlock(&lock2);
}
}

这是我给管道写信的部分:

int startLoggerEnd(){
readfifo = "./fifo/myfifo";
mkfifo(readfifo, 0666);
writedesc = open(readfifo,O_WRONLY);
sleep(2);
return 0;
}
int logger(pid_t pid, pthread_t tid, char *message){
char tmp[100];
char buff[20];
char msg[200];
struct tm *sTm;
time_t now = time (0);
sTm = gmtime (&now);
strftime (buff, sizeof(buff), "%Y-%m-%d %H:%M:%S", sTm);
sprintf(msg,"%s %s %d %s %d %s %sn", get_timestamp(tmp), "process:", pid,
"thread:",(unsigned) tid, "||", message);
write(writedesc, msg, strlen(msg));
return 0;
}

日志文件末尾的几行:

13:29:41:729 process: 14736 thread: 127881216 || moi
13:29:41:729 process: 14736 thread: 127881216 || moi
13:29:41:729 process: 14736 thread: 127881216 || moi
13:29:41:729 process: 14736 thread: 127881216 || moi
13:29:41:729 process: 14736 thread: 127881216 || moi
13:29:41:729 process: 14736 thread: 127881216 || moi
13:29:41:729 process: 14736 thread: 127881216 || moi
13:29:41:729 process: 14736 thread: 127881216 || moi
13:29:41:729 process: 14736 thread: 127881216 || moi
13:29:41:729 process: 14736 thread: 127881216 || moi
13:29:41:729 process: 14736 thread: 127881216 || moi
13:29:41:729 process: 14736 thread: 127881216 || moi
13:29:41:729 process: 14736 thread: 127881216 || moi
13:29:41:729 process: 14736 thread: 127881216 || moi
13:29:41:729 process: 14736 thread: 127881216 || moi
13:29:41:729 process: 14736 thread: 127881216 || moi
13:29:41:729 process: 14736 thread: 127881216 || moi
13:29:41:729 process: 14736 thread: 127881216 || moi
13:29:41:729 process: 14736 thread: 127881216 || moi
13:29:41:729 process: 14736 thread: 127881216 || moi
process: 14736 thread: 127881216 || moi
13:29:41:729 process: 14736 thread: 127881216 || moi
13:29:

您当前正在显示一个已分配的内存片(str2(。

但是,您不能确保您请求的内存部分是空的。

它可能包含以前操作的字节,从而导致错误的显示。

确保不显示先前字节的正确方法是在打印之前清理内存。

char *str2 = calloc(strlen(msg) + 1, sizeof(char));
strcpy(str2, msg);
str2[strlen(msg)] = ''; // index start at 0, strlen start at 1
write(fd, str2, strlen(str2));
close(fd);

calloc将清理分配的内存,然后再将其返回给您。

此外,请注意,此代码存在严重的内存泄漏,您甚至无法释放已分配的str2。

最新更新