/*
*
* 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. Linux进程管理—信号、定时器

    信号: 1.       信号的作用: 背景: 进程之间通信比较麻烦. 但进程之间又必须通信,比如父子进程之间. 作用: 通知其他进程响应.进程之间的一种通信机制. 信号: 接受信号的进程马上停止,调 ...

  2. [Angular 2] DI in Angular 2 - 1

    Orgial aritial --> Link The problem with Angular 1 DI: Angular 2 DI: Solve the singletons problem ...

  3. iOS UIKit:animation

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css); @import url(/ ...

  4. Spring Mvc session拦截器实现

    Spring Mvc拦截器实现session过期跳转到登录页面 配置拦截器 <mvc:interceptors> <mvc:interceptor> <mvc:mappi ...

  5. Builder 生成器模式

    将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. 当同时满足以下情况的时候可以使用Builder模式: 当创建复杂对象的算法应该独立于该对象的组成部分以及他们的装配方式. 当 ...

  6. thinkphp 常见问题

    0.写在最前面的不断更新 (1)trace不起作用 A:必须要输出到模板,才会有trace信息 (2)提示“您浏览的页面暂时发生了错误!请稍后再试-” A:检查控制器(看看能进到控制器没有,设断点输出 ...

  7. SQL Server 游标

    结果集,结果集就是select查询之后返回的所有行数据的集合. 在关系数据库中,我们对于查询的思考是面向集合的.而游标打破了这一规则,游标使得我们思考方式变为逐行进行. 正常面向集合的思维方式是: 而 ...

  8. React+Immutable.js的心路历程

    这段时间做的项目开发中用的是React+Redux+ImmutableJs+Es6开发,总结了immutable.js的相关使用姿势: Immutable Data 顾名思义是指一旦被创造后,就不可以 ...

  9. php 计算一个字符串在另一个字符串中出现的次数

    <?php $text = 'This is a test'; echo strlen($text); // 14 echo substr_count($text, 'is'); // 2 // ...

  10. 在VS中关于MySQL的相关问题

    最近在vs上折腾mysql数据库 遇到了一些小问题,这里记录一下 问题一:数据源选择中没有mysql数据库的选项 解放方法: 1.安装MySql的VS插件(版本请下载最新版)mysql-for-vis ...