sysgettid头文件|进程内核栈用户栈及 Linux 进程栈和线程栈的区别

❶ linux线程里面分主线程和从线程吗具体有什么区别。求大神

程序启动默认主线程在运行,要想有其他线程你必须手动创建,线程之间没用没用主从说回法,你要判断答线程是不是main线程,可以::syscall(SYS_gettid)==getpid()来判断,判断线程的tid是否等于该进程的id。

❷ linux中pid gid tgid tid的区别和联系

getpid : 取得process id,对于thread,就是取得线程对应进程的id;对应的system call: asmlinkage long sys_getpid(void)1614 {1615 return current->tgid;1616 }tgid = thread group id /世返///////////////////////////散返迅///////////////////////////////////////////// gettid:取冲此得线程id,如果是process,其实就等于getpid:#define gettid() syscall(__NR_gettid)对应的system call:/* Thread ID – the internal kernel "pid" */1761 asmlinkage long sys_gettid(void)1762 {1763 return current->pid;1764 }getgid:取得group id,就是user的group id和用id命令得到的group id是一回事:bash-3.1$ iduid=3013(xxx) gid=300(xxxx) groups=300(xxxx),301(platform),505(fpgadrv),9022(gsm) 对于一个thread来说,在kenerl中可以看到他的process id(tgid),还可以看到他的thread id(pid)。

❸ 进程内核栈,用户栈及 Linux 进程栈和线程栈的区别

内核栈、用户栈32位Linux系统上,进程的地址空间为4G,包括1G的内核地址空间—–内核栈,和3G的用户地址空间—–用户栈。内核栈,是各个进程在刚开始建立的时候通过内存映射共享的,但是每个进程拥有独立的4G的虚拟内存空间从这一点看又是独立的,互不干扰的(只是刚开始大家都是映射的同一份内存拷贝)用户栈就是大家所熟悉的内存四区,包括:代码区、全局数据区、堆区、栈区用户栈中的堆区、栈区即为进程堆、进程栈进程堆、进程栈与线程栈1.线程栈的空间开辟在所属进程的堆区与共享内存区之间,线程与其所属的进程共享进程的用户空间,所以线程栈之间可以互访。线程栈的起始地址和大小存放在pthread_attr_t 中,栈的大小并不是用来判断栈是否越界,而是用来初始化避免栈溢出的缓冲区的大小(或者说安全间隙的大小)2.进程初始化的时候,系统会在进程的地址空间中创建一个堆,叫进程默认堆。进程中所有的线程共用这一个堆。当然,可以增加1个或几个堆,给不同的线程共同使用或单独使用。—-一个进程可以多个堆3、创建线程的时候,系统会在进程的地址空间中分配1块内存给线程栈,通常是1MB或4MB或8MB。线程栈是独立的,但是还是可以互访,因为线程共享内存空间4.堆的分配:从操作系统角度来看,进程分配内存有两种方式,分别由两个系统调用完成:brk()和mmap(),glibc中malloc封装了5.线程栈位置-内存分布测试代码[cpp] view plain #include <pthread.h> #include <stdio.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <malloc.h> #include <sys/syscall.h> void* func(void* arg) { long int tid = (long int)syscall(SYS_gettid); printf("The ID of this thread is: %ld\n", tid ); static int a=10; int b=11; int* c=(int *)malloc(sizeof(int)); printf("in thread id:%u a:%p b:%p c:%p\n",tid,&a,&b,c); printf("leave thread id:%ld\n",tid); sleep(20); free((void *)c); } void main() { pthread_t th1,th2; printf("pid=%u\n",(int)getpid()); func(NULL); int ret=pthread_create(&th1,NULL,func,NULL); if(ret!=0) { printf("thread1[%d]:%s\n",th1,strerror(errno)); } ret=pthread_create(&th2,NULL,func,NULL); if(ret!=0) { printf("thread2[%d]:%s\n",th2,strerror(errno)); } pthread_join(th1,NULL); pthread_join(th2,NULL); } 输出:[[email protected] threadStack]$ ./threadStack_main pid=16433The ID of this thread is: 16433in thread id:16433 a:0x60107c b:0x7fffc89ce7ac c:0x1b54010leave thread id:16433The ID of this thread is: 16461The ID of this thread is: 16460in thread id:16461 a:0x60107c b:0x7f6abb096efc c:0x7f6ab40008c0leave thread id:16461in thread id:16460 a:0x60107c b:0x7f6abb897efc c:0x7f6aac0008c0leave thread id:16460主线程调用func后[[email protected] threadStack]$ sudo cat /proc/16433/maps 00400000-00401000 r-xp 00000000 fd:02 11666 /home/le/code/threadStack/threadStack_main00600000-00601000 r–p 00000000 fd:02 11666 /home/le/code/threadStack/threadStack_main00601000-00602000 rw-p 00001000 fd:02 11666 /home/le/code/threadStack/threadStack_main01b54000-01b75000 rw-p 00000000 00:00 0 [heap]7f6abb899000-7f6abba4f000 r-xp 00000000 fd:00 100678959 /usr/lib64/libc-2.17.so7f6abba4f000-7f6abbc4f000 —p 001b6000 fd:00 100678959 /usr/lib64/libc-2.17.so7f6abbc4f000-7f6abbc53000 r–p 001b6000 fd:00 100678959 /usr/lib64/libc-2.17.so7f6abbc53000-7f6abbc55000 rw-p 001ba000 fd:00 100678959 /usr/lib64/libc-2.17.so7f6abbc55000-7f6abbc5a000 rw-p 00000000 00:00 0 7f6abbc5a000-7f6abbc70000 r-xp 00000000 fd:00 105796566 /usr/lib64/libpthread-2.17.so7f6abbc70000-7f6abbe70000 —p 00016000 fd:00 105796566 /usr/lib64/libpthread-2.17.so7f6abbe70000-7f6abbe71000 r–p 00016000 fd:00 105796566 /usr/lib64/libpthread-2.17.so7f6abbe71000-7f6abbe72000 rw-p 00017000 fd:00 105796566 /usr/lib64/libpthread-2.17.so7f6abbe72000-7f6abbe76000 rw-p 00000000 00:00 0 7f6abbe76000-7f6abbe97000 r-xp 00000000 fd:00 105796545 /usr/lib64/ld-2.17.so7f6abc073000-7f6abc076000 rw-p 00000000 00:00 0 7f6abc095000-7f6abc097000 rw-p 00000000 00:00 0 7f6abc097000-7f6abc098000 r–p 00021000 fd:00 105796545 /usr/lib64/ld-2.17.so7f6abc098000-7f6abc099000 rw-p 00022000 fd:00 105796545 /usr/lib64/ld-2.17.so7f6abc099000-7f6abc09a000 rw-p 00000000 00:00 0 7fffc89b0000-7fffc89d1000 rw-p 00000000 00:00 0 [stack]7fffc89fe000-7fffc8a00000 r-xp 00000000 00:00 0 [vdso]ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]两个子线程启动后[[email protected] threadStack]$ sudo cat /proc/16433/maps 00400000-00401000 r-xp 00000000 fd:02 11666 /home/le/code/threadStack/threadStack_main00600000-00601000 r–p 00000000 fd:02 11666 /home/le/code/threadStack/threadStack_main00601000-00602000 rw-p 00001000 fd:02 11666 /home/le/code/threadStack/threadStack_main01b54000-01b75000 rw-p 00000000 00:00 0 [heap]7f6aac000000-7f6aac021000 rw-p 00000000 00:00 0 7f6aac021000-7f6ab0000000 —p 00000000 00:00 0 7f6ab4000000-7f6ab4021000 rw-p 00000000 00:00 0 7f6ab4021000-7f6ab8000000 —p 00000000 00:00 0 7f6aba897000-7f6aba898000 —p 00000000 00:00 0 7f6aba898000-7f6abb098000 rw-p 00000000 00:00 0 [stack:16461]7f6abb098000-7f6abb099000 —p 00000000 00:00 0 7f6abb099000-7f6abb899000 rw-p 00000000 00:00 0 [stack:16460]7f6abb899000-7f6abba4f000 r-xp 00000000 fd:00 100678959 /usr/lib64/libc-2.17.so7f6abba4f000-7f6abbc4f000 —p 001b6000 fd:00 100678959 /usr/lib64/libc-2.17.so7f6abbc4f000-7f6abbc53000 r–p 001b6000 fd:00 100678959 /usr/lib64/libc-2.17.so7f6abbc53000-7f6abbc55000 rw-p 001ba000 fd:00 100678959 /usr/lib64/libc-2.17.so7f6abbc55000-7f6abbc5a000 rw-p 00000000 00:00 0 7f6abbc5a000-7f6abbc70000 r-xp 00000000 fd:00 105796566 /usr/lib64/libpthread-2.17.so7f6abbc70000-7f6abbe70000 —p 00016000 fd:00 105796566 /usr/lib64/libpthread-2.17.so7f6abbe70000-7f6abbe71000 r–p 00016000 fd:00 105796566 /usr/lib64/libpthread-2.17.so7f6abbe71000-7f6abbe72000 rw-p 00017000 fd:00 105796566 /usr/lib64/libpthread-2.17.so7f6abbe72000-7f6abbe76000 rw-p 00000000 00:00 0 7f6abbe76000-7f6abbe97000 r-xp 00000000 fd:00 105796545 /usr/lib64/ld-2.17.so7f6abc073000-7f6abc076000 rw-p 00000000 00:00 0 7f6abc095000-7f6abc097000 rw-p 00000000 00:00 0 7f6abc097000-7f6abc098000 r–p 00021000 fd:00 105796545 /usr/lib64/ld-2.17.so7f6abc098000-7f6abc099000 rw-p 00022000 fd:00 105796545 /usr/lib64/ld-2.17.so7f6abc099000-7f6abc09a000 rw-p 00000000 00:00 0 7fffc89b0000-7fffc89d1000 rw-p 00000000 00:00 0 [stack]7fffc89fe000-7fffc8a00000 r-xp 00000000 00:00 0 [vdso]ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]

❹ C++ 中获取子线程id是什么函数

pthread_create 的第一个参数就是线程id。pthread_self 的返回值和pthread_create的第一个返回参数的值是一样的 你要的线塌烂兆程id就是通过历伏这两个函数获取的。 看一下linux下的帮助吧 man pthread_self 至于为什么ps -eLf 的线程id对不上,这个我不太清楚。但团租可以肯定的是,你用的接口应该是对的。


赞 (0)