您的位置:首页 > 其它

System and device programming lab2---gdb(debug)pthread,matrix

2016-03-20 02:38 288 查看
Pthread
1.pthread_detach(pthread_self()):
it's used when the thread doesn't need to wait the threads it create.
But in this lab,the thread can only create its second thread after first

thread finishes,so need to use pthread_join(th,NULL),can't use 1.
2.pthread_create(&th(1),NULL,mergeSort,?): (1),to create several threads
one after the other,it's ok to use only one th,but maybe better to use th[i]
to wait for specific thread;

? can't use &i,because i maybe
a register,can use pi[i]...; ? if it has already been a pointer,which points to the address of the content,so no need to add'&',otw it stands for the address of the pointer,no meaning here,if it's not pointer,should add '&'; if we want to pass more than one parameters,we
can use a structure which contains many fields.
e.g.
typedef struct//using typedef,later just use(1) ok to creat an instance

{
int l1;
int r1;
}Me;
(1)Me e;
Or
typedef struct
{
int l1;
int r1;
int *v;//!
}Me;
Me *m;//*

3.void *malloc(size_t size) allocates the requested memory and returns
a pointer to it.
(1)for structure
m=(Me*)malloc(sizeof(Me));
(2)for array in structure
m1->v=(int *)malloc(sizeof(m->v));
(3)for matrix(1-D and 2-D)
1-D:

v2=(float *)malloc(k*sizeof(float));
2-D:
mat = (float **)malloc(sizeof(*mat)*k);

for (i = 0; i < k; i++)
{
mat[i] = (float *)malloc(sizeof(float)*k);
}
Just remember adapt the type
4.to get random number in range[-0.5,0.5]:

for(i=0;i<k;i++){
v1[i]=rand()%6*0.1;
n=rand()%2+1;
for(nn=0;nn<n;nn++)
v1[i]=v1[i]*(-1);
printf("%f ",v1[i]);
}
5.Debug
gcc -o -g ..
gdb ./exe
b line/address
start
next
info breakpoints
disable id of breakpoint
q //exit
//to insert parameters:
Once gdb starts, you can run the program using "r args".
So if you are running your code by:
$ executablefile arg1 arg2 arg3
Debug it on gdb by:
$ gdb executablefile
(gdb) r arg1 arg2 arg3

By Address

A fourth way of setting breakpoints is with a memory address within the process's VM space. I'll find the address of TakeGuess() and set a breakpoint at that address:

(gdb) print TakeGuess
$1 = {int (const char *)} 0x80483f4 <TakeGuess>
(gdb) break *0x80483f4
"[Inferior 1 (process 3516) exited with code 01]" is normal exit if the code return 1 or some other function return 1. Say: (gdb)b main (gdb) r < textfile.txt – Morpfh Apr 24 '12 at 3:22
@Rune: slightly simpler variant: (gdb) start < textfile.txt - no need to set a breakpoint on main() yourself. http://stackoverflow.com/questions/10290898/gdb-input-files-and-such step count
Continue running as in step, but do so count times. If a breakpoint is reached, or a signal not related to stepping occurs before count steps, stepping stops right away.
next [count]
Continue to the next source line in the current (innermost) stack frame. This is similar to step, but function calls that appear within the line of code are executed without stopping. Execution stops when control reaches a different line of code at the original stack level that was executing when you gave the next command. This command is abbreviated n. An argument count is a repeat count, as for step. The next command now only stops at the first instruction of a source line. This prevents the multiple stops that used to occur in swtch statements, for loops, etc.
2876 malloc.c: No such file or directory.
--->http://stackoverflow.com/questions/9220853/call-to-malloc-failing-in-gdb-session
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  thread gdb malloc