C语言 printf()在调用sleep()后不在pthread中打印

szqfcxe2  于 2023-04-19  发布在  其他
关注(0)|答案(1)|浏览(198)

sleep()在我的zedboard上运行petalinux的c项目中超过2秒是错误的。下面是代码。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(){
    printf("Main sleep\n");
    sleep(3);
    printf("Main wakeup\n");
    fflush(stdout);
    exit(0);
}

睡眠3秒,我没有看到wakeup print语句,程序也没有退出。我必须按回车一次或两次,然后程序退出而没有打印wakeup。如果我睡眠2秒,那么程序的行为与预期相同。
只能在运行petalinux的zedboard上复制。在我的笔记本电脑上编译和运行的相同代码没有这种奇怪的行为
我通过串行控制台连接到zedboard。
编辑:改了题,与多线程无关。增加了最简单的可复制的例子
编辑:我添加了以下内容

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

int main(){
    time_t t = time(NULL);
    struct tm tme = *localtime(&t);
    printf("Main sleep at %d seconds\n", tme.tm_sec);
    sleep(3);
    t = time(NULL);
    tme = *localtime(&t);
    printf("Main wakeup at %d seconds\n", tme.tm_sec);
    fflush(stdout);
    exit(0);
}

我这样运行程序:

root@zed:~# ./payload-software > a.txt

程序没有退出,直到我按下回车键,我等了20秒。我猫的文件,这里是结果:

root@zed:~# cat a.txt 
Main sleep at 35 seconds
Main wakeup at 38 seconds

程序像它应该的那样睡眠,但它没有退出,当我在没有文件重定向的情况下运行它时,它没有打印到标准输出。如果我将睡眠时间更改为2秒,它工作正常。
编辑:添加strace:

root@zed:~# strace payload-software
execve("/usr/bin/payload-software", ["payload-software"], 0xbe9f8dd0 /* 13 vars */) = 0
brk(NULL)                               = 0x4dd000
uname({sysname="Linux", nodename="zed", ...}) = 0
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=6146, ...}) = 0
mmap2(NULL, 6146, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb6f98000
close(3)                                = 0
openat(AT_FDCWD, "/lib/librt.so.1", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0(\0\1\0\0\0@\33\0\0004\0\0\0"..., 512) = 512
fstat64(3, {st_mode=S_IFREG|0755, st_size=22176, ...}) = 0
mmap2(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb6f96000
mmap2(NULL, 86248, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb6f5a000
mprotect(0xb6f5f000, 61440, PROT_NONE)  = 0
mmap2(0xb6f6e000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x4000) = 0xb6f6e000
close(3)                                = 0
openat(AT_FDCWD, "/lib/libpthread.so.0", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0(\0\1\0\0\0]V\0\0004\0\0\0"..., 512) = 512
fstat64(3, {st_mode=S_IFREG|0755, st_size=75704, ...}) = 0
mmap2(NULL, 147656, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb6f35000
mprotect(0xb6f47000, 61440, PROT_NONE)  = 0
mmap2(0xb6f56000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x11000) = 0xb6f56000
mmap2(0xb6f58000, 4296, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0xb6f58000
close(3)                                = 0
openat(AT_FDCWD, "/lib/tls/v7l/neon/vfp/libc.so.6", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat64("/lib/tls/v7l/neon/vfp", 0xbeab1178) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/tls/v7l/neon/libc.so.6", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat64("/lib/tls/v7l/neon", 0xbeab1178) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/tls/v7l/vfp/libc.so.6", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat64("/lib/tls/v7l/vfp", 0xbeab1178)  = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/tls/v7l/libc.so.6", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat64("/lib/tls/v7l", 0xbeab1178)      = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/tls/neon/vfp/libc.so.6", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat64("/lib/tls/neon/vfp", 0xbeab1178) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/tls/neon/libc.so.6", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat64("/lib/tls/neon", 0xbeab1178)     = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/tls/vfp/libc.so.6", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat64("/lib/tls/vfp", 0xbeab1178)      = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/tls/libc.so.6", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat64("/lib/tls", 0xbeab1178)          = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/v7l/neon/vfp/libc.so.6", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat64("/lib/v7l/neon/vfp", 0xbeab1178) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/v7l/neon/libc.so.6", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat64("/lib/v7l/neon", 0xbeab1178)     = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/v7l/vfp/libc.so.6", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat64("/lib/v7l/vfp", 0xbeab1178)      = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/v7l/libc.so.6", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat64("/lib/v7l", 0xbeab1178)          = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/neon/vfp/libc.so.6", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat64("/lib/neon/vfp", 0xbeab1178)     = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/neon/libc.so.6", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat64("/lib/neon", 0xbeab1178)         = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/vfp/libc.so.6", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat64("/lib/vfp", 0xbeab1178)          = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/libc.so.6", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0(\0\1\0\0\0\211\260\1\0004\0\0\0"..., 512) = 512
fstat64(3, {st_mode=S_IFREG|0755, st_size=931444, ...}) = 0
mmap2(NULL, 1001036, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb6e40000
mprotect(0xb6f20000, 61440, PROT_NONE)  = 0
mmap2(0xb6f2f000, 16384, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0xdf000) = 0xb6f2f000
mmap2(0xb6f33000, 5708, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0xb6f33000
close(3)                                = 0
set_tls(0xb6f973f0)                     = 0
mprotect(0xb6f2f000, 8192, PROT_READ)   = 0
mprotect(0xb6f56000, 4096, PROT_READ)   = 0
mprotect(0xb6f6e000, 4096, PROT_READ)   = 0
mprotect(0x4d4000, 4096, PROT_READ)     = 0
mprotect(0xb6f9a000, 4096, PROT_READ)   = 0
munmap(0xb6f98000, 6146)                = 0
set_tid_address(0xb6f96f18)             = 656
set_robust_list(0xb6f96f20, 12)         = 0
rt_sigaction(SIGRTMIN, {sa_handler=0xb6f3a20d, sa_mask=[], sa_flags=SA_RESTORER|SA_SIGINFO, sa_restorer=0xb6e6a291}, NULL, 8) = 0
rt_sigaction(SIGRT_1, {sa_handler=0xb6f3a291, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART|SA_SIGINFO, sa_restorer=0xb6e6a291}, NULL, 8) = 0
rt_sigprocmask(SIG_UNBLOCK, [RTMIN RT_1], NULL, 8) = 0
ugetrlimit(RLIMIT_STACK, {rlim_cur=8192*1024, rlim_max=RLIM_INFINITY}) = 0
clock_gettime64(CLOCK_REALTIME_COARSE, {tv_sec=1520599284, tv_nsec=439968873}) = 0
brk(NULL)                               = 0x4dd000
brk(0x4fe000)                           = 0x4fe000
openat(AT_FDCWD, "/etc/localtime", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
fstat64(1, {st_mode=S_IFCHR|0600, st_rdev=makedev(0xfd, 0), ...}) = 0
ioctl(1, TCGETS, {B115200 opost isig icanon echo ...}) = 0
write(1, "Main sleep at 24 seconds\n", 25Main sleep at 24 seconds
) = 25
clock_nanosleep_time64(CLOCK_REALTIME, 0, {tv_sec=3, tv_nsec=3761122102560686080},







0xbeab1bd0) = 0
clock_gettime64(CLOCK_REALTIME_COARSE, {tv_sec=1520599287, tv_nsec=449968632}) = 0
openat(AT_FDCWD, "/etc/localtime", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
write(1, "Main wakeup at 27 seconds\n", 26Main wakeup at 27 seconds
) = 26
exit_group(0)                           = ?
+++ exited with 0 +++

注意,我不得不再次重复按回车键以获得调用sleep()后的输出来打印。

dpiehjr4

dpiehjr41#

这个问题似乎是zedboard的串行控制台的问题。我注意到在启动时也发生了同样的问题,启动时的某个系统调用被阻塞了一段时间来设置一些东西,这导致后续的启动消息不会显示。控制台提示符也不会显示,直到我按下回车键,我在运行上面的代码时看到的完全相同的问题。
所以这是串行控制台的问题。在ssh上,这个问题不存在。我不确定它是在我的端(我使用的是screen)还是在zedboard端(它使用的是getty)。
不管怎么说,不是世界末日,谢谢大家的帮助。

相关问题