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
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
相关文章推荐
- java-模拟tomcat服务器
- C#多线程之Thread中Thread.IsAlive属性用法分析
- c语言中malloc、realloc与calloc 的区别以及联系
- C++中new与delete、malloc与free应用分析
- C语言中的malloc使用详解
- Android开发笔记之:如何安全中止一个自定义线程Thread的方法
- 基于Linux调试工具strace与gdb的常用命令总结
- java thread start()和run()方法简析
- Java中Runnable和Thread的区别分析
- Android开发笔记之:Handler Runnable与Thread的区别详解
- new和malloc的区别深入解析
- C#多线程之Thread中Thread.Join()函数用法分析
- Mysql Error Code : 1436 Thread stack overrun
- MySQL错误Forcing close of thread的两种解决方法
- c#线程Thread示例
- Android线程管理之ActivityThread
- Android 关于“NetworkOnMainThreadException”问题的原因分析及解决办法
- gdb调试命令的使用及总结
- 如何使用GDB调试PHP程序
- Java之Thread的join方法实例