Beatkiller
13-09-2008, 22:25
Hallo,
ich rätsel schon seit geraumer Zeit an diesem Problem, und bin mit Sicherheit bereits betriebsblind. Kann mir jemand sagen, was an dem Code falsch sein soll, dass der Speicher mit pthread_join() nicht freigegeben wird?
/*
* main.c
*/
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string>
#define OWNPID getpid()
#define MAXTHREADS 10
struct threadargs
{
unsigned int id;
std::string first;
std::string last;
};
void *threadfunc(void *args)
{
threadargs *myArgs = (threadargs*)args;
printf("first: %s\n", myArgs->first.c_str());
printf("last: %s\n", myArgs->last.c_str());
pthread_exit((void*)0);
}
void TraceUsage(char *label)
{
pid_t pid;
char comm[30];
char state;
pid_t ppid;
gid_t pgrp;
int session;
int tty;
gid_t tpgid;
unsigned int flags;
unsigned long minflt;
unsigned long cminflt;
unsigned long majflt;
unsigned long cmajflt;
int utime;
int stime;
int cutime;
int cstime;
int counter;
short priority;
unsigned long timeout;
unsigned long itrealvalue;
int starttime;
unsigned long vsize;
unsigned long rss;
unsigned long rlim;
unsigned long startcode;
unsigned long endcode;
unsigned long startstack;
unsigned long kstkesp;
unsigned long kstkeip;
int signal;
int blocked;
int sigignore;
int sigcatch;
unsigned long wchan;
char statpath[30];
sprintf(statpath, "/proc/%d/stat", OWNPID);
FILE *fp = fopen(statpath, "r");
if(fp == NULL)
return;
int r = fscanf(fp,
"%d %s %c %d %d %d %d %d %u %ld %ld %ld %ld %d %d %d %d %d %d %ld %ld %d %ld %ld %ld %ld %ld %ld %ld %ld %d %d %d %d %ld",
&pid, comm, &state, &ppid, &pgrp, &session, &tty, &tpgid, &flags, &minflt, &cminflt,
&majflt, &cmajflt, &utime, &stime, &cutime, &cstime, &counter, &priority, &timeout,
&itrealvalue, &starttime, &vsize, &rss, &rlim, &startcode, &endcode, &startstack,
&kstkesp, &kstkeip, &signal, &blocked, &sigignore, &sigcatch, &wchan
);
fclose(fp);
if(r == 0)
{
return;
}
fprintf(stderr, "%s: rss = %ld, vsize = %ld\n\n", label, rss, vsize);
}
int main(void)
{
pthread_t thread[MAXTHREADS];
char label[10];
unsigned int i;
threadargs targs;
targs.first = "Hello";
targs.last = "World!";
TraceUsage("Init");
for(i = 0; i < MAXTHREADS; i++)
{
targs.id = i;
pthread_create(&thread[i], NULL, threadfunc, (void*)&targs);
pthread_join(thread[i], NULL);
sprintf(label, "%d", i);
TraceUsage(label);
}
}
Es wird folgende Ausgabe erzeugt:
./threadtest
Init: rss = 220, vsize = 2375680
first: Hello
last: World!
0: rss = 234, vsize = 10780672
first: Hello
last: World!
1: rss = 234, vsize = 10780672
first: Hello
last: World!
2: rss = 234, vsize = 10780672
first: Hello
last: World!
3: rss = 234, vsize = 10780672
first: Hello
last: World!
4: rss = 234, vsize = 10780672
first: Hello
last: World!
5: rss = 234, vsize = 10780672
first: Hello
last: World!
6: rss = 234, vsize = 10780672
first: Hello
last: World!
7: rss = 234, vsize = 10780672
first: Hello
last: World!
8: rss = 234, vsize = 10780672
first: Hello
last: World!
9: rss = 234, vsize = 10780672
Wäre toll, wenn mir da jemand weiter helfen könnte.
PS: Ja, ich weiß, man soll C++ Objekte nicht in C-Code einbauen, das war auch nur ein Test, um den Speicher künstlich aufzublähen.
ich rätsel schon seit geraumer Zeit an diesem Problem, und bin mit Sicherheit bereits betriebsblind. Kann mir jemand sagen, was an dem Code falsch sein soll, dass der Speicher mit pthread_join() nicht freigegeben wird?
/*
* main.c
*/
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string>
#define OWNPID getpid()
#define MAXTHREADS 10
struct threadargs
{
unsigned int id;
std::string first;
std::string last;
};
void *threadfunc(void *args)
{
threadargs *myArgs = (threadargs*)args;
printf("first: %s\n", myArgs->first.c_str());
printf("last: %s\n", myArgs->last.c_str());
pthread_exit((void*)0);
}
void TraceUsage(char *label)
{
pid_t pid;
char comm[30];
char state;
pid_t ppid;
gid_t pgrp;
int session;
int tty;
gid_t tpgid;
unsigned int flags;
unsigned long minflt;
unsigned long cminflt;
unsigned long majflt;
unsigned long cmajflt;
int utime;
int stime;
int cutime;
int cstime;
int counter;
short priority;
unsigned long timeout;
unsigned long itrealvalue;
int starttime;
unsigned long vsize;
unsigned long rss;
unsigned long rlim;
unsigned long startcode;
unsigned long endcode;
unsigned long startstack;
unsigned long kstkesp;
unsigned long kstkeip;
int signal;
int blocked;
int sigignore;
int sigcatch;
unsigned long wchan;
char statpath[30];
sprintf(statpath, "/proc/%d/stat", OWNPID);
FILE *fp = fopen(statpath, "r");
if(fp == NULL)
return;
int r = fscanf(fp,
"%d %s %c %d %d %d %d %d %u %ld %ld %ld %ld %d %d %d %d %d %d %ld %ld %d %ld %ld %ld %ld %ld %ld %ld %ld %d %d %d %d %ld",
&pid, comm, &state, &ppid, &pgrp, &session, &tty, &tpgid, &flags, &minflt, &cminflt,
&majflt, &cmajflt, &utime, &stime, &cutime, &cstime, &counter, &priority, &timeout,
&itrealvalue, &starttime, &vsize, &rss, &rlim, &startcode, &endcode, &startstack,
&kstkesp, &kstkeip, &signal, &blocked, &sigignore, &sigcatch, &wchan
);
fclose(fp);
if(r == 0)
{
return;
}
fprintf(stderr, "%s: rss = %ld, vsize = %ld\n\n", label, rss, vsize);
}
int main(void)
{
pthread_t thread[MAXTHREADS];
char label[10];
unsigned int i;
threadargs targs;
targs.first = "Hello";
targs.last = "World!";
TraceUsage("Init");
for(i = 0; i < MAXTHREADS; i++)
{
targs.id = i;
pthread_create(&thread[i], NULL, threadfunc, (void*)&targs);
pthread_join(thread[i], NULL);
sprintf(label, "%d", i);
TraceUsage(label);
}
}
Es wird folgende Ausgabe erzeugt:
./threadtest
Init: rss = 220, vsize = 2375680
first: Hello
last: World!
0: rss = 234, vsize = 10780672
first: Hello
last: World!
1: rss = 234, vsize = 10780672
first: Hello
last: World!
2: rss = 234, vsize = 10780672
first: Hello
last: World!
3: rss = 234, vsize = 10780672
first: Hello
last: World!
4: rss = 234, vsize = 10780672
first: Hello
last: World!
5: rss = 234, vsize = 10780672
first: Hello
last: World!
6: rss = 234, vsize = 10780672
first: Hello
last: World!
7: rss = 234, vsize = 10780672
first: Hello
last: World!
8: rss = 234, vsize = 10780672
first: Hello
last: World!
9: rss = 234, vsize = 10780672
Wäre toll, wenn mir da jemand weiter helfen könnte.
PS: Ja, ich weiß, man soll C++ Objekte nicht in C-Code einbauen, das war auch nur ein Test, um den Speicher künstlich aufzublähen.