pthreads

Does each thread have its own stack?

断了今生、忘了曾经 提交于 2021-02-05 21:35:33
问题 When I create multiple threads from a process, then does each thread have its own stack, or is it that they share the stack of their parent process. What happens when a thread makes a system call? Do threads also maintain their own kernel stack like processes? 回答1: Yes threads have their own stacks and their own kernel stacks (e.g. linux). When a thread makes a system call, you trap into kernel mode (from user mode), you pass the arguments to the kernel, the arguments are checked, the kernel

void * typed function parameter

こ雲淡風輕ζ 提交于 2021-02-04 13:44:38
问题 I have a function, void *Client(void *threaData){} Can you tell me some things about void *threadData parameter. When you use void * parameter and why? 回答1: void * is a generic pointer which can point to any object type. The above function can take a pointer to any type and can return a pointer to any type. A generic pointer can be used if it is not sure about the data type of data inputted by the user. Example: The following function will print any data type provided the user input about the

void * typed function parameter

时光怂恿深爱的人放手 提交于 2021-02-04 13:44:23
问题 I have a function, void *Client(void *threaData){} Can you tell me some things about void *threadData parameter. When you use void * parameter and why? 回答1: void * is a generic pointer which can point to any object type. The above function can take a pointer to any type and can return a pointer to any type. A generic pointer can be used if it is not sure about the data type of data inputted by the user. Example: The following function will print any data type provided the user input about the

Linux socket using multiple threads to send

这一生的挚爱 提交于 2021-02-04 13:43:46
问题 I have a single non-blocking socket sending udp packets to multiple targets and receiving responses from all of them on the same socket. I'm reading in a dedicated thread but writes (sendto) can come from several different threads. Is this a safe without any additional synchronization? Do I need to write while holding a mutex? Or, do writes need to come from the same thread and I need a queue? 回答1: The kernel will synchronize access to underlying file descriptor for you, so you don't need a

Linux C编程之十四 线程、线程控制、线程属性

放肆的年华 提交于 2021-02-01 02:45:22
一、整体大纲 二、线程相关 1. 什么是线程 LWP:light weight process 轻量级的进程,本质仍是进程(在Linux环境下) 进程:独立地址空间,拥有PCB 线程:也有PCB,但没有独立的地址空间(共享) 区别:在于是否共享地址空间。 独居(进程);合租(线程)。 Linux下: 线程:最小的执行单位 进程:最小分配资源单位,可看成是只有一个线程的进程。 2. Linux内核线程实现原理 (1)线程实现原理 类Unix系统中,早期是没有“线程”概念的,80年代才引入,借助进程机制实现出了线程的概念。因此在这类系统中,进程和线程关系密切。 1)轻量级进程(light-weight process),也有PCB,创建线程使用的底层函数和进程一样,都是clone 2)从内核里看进程和线程是一样的,都有各自不同的PCB,但是PCB中指向内存资源的三级页表是相同的 3)进程可以蜕变成线程 4)线程可看做寄存器和栈的集合 5)在linux下,线程最是小的执行单位;进程是最小的分配资源单位 察看LWP号:ps -Lf pid 查看指定线程的lwp号。 三级映射:进程PCB --> 页目录(可看成数组,首地址位于PCB中) --> 页表 --> 物理页面 --> 内存单元 参考:《Linux内核源代码情景分析》 ----毛德操 对于进程来说,相同的地址(同一个虚拟地址

Memory leaks in pthread even if the state is detached

穿精又带淫゛_ 提交于 2021-01-29 18:37:55
问题 I am learning pthreads programming. I understood that there are two states of thread: 1. Joinable 2. Detachable In case of Joinable, we need to call pthread_join to free the resources(stack), whereas in case of detached there is no need to call pthread_join and the resources will be freed on thread exit. I wrote a sample program to observe the behavior #include <stdio.h> #include <pthread.h> #include <stdlib.h> void *threadFn(void *arg) { pthread_detach(pthread_self()); sleep(1); printf(

Printing the default thread's priority

北战南征 提交于 2021-01-29 07:10:08
问题 I'd like to write a code which prints the default thread's priority, but I don't know if this is possible. So far I created a thread with default attributes, but I didn't find any statement which allows me to store and print its default priority. // main.c #include <stdlib.h> #include <stdio.h> #include <pthread.h> #include <sched.h> #include "task.h" int main() { pthread_attr_t attr; struct sched_param prio; pthread_t tid; int create = 1; // default attributes pthread_attr_init(&attr);

Why `pthread_rwlock_t`'s ABI differs a lot among versions?

僤鯓⒐⒋嵵緔 提交于 2021-01-29 05:53:52
问题 I am looking into different versions of implementation of pthread_rwlock_t . GLIBC2.30 typedef union { struct __pthread_rwlock_arch_t __data; char __size[__SIZEOF_PTHREAD_RWLOCK_T]; long int __align; } pthread_rwlock_t; struct __pthread_rwlock_arch_t { unsigned int __readers; unsigned int __writers; unsigned int __wrphase_futex; unsigned int __writers_futex; unsigned int __pad3; unsigned int __pad4; ... GLIBC2.17 typedef union { # ifdef __x86_64__ struct { int __lock; unsigned int __nr

Pthreads compile not working

╄→гoц情女王★ 提交于 2021-01-28 13:30:35
问题 I have written some code but it doesn't seem to work when I compile it. I am trying to run this in Ubuntu: #include <pthread.h> #include <ctype.h> #include <unistd.h> char buffer[128]; void *write_thread(void *args) { int count = *((int*)args); write(STDOUT_FILENO, buffer, count); pthread_exit(0); } void *toupper_thread(void *args) { int i; int count = *((int*)args); for(i = 0; i < count; i++) { buffer[i] = toupper(buffer[i]); } pthread_t writeId; pthread_create(&writeId, NULL, write_thread,

Undefined reference error _dl_stack_flags with gcc and pthreads

回眸只為那壹抹淺笑 提交于 2021-01-28 04:31:07
问题 I get the following error when trying to compile an application using pthreads on Ubuntu: /usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/libpthread.a(pthread_create.o): In function `allocate_stack': /build/buildd/eglibc-2.11.1/nptl/allocatestack.c:444: undefined reference to `_dl_stack_flags' Ubuntu version is: wade@wadesworld:~$ uname -a Linux wadesworld 2.6.18-194.8.1.el5.028stab070.5ent #1 SMP Fri Sep 17 19:46:02 MSD 2010 i686 GNU/Linux gcc version is: wade@wadesworld:~$ gcc -v Using