Chinaunix首页 | 论坛 | 博客
  • 博客访问: 957905
  • 博文数量: 173
  • 博客积分: 3436
  • 博客等级: 中校
  • 技术积分: 1886
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-07 09:29
文章分类

全部博文(173)

文章存档

2016年(6)

2015年(10)

2014年(14)

2013年(8)

2012年(36)

2011年(63)

2010年(19)

2009年(17)

分类: LINUX

2011-04-04 22:29:52

linux app中printf输出顺序
 
linux app中printf输出顺序有可能与程序中语句顺序不一致。
如在我的应用程序中:
 
 

A1 0.339000, B1 0.402561, B2 0.000101, B3 0.000000
++ fpga_me_open is called, p file = 0xc3b0ed80
++ LCD_mini_open is called, p file = 0xc3b0eb00
------------[ cut here ]------------
WARNING: at kernel/irq/manage.c:274 __enable_irq+0x60/0x90()
Unbalanced enable for IRQ 62
Modules linked in: lcd_mini fpga_me [last unloaded: lcd_mini]
Backtrace:
[<c00341cc>] (dump_backtrace+0x0/0x10c) from [<c03297d0>] (dump_stack+0x18/0x1c)
 r7:c03dfde8 r6:00000112 r5:c006fc10 r4:c3b0be48
[<c03297b8>] (dump_stack+0x0/0x1c) from [<c0048a80>] (warn_slowpath_common+0x4c/0x64)
[<c0048a34>] (warn_slowpath_common+0x0/0x64) from [<c0048ae4>] (warn_slowpath_fmt+0x30/0x38)
 r7:bf000d5c r6:0000003e r5:60000013 r4:c044e4bc
[<c0048ab4>] (warn_slowpath_fmt+0x0/0x38) from [<c006fc10>] (__enable_irq+0x60/0x90)
 r3:0000003e r2:c03dfe48
[<c006fbb0>] (__enable_irq+0x0/0x90) from [<c0070114>] (enable_irq+0x48/0x7c)
 r5:60000013 r4:c044e4bc
[<c00700cc>] (enable_irq+0x0/0x7c) from [<bf000600>] (fpga_me_ioctl+0x294/0x4a0 [fpga_me])
 r7:bf000d5c r6:00000000 r5:0000630b r4:c3afe3e0
[<bf00036c>] (fpga_me_ioctl+0x0/0x4a0 [fpga_me]) from [<c00a7a2c>] (vfs_ioctl+0x74/0x7c)
 r7:00000004 r6:00000000 r5:0000630b r4:c3b0ed80
[<c00a79b8>] (vfs_ioctl+0x0/0x7c) from [<c00a7ce8>] (do_vfs_ioctl+0x6c/0x5d4)
 r7:00000004 r6:0000630b r5:00000000 r4:00000000
[<c00a7c7c>] (do_vfs_ioctl+0x0/0x5d4) from [<c00a8290>] (sys_ioctl+0x40/0x68)
[<c00a8250>] (sys_ioctl+0x0/0x68) from [<c002fee0>] (ret_fast_syscall+0x0/0x28)
 r7:00000036 r6:00000000 r5:00000000 r4:00000000
---[ end trace fdde0938a9f4c70b ]---
TA 0.000002, TA0 0.000024, TB -0.002726, TB0 -0.047165

para_init_f_bin -- OK

在我的程序中 上述输出信息的红色部分本来是连着的。

为什么会有这样的效果。如果,printf是阻塞的话,在一个单进程的环境中,后面的语句也会先执行么?

不能明白的是,这个地方是单线程环境中,如果printf是打印到缓冲区的,那么放到缓冲区得数据为什么顺序会乱呢?

主程序如下:

//init para from default file or default value.

    para_init();

    ret = sem_init(&sem_com_ready, 0,0);
    ret = sem_init(&sem_fpga_ready, 0,0);
    ret = sem_init(&sem_com_data_ready, 0,0);

    ret = pthread_mutex_init(&(mutex_com_send),NULL);

    if(ret != 0)
    {
     ERR("sem_init failed\n");
    }

    ret = pthread_rwlockattr_init( &attr );
    if( ret < 0 )
        printf( "pthread_rwlockattr_init failed: %d\n", ret );

    ret = pthread_rwlockattr_setpshared( &attr, PTHREAD_PROCESS_SHARED );
    if( ret < 0 )
        printf( "pthread_rwlockattr_setpshared failed: %d\n", ret );

    ret = pthread_rwlock_init( &rwlock, &attr );
    if( ret < 0 )
        printf( "pthread_rwlock_init failed: %d\n", ret );


    pthread_create(&pid_fpga, NULL, thread_fpga, NULL);
    pthread_create(&pid_com_recv, NULL, thread_com_recv, NULL);
    pthread_create(&pid_com_data_process, NULL, thread_com_data_process, NULL);

    fd = open(DEV_FPGA_ME, O_RDWR);
    if (fd < 0)
    {
        printf("Error: cannot open device (Maybe not present in your board?) %s \n", DEV_FPGA_ME);
        return -1;
    }else
    {
        printf("open file success\n");
        g_fpga_me_fd = fd;
        sem_post(&sem_fpga_ready);

    }


其中:

para_init();会调用

    DBG("A1 %f, B1 %f, B2 %f, B3 %f \n",A1,B1,B2,B3);
    DBG("TA %f, TA0 %f, TB %f, TB0 %f \n",TA,TA0,TB,TB0);


而从打印输出看,这两句话,被隔开了。

 

阅读(1865) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~