我有一个用i编写的多线程linux应用程序在i.mx6上运行。我有25lc256 spi eeprom映射到文件系统。在驱动程序级别写入相对较慢,在那里做的不多。问题是文件函数阻塞其他线程的时间太长。我们似乎没有帮助,似乎我不得不做一些不同的事情,但我不清楚要改变什么。
从一个线程调用此函数的输出是
EEprom写入EEprom保存在522.000000 703.000000 705.000000 723.000000 662596.000000 1328858.000000
捕获-EPIPE snd_pcm_prepare
使用Capture -EPIPE snd_pcm_prepare来自线程,其中音频缓冲区由于被阻塞的线程而变坏,我想。
int SaveCurrentConfig(void) {//EEPROM
int r = 1;
struct timeval tvs, tv1, tv2, tv3, tv4, tv5, tv6;
gettimeofday(&tvs, NULL);
printf("EEprom write\n");
pthread_mutex_lock(&eepromconfigmutex);
{
char * ConfigXml = BuildXmlConfig();
FILE * WriteConfig = fopen(ConfigPath, "w");
if (WriteConfig == NULL) {
MyLog("Unable to open eeprom %s\n", strerror(errno));
r = 0;
goto badfinish;
}
gettimeofday(&tv1, NULL);
size_t len = strlen(ConfigXml);
unsigned short CRC = ComputeChecksum(ConfigXml, len);
fwrite((char*) &len, 1, sizeof (size_t), WriteConfig);
gettimeofday(&tv2, NULL);
fwrite((char*) &CRC, 1, 2, WriteConfig);
gettimeofday(&tv3, NULL);
fwrite(ConfigXml, 1, strlen(ConfigXml), WriteConfig);
gettimeofday(&tv4, NULL);
fseek(WriteConfig, ConfigOffset2, SEEK_SET);
fwrite((char*) &len, 1, sizeof (size_t), WriteConfig);
fwrite((char*) &CRC, 1, 2, WriteConfig);
fwrite(ConfigXml, 1, strlen(ConfigXml), WriteConfig);
gettimeofday(&tv5, NULL);
fclose(WriteConfig);
badfinish:
free(ConfigXml);
}
pthread_mutex_unlock(&eepromconfigmutex);
gettimeofday(&tv6, NULL);
double diff1 = time_diff(tvs, tv1);
double diff2 = time_diff(tvs, tv2);
double diff3 = time_diff(tvs, tv3);
double diff4 = time_diff(tvs, tv4);
double diff5 = time_diff(tvs, tv5);
double diff6 = time_diff(tvs, tv6);
printf("EEprom saved in %f %f %f %f %f %f\n", diff1, diff2, diff3, diff4, diff5, diff6);
return r;
}
答案 0 :(得分:1)
如果调用fclose()
的线程阻止其他线程长时间运行,则问题很可能是它在eeprom驱动程序代码中的内核模式中花费了大量时间,从而清除了挂起的写入。
你可以尝试一些事情:
PREEMPT
配置选项。这将允许线程在运行eeprom驱动程序代码时被抢占。fflush()
,更频繁地将写入刷新到eeprom(您可能还需要在基础文件描述符上调用fsync()
)。 / LI>
答案 1 :(得分:0)
问题出现在fclose()
中。 fclose()
,但默认情况下,在文件内容写入磁盘之前不会返回。我相信您可以O_NONBLOCK
从man 2 open
When possible, the file is opened in nonblocking mode. Neither
the open() nor any subsequent operations on the file descriptor
which is returned will cause the calling process to wait. For the
handling of FIFOs (named pipes), see also fifo(7). For a discus‐
sion of the effect of O_NONBLOCK in conjunction with mandatory
file locks and with file leases, see fcntl(2).
O_NONBLOCK或O_NDELAY
int fd = open(ConfigPath, O_WRONLY | O_NONBLOCK); FILE * WriteConfig = fdopen(fd, "w");
您可以尝试使用:
import pytest
from account_system.action import my_action
from account_system.models import MyUser
@pytest.mark.django_db
def test_ok_profile():
req = FakeRequest()
email = 'tester@example.com'
MyUser.objects.create_user(
email=email,
password="example",
)
# query MyUser table and return result
result = my_action.get_profile(email)
assert result == 'success'
答案 2 :(得分:0)
我会尝试
fflush( WriteConfig );
在你的
之前fclose(WriteConfig);