linux下怎样实现WaitForSingleObject的功能
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux下怎样实现WaitForSingleObject的功能相关的知识,希望对你有一定的参考价值。
windows的WaitForSingleObject这个接口超级混乱,等线程用 pthread_join,
等semphore用 sem_wait
等mutex用 pthread_mutex_lock
windows偏要做大而全, 让人很无奈. 参考技术A Windows中的WaitForSingleObject()函数对应在Linux中的sem_wait(),SetEvent对应sem_post(),
参考下面的Linux程序:
#include
#include
#include
#include
#include
#include
char tem[10]; //读写公共区
sem_t sem;
void* thread_fun(void*);
int main()
int counter=0;
pthread_t mythread;
sem_init(&sem,0,0);
pthread_create(&mythread,NULL,thread_fun,NULL);
while(counter<10) //往读写区里写10次'f'
tem[counter]='f';
counter++;
sem_post(&sem);
pthread_join(mythread,NULL); //等待子线程
sem_destroy(&sem);
exit(0);
void* thread_fun(void* arg) //子线程函数
int counter=0;
while(counter<10&&sem_wait(&sem)==0)
printf("%c",tem[counter]); //读出来显示
counter++;
//sem_wait(&sem);
pthread_exit(NULL);
怎样使用C语言列出某个目录下的文件?
用C语言列出目录下的文件,在linux下可采用readdir()函数来实现,代码实现过程为:
打开目录
循环读目录,输出目录下文件
关闭目录指针
参考代码:
#include <dirent.h>#include <stdio.h>
int main()
DIR *dirp;
struct dirent *dp;
dirp = opendir("."); //打开目录指针
while ((dp = readdir(dirp)) != NULL) //通过目录指针读目录
printf("%s\\n", dp->d_name );
(void) closedir(dirp); //关闭目录
return 0;
在windows下,代码如下:
#include <io.h>#include <stdio.h>
void printDir( const char* path )
struct _finddata_t data;
long hnd = _findfirst( path, &data ); // 查找文件名与正则表达式chRE的匹配第一个文件
if ( hnd < 0 )
perror( path );
int nRet = (hnd <0 ) ? -1 : 1;
while ( nRet >= 0 )
if ( data.attrib == _A_SUBDIR ) // 如果是目录
printf(" [%s]*\\n", data.name );
else
printf(" [%s]\\n", data.name );
nRet = _findnext( hnd, &data );
_findclose( hnd ); // 关闭当前句柄
void main()
printDir("d:/*.*");
相关函数说明:
long _findfirst( char *filespec, struct _finddata_t *fileinfo );// 功 能 : 提供与filespec指定入口泛式匹配的第一个文件.通常后继用_findnext()函数来完成某泛式下的文件遍历.
// 头文件 : #include <io.h>
// 参 数 : filespec - 目标文件规范,可以包含通配符
// fileinfo - 文件信息buffer
// 返回值 : 成功返回唯一的搜索句柄
// 出错返回-1,且设置errno为如下值:
// ENOENT 该泛式无法匹配
// EINVAL 无效文件名
int _findnext( long handle, struct _finddata_t *fileinfo );
// 功 能 : 按照前面_findfirst中的泛式规则,查找下一个符合该泛式的文件,并以此为依据修改fileinfo中的值
// 头文件 : #include <io.h>
// 参 数 : long handle - 搜索句柄(通常由紧靠其前的_findfirst()返回)
// fileinfo - 文件信息buffer
// 返回值 : 成功返回0
// 出错返回-1
int _findclose( long handle );
// 功 能 : 关闭搜寻句柄并释放相应资源
// 头文件 : #include <io.h>
// 参 数 : long handle - 搜索句柄(通常由紧靠其前的_findfirst()返回)
// 返回值 : 成功返回0
// 出错返回-1 参考技术A C语言本身没有提供象dir_list()这样的函数来列出某个目录下所有的文件。不过,利用C语言的几个目录函数,你可以自己编写一个dir_list()函数。首先,头文件dos.h定义了一个find_t结构,它可以描述DOS下的文件信息,包括文件名、时间、日期、大小和属性。其次,C编译程序库中有_dos_findfirst()和_dos_findnext()这样两个函数,利用它们可以找到某个目录下符合查找要求的第一个或下一个文件。dos_findfirst()函数有三个参数,第一个参数指明要查找的文件名,例如你可以用“*.*”指明要查找某个目录下的所有文件。第二个参数指明要查找的文件属性,例如你可以指明只查找隐含文件或子目录。第三个参数是指向一个find_t变量的指针,查找到的文件的有关信息将存放到该变量中。dos_findnext()函数在相应的目录中继续查找由_dos_findfirst()函数的第一个参数指明的文件。_dos_findnext()函数只有一个参数,它同样是指向一个find_t变量的指针,查找到刚文件的有关信息同样将存放到该变量中。利用上述两个函数和find_t结构,你就可以遍历磁盘上的某个目录,并列出该目录下所有的文件,请看下例:#include <stdio.h>#include <direct.h>#include <dos.h>#include <malloc.h>#include <memory.h>#include <string.h>typedef struct find_t FILE_BLOCKvoid main(void);void main(void)FILE_BLOCK f-block; /* Define the find_t structure variable * /int ret_code; / * Define a variable to store the return codes * // * Use the "*.*" file mask and the 0xFF attribute mask to listall files in the directory, including system files, hiddenfiles, and subdirectory names. * /ret_code = _dos_findfirst(" *. * ", 0xFF, &f_block);/* The _dos_findfirst() function returns a 0 when it is successfuland has found a valid filename in the directory. * /while (ret_code == 0)/* Print the file's name * /printf(" %-12s\n, f_block, name);/ * Use the -dos_findnext() function to look本回答被提问者采纳
以上是关于linux下怎样实现WaitForSingleObject的功能的主要内容,如果未能解决你的问题,请参考以下文章