/*
*
* 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. C# 可访问一致性不一致

    出现原因,其中一个是返回参数的访问级别小于函数的访问级别, 也就是说当定义一个返回参数的方法的时候,如果返回参数的访问级别低于方法的访问级别就会出现这样的错误,这个是可以理解的,如果返回的参数不能被访 ...

  2. Java Stax操作XML简介

    使用stax操作xml 非常的简单,它的读取过程像是一个光标在移动.针对不同的节点做不同的处理. 先看一个基于光标的模型处理xml: public class StaxTest { @Test pub ...

  3. Linux下git的配置

    需求和环境 硬件:一台装有Ubuntu14.04的PC 软件:git-core.openssh-server.openssh-client Git的安装 sudo apt-get install gi ...

  4. springMvc解决json中文乱码

    springMvc解决json中文乱码 springMvc解决json中文乱码,springMvc中文乱码,spring中文乱码 >>>>>>>>> ...

  5. Python实战:美女图片下载器,海量图片任你下载

    Python应用现在如火如荼,应用范围很广.因其效率高开发迅速的优势,快速进入编程语言排行榜前几名.本系列文章致力于可以全面系统的介绍Python语言开发知识和相关知识总结.希望大家能够快速入门并学习 ...

  6. [转]JavaScript 的同源策略

    同源策略限制了一个源(origin)中加载文本或脚本与来自其它源(origin)中资源的交互方式. 同源定义 如果两个页面拥有相同的协议(protocol),端口(如果指定),和主机,那么这两个页面就 ...

  7. javascript 之原型理解

    最近一直在了解javascript原型的问题,也算是理解了一点,希望把我所理解的,用简单的例子和说明,让更多人清除的去理解javascript原型 1,原型 prototype 是一个什么东西 我们创 ...

  8. php 计算代码行数

    <?php header("Content-type:text/html;charset=utf-8"); // php 递归计算文件夹代码行数 function codeL ...

  9. DataGrid列的合并

    /// <summary> /// DataGrid列的合并 /// 注意:1.DataGrid在绑定的时候进行分组和排序,才能让相同的行放在一起 /// 2.方法应用的时机,应该在Dat ...

  10. Javascript基础学习(1)_类型、值和变量

    1.null和undefined ①概念上区别: null是一个特殊的对象,是“非对象”,使用typeof后是object对象 undefined用未定义的值表示更深层次的“空值”,它是变量的一种取值 ...