open files
/*
*
* Copyright (c) International Business Machines Corp., 2001
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
* the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/ /*
* FILE : openfile.c
* DESCRIPTION : Create files and open simultaneously
* HISTORY:
* 03/21/2001 Paul Larson (plars@us.ibm.com)
* -Ported
* 11/01/2001 Mnaoj Iyer (manjo@austin.ibm.com)
* - Modified.
* added #inclide <unistd.h>
*
*/ #include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h> #include "test.h"
#include "usctest.h" char *TCID = "openfile01"; /* Test program identifier. */
int TST_TOTAL = ; #define MAXFILES 32768
#define MAXTHREADS 10 /* Control Structure */
struct cb {
pthread_mutex_t m;
pthread_cond_t init_cv;
pthread_cond_t thr_cv;
int thr_sleeping;
} c; /* Global Variables */
int numthreads = , numfiles = ;
int debug = ;
char *filename = "FILETOOPEN"; void setup(void)
{
tst_tmpdir();
} void cleanup(void)
{
tst_rmdir(); } /* Procedures */
void *threads(void *thread_id); /* **************************************************************************
* MAIN PROCEDURE *
************************************************************************** */ int main(int argc, char *argv[])
{
int i, opt, badopts = ;
FILE *fd;
pthread_t th_id;
char msg[] = "";
extern char *optarg; while ((opt = getopt(argc, argv, "df:t:h")) != EOF) {
switch ((char)opt) {
case 'd':
debug = ;
break;
case 'f':
numfiles = atoi(optarg);
if (numfiles <= )
badopts = ;
break;
case 't':
numthreads = atoi(optarg);
if (numthreads <= )
badopts = ;
break;
case 'h':
default:
printf("Usage: openfile [-d] -f FILES -t THREADS\n");
_exit();
}
}
if (badopts) {
printf("Usage: openfile [-d] -f FILES -t THREADS\n");
_exit();
} setup(); /* Check if numthreads is less than MAXFILES */
if (numfiles > MAXFILES) {
sprintf(msg, "%s\nCannot use %d files", msg, numfiles);
sprintf(msg, "%s, used %d files instead\n", msg, MAXFILES);
numfiles = MAXFILES;
} /* Check if numthreads is less than MAXTHREADS */
if (numthreads > MAXTHREADS) {
sprintf(msg, "%s\nCannot use %d threads", msg, numthreads);
sprintf(msg, "%s, used %d threads instead\n", msg, MAXTHREADS);
numthreads = MAXTHREADS;
} /* Create files */
if ((fd = fopen(filename, "w")) == NULL) {
tst_resm(TFAIL, "Could not create file");
cleanup();
} /* Initialize thread control variables, lock & condition */
pthread_mutex_init(&c.m, NULL);
pthread_cond_init(&c.init_cv, NULL);
pthread_cond_init(&c.thr_cv, NULL);
c.thr_sleeping = ; /* Grab mutex lock */
if (pthread_mutex_lock(&c.m)) {
tst_resm(TFAIL, "failed to grab mutex lock");
fclose(fd);
unlink(filename);
cleanup();
} printf("Creating Reading Threads\n"); /* Create threads */
for (i = ; i < numthreads; i++)
if (pthread_create(&th_id, (pthread_attr_t *) NULL, threads,
(void *)(uintptr_t) i)) {
tst_resm(TFAIL,
"failed creating a pthread; increase limits");
fclose(fd);
unlink(filename);
cleanup();
} /* Sleep until all threads are created */
while (c.thr_sleeping != numthreads)
if (pthread_cond_wait(&c.init_cv, &c.m)) {
tst_resm(TFAIL,
"error while waiting for reading threads");
fclose(fd);
unlink(filename);
cleanup();
} /* Wake up all threads */
if (pthread_cond_broadcast(&c.thr_cv)) {
tst_resm(TFAIL, "failed trying to wake up reading threads");
fclose(fd);
unlink(filename);
cleanup();
}
/* Release mutex lock */
if (pthread_mutex_unlock(&c.m)) {
tst_resm(TFAIL, "failed to release mutex lock");
fclose(fd);
unlink(filename);
cleanup();
} tst_resm(TPASS, "Threads are done reading"); fclose(fd);
unlink(filename);
cleanup();
_exit();
} /* **************************************************************************
* OTHER PROCEDURES *
************************************************************************** */ void close_files(FILE * fd_list[], int len)
{
int i;
for (i = ; i < len; i++) {
fclose(fd_list[i]);
}
} /* threads: Each thread opens the files specified */
void *threads(void *thread_id_)
{
int thread_id = (uintptr_t) thread_id_;
char errmsg[];
FILE *fd_list[MAXFILES];
int i; /* Open files */
for (i = ; i < numfiles; i++) {
if (debug)
printf("Thread %d : Opening file number %d \n",
thread_id, i);
if ((fd_list[i] = fopen(filename, "rw")) == NULL) {
sprintf(errmsg, "FAIL - Couldn't open file #%d", i);
perror(errmsg);
if (i > ) {
close_files(fd_list, i - );
}
unlink(filename);
pthread_exit((void *));
}
} /* Grab mutex lock */
if (pthread_mutex_lock(&c.m)) {
perror("FAIL - failed to grab mutex lock");
close_files(fd_list, numfiles);
unlink(filename);
pthread_exit((void *));
} /* Check if you should wake up main thread */
if (++c.thr_sleeping == numthreads)
if (pthread_cond_signal(&c.init_cv)) {
perror("FAIL - failed to signal main thread");
close_files(fd_list, numfiles);
unlink(filename);
pthread_exit((void *));
} /* Sleep until woken up */
if (pthread_cond_wait(&c.thr_cv, &c.m)) {
perror("FAIL - failed to wake up correctly");
close_files(fd_list, numfiles);
unlink(filename);
pthread_exit((void *));
} /* Release mutex lock */
if (pthread_mutex_unlock(&c.m)) {
perror("FAIL - failed to release mutex lock");
close_files(fd_list, numfiles);
unlink(filename);
pthread_exit((void *));
} /* Close file handles and exit */
close_files(fd_list, numfiles);
unlink(filename);
pthread_exit((void *));
}
open files的更多相关文章
- Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ...
Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ... 这个错误是因为有两个相 ...
- The type javax.ws.rs.core.MediaType cannot be resolved. It is indirectly referenced from required .class files
看到了http://stackoverflow.com/questions/5547162/eclipse-error-indirectly-referenced-from-required-clas ...
- 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\106f9ae8\cc0e1
在本地开发环境没问题,但是发布到服务器出现:未能写入输出文件"c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.Ne ...
- Find and delete duplicate files
作用:查找指定目录(一个或多个)及子目录下的所有重复文件,分组列出,并可手动选择或自动随机删除多余重复文件,每组重复文件仅保留一份.(支持文件名有空格,例如:"file name" ...
- Android Duplicate files copied in APK
今天调试 Android 应用遇到这么个问题: Duplicate files copied in APK META-INF/DEPENDENCIES File 1: httpmime-4.3.2.j ...
- Oracle客户端工具出现“Cannot access NLS data files or invalid environment specified”错误的解决办法
Oracle客户端工具出现"Cannot access NLS data files or invalid environment specified"错误的解决办法 方法一:参考 ...
- files list file for package 'xxx' is missing final newline
#!/usr/bin/python # 8th November, 2009 # update manager failed, giving me the error: # 'files list f ...
- Mac osx 安装PIL出现Some externally hosted files were ignored (use --allow-external PIL to allow).
出现这个问题Some externally hosted files were ignored (use --allow-external PIL to allow)的主要原因是PIL的一些依赖库还没 ...
- 【Linux】Too many open files
ZA 的BOSS 最近出现Too many open files 异常,这个异常一般是由于打开文件数过多引起, 最常见原因是某些连接一致未关闭 记录一些排查用到的指令 查看每个用户最大允许打开文件数量 ...
- [转]html5表单上传控件Files API
表单上传控件:<input type="file" />(IE9及以下不支持下面这些功能,其它浏览器最新版本均已支持.) 1.允许上传文件数量 允许选择多个文件:< ...
随机推荐
- Can't connect to MySQL server on localhost (10061)解决方法
出现这种错误的原因是由于MySQL的服务被关闭的原因,重新启动一下服务就可以了,启动服务的操作如下: 右键[计算机]-[管理]
- Java的RandomAccessFile
Java的RandomAccessFile提供对文件的读写功能,与普通的输入输出流不一样的是RamdomAccessFile可以任意的访问文件的任何地方.这就是“Random”的意义所在. Rando ...
- 兔子--R.java丢失原因及解决的方法
R.jar丢失原因: a:eclipse指向的adk路径有中文,或者是workspace路径有中文 b:xml文件里有错误或者引用的资源不存在 c:xml或者drawable下资源文件不能够有大写字母 ...
- python如何使用 os.path.exists()--Learning from stackoverflow 分类: python 2015-04-23 20:48 139人阅读 评论(0) 收藏
Q&A参考连接 Problem:IOError: [Errno 2] No such file or directory. os.path.exists() 如果目录不存在,会返回一个0值. ...
- ios6如何处理内存,分别为前警告后
这里有一篇文章.非常具体地说明了ios6前后是怎样处理内存警告的: 来自唐巧的技术博客:http://blog.devtang.com/blog/2013/05/18/goodbye-viewdidu ...
- careercup-栈与队列 3.4
3.4 在经典问题汉诺塔中,有3根柱子及N个不同大小的穿孔圆盘,盘子可以滑入任意一根柱子.一开始,所有盘子自底向上从大到小依次套在第一根柱子上(即每一个盘子只能放在更大的盘子上面).移动圆盘时有以下限 ...
- python学习笔记--Django入门三 Django 与数据库的交互:数据建模
把数据存取逻辑.业务逻辑和表现逻辑组合在一起的概念有时被称为软件架构的 Model-View-Controller (MVC)模式.在这个模式中, Model 代表数据存取层,View 代表的是系统中 ...
- NDK开发之字符串操作
在JNI中,Java字符串被当作一个引用来处理.这些引用类型并不像原生C字符串一样可以直接使用,JNI提供了Java字符串与C字符串之间转换的必要函数,因为Java字符串对象是不可变的(如果对这里有异 ...
- Div+css中ul ol li dl dt dd使用
ol 有序列表.<ol><li>……</li><li>……</li><li>……</li></ol>表现 ...
- 【bzoj1212】 [HNOI2004]L语言
题目描述 标点符号的出现晚于文字的出现,所以以前的语言都是没有标点的.现在你要处理的就是一段没有标点的文章. 一段文章T是由若干小写字母构成.一个单词W也是由若干小写字母构成.一个字典D是若干个单词的 ...