/*
*
* 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的更多相关文章

  1. 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 ... 这个错误是因为有两个相 ...

  2. 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 ...

  3. 未能写入输出文件“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 ...

  4. Find and delete duplicate files

    作用:查找指定目录(一个或多个)及子目录下的所有重复文件,分组列出,并可手动选择或自动随机删除多余重复文件,每组重复文件仅保留一份.(支持文件名有空格,例如:"file  name" ...

  5. Android Duplicate files copied in APK

    今天调试 Android 应用遇到这么个问题: Duplicate files copied in APK META-INF/DEPENDENCIES File 1: httpmime-4.3.2.j ...

  6. Oracle客户端工具出现“Cannot access NLS data files or invalid environment specified”错误的解决办法

    Oracle客户端工具出现"Cannot access NLS data files or invalid environment specified"错误的解决办法 方法一:参考 ...

  7. 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 ...

  8. 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的一些依赖库还没 ...

  9. 【Linux】Too many open files

    ZA 的BOSS 最近出现Too many open files 异常,这个异常一般是由于打开文件数过多引起, 最常见原因是某些连接一致未关闭 记录一些排查用到的指令 查看每个用户最大允许打开文件数量 ...

  10. [转]html5表单上传控件Files API

    表单上传控件:<input type="file" />(IE9及以下不支持下面这些功能,其它浏览器最新版本均已支持.) 1.允许上传文件数量 允许选择多个文件:< ...

随机推荐

  1. BMVC reading list

    'Combining Local and Global Cues for Closed Contour Extraction' Vida Movahedi, James Elder 'FRIF: Fa ...

  2. oracle17 视图

    oracle的视图 介绍 视图是一个虚拟表是一个表,其内容由查询定义,同真实的表一样,视图包含一系列带有名称的列和行数据.但是,视图并不在数据库中以存储的数据值集形式存在(视图不实际存在).行和列数据 ...

  3. Mysql参数详解

    1.配置参数 MySQL有两种途径途径了解其的配置参数,一个是MySQL交互模式下的命令SHOW  VARIABLES,一个使用mysqladmin variables 查询. MySQL的配置参数分 ...

  4. raft 分布式协议 -- mongodb

    http://www.mongoing.com/presentations/webinar-raft-consensus-in-mongodb#rd

  5. c# 格式化百分比

    代码示例: <%#Eval("total").ToString() == "0" ? "00.00%" : (Convert.ToDe ...

  6. js--小结⑤

    js中的for循环,while循环,do...while循环和C语言的一模一样 有几个问题要提醒一下的是 1.  null是对象,即object       undefined是undefined d ...

  7. 使用原生JS编写ajax操作XMLHttpRequst对象

    ajax其本质就是XMLHttpRequest,现在jquery调用异步的方法很方便,但是也不能忘记原生的JS去编写ajax; 需要注意的是,很多人在写的时候喜欢只用XMLHttpRequest对象r ...

  8. SQL几个有点偏的语句

    SQL语句是一种集合操作,就是批量操作,它的速度要比其他的语言快,所以在设计的时候很多的逻辑都会放在sql语句或者存储过程中来实现,这个是一种设计思想.但是今天我们来讨论另外一个话题.Sql页提供了丰 ...

  9. CALayer 认识

    一篇很好的讲述calayer的博客 http://www.cnblogs.com/kenshincui/p/3972100.html

  10. 菜鸟日记之JSP1

                             JSP全名为Java Server Pages,中文名叫java服务器页面,其根本是一个简化的Servlet设计,它 是由Sun Microsyste ...