-
Notifications
You must be signed in to change notification settings - Fork 0
/
posix_shm_malloc_stats.c
196 lines (163 loc) · 6.2 KB
/
posix_shm_malloc_stats.c
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Copied from https://www.softprayog.in/programming/interprocess-communication-using-posix-shared-memory-in-linux
* Copyright © 2007-2017 SoftPrayog.in. All Rights Reserved.
*
* Suggestion to use malloc_stats() from www.linuxjournal.com/article/6390
*/
//input output commands
#include <stdio.h>
//contains malloc(...) and free(...)
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
//includes _exit for sudden exit
#include <unistd.h>
#include <semaphore.h>
#include <sys/mman.h>
//for malloc_stats() which writes to stderr
#include <malloc.h>
//for uint16_t, 32_t, 64_t declarations
#include <stdint.h>
// Buffer data structures
#define MAX_BUFFERS 10
//not really needed
//before I happened to stumble on this code, this file
//was used as log, to see the messages written by clients
//It was written with the usual open, write, close commands
#define LOGFILE "/tmp/example.log"
#define SEM_MUTEX_NAME "/sem-mutex"
#define SEM_BUFFER_COUNT_NAME "/sem-buffer-count"
#define SEM_SPOOL_SIGNAL_NAME "/sem-spool-signal"
#define SHARED_MEM_NAME "/posix-shared-mem-example"
#define VALUE_ALLOC_2k 2048
#define VALUE_ALLOC_4k 4096
#define VALUE_ALLOC_8k 8192
struct shared_memory {
char buf [MAX_BUFFERS] [256];
//hello
int buffer_index;
int buffer_print_index;
};
//printing an error via perror to stderr
//AND exit with status 1 (what a waste of info)
void error (char *msg);
int main (int argc, char **argv)
{
struct shared_memory *shared_mem_ptr;
// hello, i do not need those, but I have to know what they do
sem_t *mutex_sem, *buffer_count_sem, *spool_signal_sem;
//as i said
int fd_shm;
char mybuf [256];
void *dyn_mem;
printf("no alloc:\n--------\n");
malloc_stats();
getchar();
// Get shared memory
if ((fd_shm = shm_open (SHARED_MEM_NAME, O_RDWR | O_CREAT, 0660)) == -1)
error ("shm_open");
// Setting the size for the shared memory
if (ftruncate (fd_shm, sizeof (struct shared_memory)) == -1)
error ("ftruncate");
// mapping the shared memory to process memory
if ((shared_mem_ptr = mmap (NULL, sizeof (struct shared_memory), PROT_READ | PROT_WRITE, MAP_SHARED,
fd_shm, 0)) == MAP_FAILED)
error ("mmap");
//looking at malloc
printf("\nno alloc->shmem allocated:\n--------\n");
printf("size of (struct shared_memory): %ld\n--------\n",
sizeof(struct shared_memory) );
malloc_stats();
getchar();
//using an actual malloc
dyn_mem = malloc( VALUE_ALLOC_4k );
//looking at malloc
printf("\nno alloc->shmem allocated->malloc:\n--------\n");
malloc_stats();
getchar();
// mutual exclusion semaphore, mutex_sem with an initial value 0.
if ((mutex_sem = sem_open (SEM_MUTEX_NAME, O_CREAT, 0660, 0)) == SEM_FAILED)
error ("sem_open");
// Initialize the shared memory, I do not need that in this code
//shared_mem_ptr -> buffer_index = shared_mem_ptr -> buffer_print_index = 0;
// counting semaphore, indicating the number of available buffers. Initial value = MAX_BUFFERS
if ((buffer_count_sem = sem_open (SEM_BUFFER_COUNT_NAME, O_CREAT, 0660,
MAX_BUFFERS)) == SEM_FAILED)
error ("sem_open");
// counting semaphore, indicating the number of strings to be printed. Initial value = 0
if ((spool_signal_sem = sem_open (SEM_SPOOL_SIGNAL_NAME, O_CREAT, 0660,
0)) == SEM_FAILED)
error ("sem_open");
//looking at malloc
printf("\nmore variables looked at:\n--------\n");
malloc_stats();
getchar();
//I do not need that
// Initialization complete; now we can set mutex semaphore as 1 to
// indicate shared memory segment is available
//if (sem_post (mutex_sem) == -1)
//error ("sem_post: mutex_sem");
//no action needed
/*
while (1) { // forever
// Is there a string to print? P (spool_signal_sem);
if (sem_wait (spool_signal_sem) == -1)
error ("sem_wait: spool_signal_sem");
strcpy (mybuf, shared_mem_ptr -> buf [shared_mem_ptr -> buffer_print_index]);
//* /* Since there is only one process (the logger) using the
// buffer_print_index, mutex semaphore is not necessary *//*
(shared_mem_ptr -> buffer_print_index)++;
if (shared_mem_ptr -> buffer_print_index == MAX_BUFFERS)
shared_mem_ptr -> buffer_print_index = 0;
// /* /* Contents of one buffer has been printed.
One more buffer is available for use by producers.
// Release buffer: V (buffer_count_sem); *//*
if (sem_post (buffer_count_sem) == -1)
error ("sem_post: buffer_count_sem");
}
//*/
/*since I prefer to clean my system ressources*/
if ( munmap ( shared_mem_ptr, sizeof (struct shared_memory) ) == -1 )
error ("munmap");
//looking at malloc
printf("\nno alloc->shmem allocated->malloc->munmap:\n--------\n");
malloc_stats();
getchar();
if ( close(fd_shm) == -1 )
error ("close(shm)");
if ( shm_unlink(SHARED_MEM_NAME) == -1 )
error ("shm_unlink");
if ( sem_close(mutex_sem) == -1 )
error ("sem_close");
if ( sem_close(buffer_count_sem) == -1 )
error ("sem_close");
if ( sem_close(spool_signal_sem) == -1 )
error ("sem_close");
if ( sem_unlink( SEM_MUTEX_NAME ) == -1 ) //"/sem-mutex"
error ("sem_unlink");
if ( sem_unlink( SEM_BUFFER_COUNT_NAME ) == -1 ) //"/sem-buffer-count"
error ("sem_unlink");
if ( sem_unlink( SEM_SPOOL_SIGNAL_NAME ) == -1 ) //"/sem-spool-signal"
error ("sem_unlink");
//looking at malloc
printf("\nclearing the different types:\n--------\n");
malloc_stats();
getchar();
//freeing the dyn variable
printf("\n dyn_mem variable 0x%lux \n---\nfree\n---\n", (uint64_t) dyn_mem);
free(dyn_mem);
//out of curiosity
printf("\n dyn_mem variable 0x%lux \n", (uint64_t) dyn_mem);
//looking at malloc
printf("\nno alloc->shmem allocated->malloc->munmap->free previous malloc:\n--------\n");
malloc_stats();
getchar();
} //I realize there is not exit, sorry for that dear Linux prof
// Print system error and exit
void error (char *msg)
{
perror (msg);
exit (1);
}