linux单纯测试文件线程IO操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <errno.h>
#include <inttypes.h>
#include <libgen.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <unistd.h>
#include <pthread.h>
typedef int (*sock_callback_t)();
class BaseSo
{
public:
    BaseSo();
BaseSo(sock_callback_t fun):m_fun(fun){};
int run();
private:
    sock_callback_t m_fun;
};
BaseSo::BaseSo()
{
}
int BaseSo::run()
{
    m_fun();
}
 
 struct mystruct {    
            int i;    
            char cha;
            char shuzu[800000];
 };
 
static int callback()
{
  struct mystruct s;
  s.cha = 'A';
 pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
 
 pthread_mutex_lock(&mutex1);
 
 
 FILE *fp = fopen("test.txt","rb+"); 
 fseek(fp,0L,SEEK_SET);
 fread(&s,sizeof(s),1,fp);
 s.i = s.i + 1;
 fseek(fp,0L,SEEK_SET);
 fwrite(&s,sizeof(s),1,fp); 
 fclose(fp);
 
 pthread_mutex_unlock(&mutex1);
}
void *thread(void *ptr)
{
       BaseSo basesock(callback);
       basesock.run();
 
   return 0;
}
using namespace std;
int main()
{
  int i = 0;
  for(i =0; i<20000; i++)
  {
pthread_t id;
 int ret = pthread_create(&id, NULL, thread, NULL);
 pthread_join(id, NULL);
 
 
 
  }
   FILE *stream;    
    struct mystruct s;    
    if ((stream = fopen("test.txt", "r")) == NULL) /* open file TEST.$$$ */   
    {        
        fprintf(stderr, "Cannot open output file.\n");        
        return 1;    
    }    
 fseek(stream,0L,SEEK_SET);
    fread(&s, sizeof(s), 1, stream); /* 写的struct文件*/   
    fclose(stream); /*关闭文件*/   
    printf("%d",s.i);
    printf("\n%c",s.cha);
 
}

  

posted on   lydstory  阅读(284)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2019-04-08 sqlite.c sqlite.h
2019-04-08 ACE自适配通信环境
2019-04-08 服务程序 -st
2019-04-08 windows无法停止 服务 错误1053 服务没有及时响应
2019-04-08 ACE网络库
2019-04-08 xml配置sql语句
2019-04-08 竞品

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示