QNX系统-关于delay函数与sleep函数的区别

本文详细解析了QNX系统中delay与sleep函数的区别及应用。delay()为循环等待,占用处理器;sleep()则会挂起进程并释放处理器。文章还介绍了usleep()函数的功能,并提供了代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

QNX是类unix系统。
在c语言编程过程中,往往会用到delay或者sleep延时函数。两者之间在使用上有一定的区别!!!
delay()是循环等待,该进程还在运行,占用处理器。   
sleep()不同,它会被挂起,把处理器让给其他的进程。

sleep()参数指定暂停时间,单位是s   
delay()参数指定暂停时间,单位是ms
usleep功能:
暂停执行。 语法: void usleep(int micro_seconds); 返回值: 无 函数种类: PHP 系统功能 内容说明:本函数可暂时使程序停止执行。参数 micro_seconds 为要暂停的毫秒数(微妙还是毫秒?)。 注意:这个函数不能工作在 Windows 操作系统中。参见:usleep() 与sleep()类似,用于延迟挂起进程。进程被挂起放到reday queue。
  只是一般情况下,延迟时间数量级是秒的时候,尽可能使用sleep()函数。
  且,此函数已被废除,可使用nanosleep。
  如果延迟时间为几十毫秒,或者更小,尽可能使用usleep()函数。这样才能最佳的利用CPU时间

delay:
函数名: delay 
  功 能: 将程序的执行暂停一段时间(毫秒) 
  用 法: void delay(unsigned milliseconds); 
  程序例: 
  /* Emits a 440-Hz tone for 500 milliseconds */ 
  #include<dos.h> 
  int main(void) 
  { 
  sound(440); 
  delay(500); 
  nosound(); 
  return 0; 
  }



附:QNX下相关解释

delay()

Suspends a calling thread for a given length of time

Synopsis:

#include <unistd.h>

unsigned int delay( unsigned int duration );

Arguments:

duration
The number of milliseconds for which to suspend the calling thread from execution.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The delay() function suspends the calling thread for duration milliseconds. 

 

 

 

 

 

 

 

sleep()

Suspend a thread for a given length of time

Synopsis:

#include <unistd.h>

unsigned int sleep( unsigned int seconds );

Arguments:

seconds
The number of realtime seconds that you want to suspend the thread for.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The sleep() function suspends the calling thread until the number of realtime seconds specified by the seconds argument have elapsed, or the thread receives a signal whose action is either to terminate the process or to call a signal handler. The suspension time may be greater than the requested amount, due to the nature of time measurement (see the Tick, Tock: Understanding the Neutrino Microkernel's Concept of Time chapter of the QNX Neutrino Programmer's Guide), or due to the scheduling of other, higher priority threads by the system.

Returns:

0 if the full time specified was completed; otherwise, the number of seconds unslept if interrupted by a signal.

Examples:

/*
 * The following program sleeps for the
 * number of seconds specified in argv[1].
 */
#include <stdlib.h>
#include <unistd.h>

int main( int argc, char **argv )
{
    unsigned seconds;

    seconds = (unsigned) strtol( argv[1], NULL, 0 );
    sleep( seconds );
    
    return EXIT_SUCCESS;
}

转载于:https://www.cnblogs.com/splovecyk/p/4386146.html

<think>嗯,用户想了解QNX系统中fread函数的使用方法,包括文档和示例。首先,我需要回忆一下QNX系统的特点。QNX是一个实时操作系统,资源管理器是其核心机制之一,负责处理文件、设备等资源。根据引用3,资源管理器的编写涉及MsgReply等函数,这可能I/O操作相关。 接下来,关于fread函数,它是C标准库中的一个函数,用于从文件流中读取数据。在类UNIX系统中,系统调用通常通过C库函数封装,如引用1提到的fseek调用系统调用。因此,QNX中的fread应该也是类似的机制,最终会通过资源管理器访问文件系统。 需要确认QNX的fread使用方式是否标准C一致。标准用法是: ```c size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); ``` 参数包括数据缓冲区、元素大小、数量、文件流。返回值是成功读取的元素数。 然后,用户可能需要示例代码。比如打开文件、读取、关闭的流程。注意QNX可能对文件路径有特定要求,比如设备路径可能需要通过资源管理器访问。 另外,要考虑错误处理,比如检查fopen是否返回NULL,fread的返回值是否小于预期,这可能表示文件结束或错误。ferror和feof函数可以用来判断。 用户还可能关心线程安全,QNX作为实时系统,多任务环境下fread是否安全。通常C库函数QNX中是线程安全的,但需要确认。 还有性能方面,频繁的小数据读取可能效率低,建议使用合适的缓冲区大小。引用2提到FreeType处理字体渲染,虽不直接相关,但资源管理器的效率可能影响I/O性能。 最后,需要提醒用户参考QNX官方文档,因为不同版本可能有差异,特别是在资源管理器的实现细节上,如引用3提到的_RESMGR_NOREPLY,可能影响底层I/O处理,但fread作为高层函数应屏蔽这些细节。 总结回答结构:先介绍fread在QNX中的定位,然后给出函数原型,示例代码,注意事项,以及相关问题的建议。</think>### QNX系统中fread函数使用说明 #### 1. 函数定位原理 在QNX系统中,$fread()$是C标准库提供的文件读取函数,通过资源管理器(Resource Manager)实现对文件或设备的访问[^3]。其底层会调用QNX系统调用(如read_block()),但开发者无需直接操作资源管理器即可使用标准文件I/O函数[^1]。 #### 2. 函数原型 ```c #include <stdio.h> size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); ``` - **ptr**: 数据存储缓冲区指针 - **size**: 单个元素字节大小 - **nmemb**: 要读取的元素数量 - **stream**: 已打开的文件流指针 - **返回值**: 成功读取的完整元素数量(可能小于请求值) #### 3. 使用示例 ```c #include <stdio.h> int main() { FILE *fp = fopen("/tmp/sample.bin", "rb"); // 注意QNX路径格式 if (!fp) { perror("文件打开失败"); return 1; } char buffer[1024]; size_t elements_read = fread(buffer, sizeof(char), sizeof(buffer), fp); if (elements_read < sizeof(buffer)) { if (feof(fp)) printf("已到达文件末尾\n"); if (ferror(fp)) printf("读取错误发生\n"); } fclose(fp); return 0; } ``` #### 4. 关键注意事项 1. **资源路径规范**: - 设备文件路径遵循QNX命名规范,如访问串口:`/dev/ser1` - 普通文件路径需符合挂载点规则,如使用FTP服务器挂载路径:`/net/ftp/pub/file.txt` 2. **实时性处理**: ```c // 设置非阻塞模式 int fd = fileno(fp); fcntl(fd, F_SETFL, O_NONBLOCK); ``` 3. **线程安全**: - 在多线程环境中使用时,建议通过`flockfile(fp)`/`funlockfile(fp)`显式控制锁 4. **性能优化**: - 建议缓冲区大小设为4096字节(QNX常见块大小) - 大批量读取时使用`setvbuf()`调整缓冲区策略 #### 5. 相关文档参考 - QNX官方C库手册:`fread()`条目 -QNX系统编程手册》第6章"File I/O" - 资源管理器开发指南(涉及底层实现细节)[^3]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值