malloc at Line A will consume more memory than Line B,
why?is it relevant to pthread?
int main()
{
char *buf = (char*)malloc(1024*1024*1024); //Line A
memset(buf,0,sizeof(1024*1024*1024));
pthread_t m_sockThreadHandle[8];
for (int i=0;i<8;i++)
{
if ( pthread_create(&m_sockThreadHandle[i], NULL, thread_run, NULL) != 0 )
{
perror("pthread_create");
}
}
sleep(10);
char *buf = (char*)malloc(1024*1024*1024);//Line B
memset(buf,0,sizeof(1024*1024*1024));
for (int i=0;i<8;i++)
{
pthread_join(m_sockThreadHandle[i],NULL);
}
}
Possibly because this isn't doing what you thought it was:
memset(buf,0,sizeof(1024*1024*1024));
sizeof(1024*1024*1024) is 4 on my compiler. I think you meant:
memset(buf,0, 1024*1024*1024);
From the code you post buf is unused, so it's not clear what you're trying to do, or why. But this at least is wrong....
Related
[Edited and added reprex] When I put the declarations below in the beginning of the main() function, the behavior of the threads is erratic and incorrect. When I make these declarations global, or not the first declarations in main(), everything works fine. I'm using mingw-w64 on Windows 10.
pthread_t thr1, thr2, thr3;
Below is the program, it spawns three threads to read integers from three different text files and add them all to a single global variable. The call to pthread_join() for thr3 always fails with error code 3, and the final result of sum is different in different runs. But it all works fine if the pthread_t declarations are in a different location. I hope this is short enough it's less than 70 lines including whitespace.
#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>
long sum = 0;
pthread_mutex_t sumLock = PTHREAD_MUTEX_INITIALIZER;
void* thrFunc(void* arg)
{
char curLine[50];
FILE *inFile = fopen((char*)arg, "r");
long curNum;
if (!inFile) {
printf("Error opening input file %s\n", arg);
pthread_exit((void*)1);
}
while (fgets(curLine, sizeof(curLine), inFile)) {
curNum = strtol(curLine, 0, 0);
pthread_mutex_lock(&sumLock);
sum += curNum;
pthread_mutex_unlock(&sumLock);
}
fclose(inFile);
pthread_exit((void*)0);
}
int main(void)
{
pthread_t thr1, thr2, thr3;
long threadResult;
int err;
if (pthread_create(&thr1, 0, thrFunc, (void*)"C:\\Users\\paulc\\long1.txt")) {
printf("Failed to create thread thr1\n");
exit(1);
}
if (pthread_create(&thr2, 0, thrFunc, (void*)"C:\\Users\\paulc\\long2.txt")) {
printf("Failed to create thread thr2\n");
exit(1);
}
if (pthread_create(&thr3, 0, thrFunc, (void*)"C:\\Users\\paulc\\long3.txt")) {
printf("Failed to create thread thr3\n");
exit(1);
}
if (err = pthread_join(thr1, (void**)&threadResult)) {
printf("failed to join thr1, error code = %d\n", err);
}
if (err = pthread_join(thr2, (void**)&threadResult)) {
printf("failed to join thr2, error code = %d\n", err);
}
if (err = pthread_join(thr3, (void**)&threadResult)) {
printf("failed to join thr3, error code = %d\n", err);
}
printf("Total: %ld\n", sum);
}
I'm trying to pass in a Professor struct into my professor function but I can't properly get the information stored in it to pass into the function. I suspect it has something to do with how I malloc'd p but I thought freeing after it's completed would solve that problem. I get a segfault when I try to print *professor->id, because apparently it decides to read p as memory location 0x0, even though it's not in main
typedef struct{
int *id;
int *assignings;
int *min_wait;
int *max_wait;
int *min_assignments;
int *max_assignments;
int *min_hrs;
int *max_hrs;
} Professor;
Professor* makeProfessor(){
Professor *professor = malloc(sizeof *professor);
return professor;
}
void * professorFunc(void *p){
Professor *professor = (Professor*)p;
fprintf(stdout,"Starting Professor %d\n", *professor->id);
pthread_exit(0);
}
int main(int argc, char **argv){
//Creating threads
pthread_t professor[num_professors];
Professor *p;
int i;
int id;
for(i = 0; i < num_professors; ++i){
id = i + 1;
p = malloc (sizeof *p);
p->id = &id;
if(pthread_create(&professor[i], NULL, professorFunc, (void*)p) != 0){
perror("pthread_create");
exit(1);
}
free(p);
}
for(i = 0; i < num_professors; ++i){
if(pthread_join(professor[i], NULL) != 0){
perror("pthread_join");
exit(1);
}
}
You are allocating the array of Professor structs, and immediately freeing them, likely before your thread has the chance to operate on them. A better way to implement this, would be to allocate the whole array, process them, and then free the memory, once you know the threads have exited (example below).
pthread_t professor[num_professors];
Professor *p;
int i;
int id;
p = malloc (sizeof(*p) * num_professors);
for(i = 0; i < num_professors; ++i){
id = i + 1;
p->id = &id;
if(pthread_create(&professor[i], NULL, professorFunc, (void*)p) != 0){
perror("pthread_create");
exit(1);
}
}
for(i = 0; i < num_professors; ++i){
if(pthread_join(professor[i], NULL) != 0){
perror("pthread_join");
exit(1);
}
}
free(p);
I load two strings with loadbuffer into one lua_state.
if( luaL_loadbuffer( L, str.c_str(), str.size(), "line") != 0 )
{
printf( "%s\n", lua_tostring ((lua_State *)L, -1));
}
lua_pcall(L, 0, 0, 0);
if( luaL_loadbuffer( L, str2.c_str(), str2.size(), "line2") != 0 )
{
printf( "%s\n", lua_tostring ((lua_State *)L, -1));
}
lua_pcall(L, 0, 0, 0);
For example:
function f ()
print( "Hello World!")
end
and
function g ()
f(
end
The forgotten ) in the second string throws an error:
[string "line2"]:9: unexpected Symbol
But 9 is the line number from string 1 plus string 2. The line number should be 3.
Is there a way to reset the line number counter before call to loadbuffer?
I guess this link describes your situation:
http://www.corsix.org/content/common-lua-pitfall-loading-code
You are loading two chunks of information, calling the chunks will put them consecutive into the global table. The lua_pcall(L, 0, 0, 0); is not calling your f() and g(), but is constructing your lua code sequential.
Your code could possibly be simplified to:
if (luaL_dostring(L, str.c_str()))
{
printf("%s\n", lua_tostring (L, -1));
}
if (luaL_dostring(L, str2.c_str()));
{
printf("%s\n", lua_tostring (L, -1));
}
which also protects against calling a chunk when it fails to load;
You are right Enigma, the code from str2 is appended consecutive. A breakpoint in
static void statement (LexState *ls) {
in lparser.cpp shows LexState.linenumber to be 5 and 7 for str, and 5, 7, 14 and 16 for str2.
So str is lexed and added to the VM twice.
I will find a different way to put a script made of multiple files into one VM.
Just if someone would need it too.
Add this function to lauxlib.h
LUALIB_API int (luaL_loadbuffers) (lua_State *L, size_t count, const char **buff, size_t *sz,
const char **name, const char *mode);
and to lauxlib.c
#include"lzio.h"
#include"ldo.h"
#include"ltable.h"
#include"lgc.h"
LUALIB_API int luaL_loadbuffers (lua_State *L, size_t count, const char **buff, size_t *sz,
const char **name, const char *mode)
{
ZIO z;
int status;
int i;
for( i=0; i<count; i++)
{
LoadS ls;
ls.s = buff[i];
ls.size = sz[i];
lua_lock(L);
luaZ_init(L, &z, getS, &ls);
status = luaD_protectedparser(L, &z, name[i], mode);
if (status == LUA_OK) { /* no errors? */
LClosure *f = clLvalue(L->top - 1); /* get newly created function */
if (f->nupvalues == 1) { /* does it have one upvalue? */
/* get global table from registry */
Table *reg = hvalue(&G(L)->l_registry);
const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
/* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
setobj(L, f->upvals[0]->v, gt);
luaC_barrier(L, f->upvals[0], gt);
} // == 1
lua_pcall( L, 0, 0, 0);
}
lua_unlock(L);
if( status != LUA_OK )
break;
}
return status;
}
Every string/file gets its own line numnbering.
It is just a copy, almost, of lua_load in lapi.c. So easy to adjust in a new release of LUA.
I want to get 2 programs to communicate, one (server) would store datas, and the other (client) would just access it.
I'll have to use a linked list to store datas because it won't stop storing, and then I was wondering if I could access to the whole linked list if only the first node is shared in memory.
What I mean is… are we allowed to access from the client program to the memory pointed by a shared pointer?
Sorry it seems obvious that we can not, so should I store my linked list into the shared memory, or do you think that would be awkward?
Because if I do so, I'll have to declare a shared memory for every node right?
So, to add shared memory to both programs I need the same keys, but I don't know how many keys there will be, and I can't just store it for both programs, unless I would have had already a linked list…
so I used a very very VERY awkward method that I don't even know if it works right, but I wish you can tell, which is to use ftok that is supposed to take an (url,pid) and return a key. So I assumed it would send the exact same key if I used the same url and pid, using a fake pid starting from 0 that I would increment for every element I add to the linked list… what do you think about it? Any other way to do it which would seem less… crap?
typedef struct s_shared_elem
{
char c;
struct s_shared_elem* next;
struct s_shared_elem* previous;
}shared_elem;
typedef struct s_shared_list
{
s_shared_elem* first;
s_shared_elem* last;
}shared_list;
int forthekey = 0;
char* url="/home/toor/Projet_cgi/";
shared_elem* shared_malloc(int pid, const char* url)
{
shared_elem* shm;
int shmid;
int key=ftok(url,pid);
if((shmid=shmget(key,1,IPC_CREAT | 0666)) < 0)
{
perror("shmget");
exit(1);
}
if ((shm = shmat(shmid,NULL,0)) == (shared_elem*)-1)
{
perror("shmat");
exit(1);
}
return shm;
}
void Init_shared_list(shared_list* liste)
{
liste->first = NULL;
liste->last = NULL;
}
void Add_elem(shared_list* liste)
{
shared_elem* new = shared_malloc(pid,url);
new->next = NULL;
new->previous = liste->last;
if(liste->first == NULL)
{
liste->first = new;
liste->last = new;
}
else
{
liste->last->next = new;
liste->last = new;
}
forthekey++;
}
void shared_free(shared_elem* todelete,int pid, const char* url)
{
shared_elem* shm;
int shmid;
int key=ftok(url,pid);
if((shmid=shmget(key,1,IPC_CREAT | 0666)) < 0)
{
perror("shmget");
exit(1);
}
shmdt(todelete);
shmctl(shmid,IPC_RMID,NULL);
forthekey--;
}
void Delete_list(shared_list* liste)
{
while(liste->last != liste->first)
{
shared_elem* tmp=liste->last;
liste->last=liste->last->previous;
Shared_free(tmp,pid,url);
}
Shared_free(liste->first,pid,url);
}
In share memory you can insert a whole linked list. It is useful in many cases. You do not need to create a linked list of share memory (e.g. using previous key, next key ). All you need to copy each node of linked list to the shared memory.
for example .....
process2.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
int main(int argc, char *argv[])
{
int shmid,i;
node *data;
if ((shmid = shmget(10, SHM_SIZE, 0644 | IPC_CREAT)) == -1) {
perror("shmget");
exit(1);
}
data = (node *)shmat(shmid, (void *)0, 0); // node is linked list
for(i=0;i<2;i++)
printf("%d\n",(data++)->item_code);
if (shmdt(data) == -1) {
perror("shmdt");
exit(1);
}
return 0;
}
process1.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
int main(int argc, char *argv[])
{
node *SELL=NULL; // node is linked list (structure) SELL is header
insert(&SELL,"Soap",1,12.5,10);
insert(&SELL,"Pen",2,20.75,8);
display(SELL);
int shmid,i;
node *data;
if ((shmid = shmget(10, 2*sizeof(node), 0644 | IPC_CREAT)) == -1) {
perror("shmget");
exit(1);
}
data = (node *) shmat(shmid, (void *)0, 0);
for(i=0;i<2;i++)
{
*(data++)=*SELL;
SELL=SELL->next;
}
getchar();
if (shmdt(data) == -1) {
perror("shmdt");
exit(1);
}
shmctl(shmid, IPC_RMID, NULL);
return 0;
}
Run process1.c 1st then run process2.c
I am trying madvise() to mark allocated memory as mergeable so that two applications having same pages can be merged.
While using the madvise() function it shows "invalid argument".
#include<stdio.h>
#include<sys/mman.h>
#include<stdlib.h>
#include<errno.h>
#define ADDR 0xf900f000
int main()
{
int *var1=NULL,*var2=NULL;
size_t size=0;
size = 1000*sizeof(int);
var1 = (int*)malloc(size);
var2 = (int *)malloc(size);
int i=0;
for(i=0;i<999;i++)
{
var1[i] = 1;
}
for(i=0;i<999;i++)
{
var2[i] = 1;
}
i = -1;
while(i<0)
{
i = madvise((void *)var1, size, MADV_MERGEABLE); //to declare mergeable
printf("%d %p\n", i, var1); //to print the output value
err(1,NULL); //to print the generated error
i = madvise((void *)var2, size, MADV_MERGEABLE); //to declare mergeable
printf("%d\n", i);
}
return 0;
}
Error:
a.out: Invalid argument
Please help me.
Thank You.
You can only merge whole pages. You can't merge arbitrary chunks of data.