-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemmove.c
More file actions
169 lines (146 loc) · 3.76 KB
/
memmove.c
File metadata and controls
169 lines (146 loc) · 3.76 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <semaphore.h>
#include "pthread.h"
#include "semaphore.h"
#include "unistd.h"
#define BUFF_SIZE 10*1024
#define SRC_STRING "KEY11\nKEY21\nKEY31\nKEY41\nKEY51\nKEY61\nKEY71\nKEY81\nKEY91\n"
#define SRC1 "1\n"
#define SRC2 "2000\n"
#define SRC3 "3000000\n"
#define SRC4 "4000000000\n"
#define MAX_KEY_LEN 10
sem_t sem;
sem_t sem_check;
typedef struct thread_info_st
{
char * param;
int index;
}THREAD_INFO_T;
const char* get_value_by_index(int num)
{
const char* value;
switch(num)
{
case 1:
value = SRC1;
break;
case 2:
value = SRC2;
break;
case 3:
value = SRC3;
break;
default:
value = SRC4;
break;
}
return value;
}
void memmove_test(void* arg, int num, char* str_key, char* str_next_key)
{
const char* value;
int change = num == 1 ? -9 : 3;
char* src = NULL;
char* dest = NULL;
value = get_value_by_index(num);
printf("debug %s", value);
dest = strstr(arg, str_next_key) +change;
src = strstr(arg, str_next_key);
memmove(dest, src, strlen(src));
dest = dest + strlen(dest);
memset(dest,0, 1024);
dest = strstr(arg, str_key) + strlen(str_key);
strncpy(dest, value, strlen(value));
return;
}
void check_memmove(char* str, int num, const char* str_key)
{
const char* value;
char* src = NULL;
value = get_value_by_index(num);
src = strstr(str, str_key) + strlen(str_key);
if (strncmp(src, value, strlen(value)) != 0 )
{
printf("conflict occured \n");
printf("expect key :%s, vlaue:%s", str_key, value);
printf("********************\n%s*****************\n", str);
exit(0);
}
}
void* mem_test(void* arg)
{
THREAD_INFO_T* thread_info = (THREAD_INFO_T*) arg;
int i = 1;
int index = 0;
char str_key[MAX_KEY_LEN];
char str_next_key[MAX_KEY_LEN];
if (thread_info == NULL)
{
perror("child thread:get thread info failed\n");
}
memset(str_key, 0, MAX_KEY_LEN);
memset(str_next_key, 0, MAX_KEY_LEN);
snprintf(str_key, MAX_KEY_LEN, "KEY%d", thread_info->index);
snprintf(str_next_key, MAX_KEY_LEN, "KEY%d", thread_info->index + 1);
while (1)
{
if(sem_wait(&sem) == 0)
{
i++;
i = i % 4;
printf("mem test %d\n", i);
memmove_test(thread_info->param, i, str_key, str_next_key);
printf("********************\n%s*****************\n", thread_info->param);
usleep(500);
check_memmove(thread_info->param,i, str_key);
}
}
return NULL;
}
int main(int argc, char *argv[])
{
char* str_test = NULL;
pthread_t pid;
int num = 0;
int i = 0;
THREAD_INFO_T * thread_info = NULL;
if (argc < 2)
{
perror("no param\n");
return -1;
}
sem_init(&sem, 0, 0);
sscanf(argv[1], "%d", &num);
str_test = malloc(BUFF_SIZE);
if (str_test == NULL)
{
printf("malloc failed\n");
return -1;
}
memset(str_test,0, BUFF_SIZE);
strncpy(str_test, SRC_STRING, strlen(SRC_STRING));
for (i = 0; i < num; i++)
{
thread_info = NULL;
thread_info = malloc(sizeof (struct thread_info_st));
if (thread_info == NULL)
{
perror("malloc thread info failed\n");
return -1;
}
thread_info->param = str_test;
thread_info->index = i + 1;
pthread_create(&pid, NULL, mem_test, thread_info);
}
while(1)
{
for (i = 0; i < num; i++)
{
sem_post(&sem);
}
sleep(1);
}
}