/* example.c*/ #include <stdio.h> #include <pthread.h> void thread(char * k) { int i; char * ss; ss = k;
//strcpy(ss,"xyz"); /*for(i=0;i <3;i++) { //printf("This is a pthread.\n"); //printf("in pthread s is %s\n",ss); (*ss)++; printf("now %d ss is %c\n",i,ss); ss++; printf("now %d ss 2 is %c\n",i,ss);
} ss = k; */ ss = (char *) calloc(sizeof(char),4); strcpy(ss,"abc"); k = ss; printf("in pthread ss is %s\n",ss); printf("This is a pthread.\n"); printf("in pthread k is %s\n",k);
}
int main(void) { pthread_t id; int i,ret; char * s; //s = (char *) calloc(sizeof(char),4); //strcpy(s,"abc"); //s[4] = '\0' printf("in main s is %s \n",s); ret=pthread_create(&id,NULL,(void *) thread,s); if(ret!=0){ printf ("Create pthread error!\n"); exit (1); } //for(i=0;i <3;i++) printf("This is the main process.\n"); pthread_join(id,NULL); printf("at the end s is %s \n",s); return (0);
这是从 < <unix环境高级编程>>中摘出来的一段话,描述了线程共享进程的哪些内容. A thread consists of the information necessary to represent an execution context within a process. This includes a thread ID that identifies the thread within a process, a set of register values, a stack, a scheduling priority and policy, a signal mask, an errno variable (recall Section 1.7), and thread-specific data (Section 12.6). Everything within a process is sharable among the threads in a process, including the text of the executable program, the program's global and heap memory, the stacks, and the file descriptors.
str2=(char *)malloc(51); /*存放在堆中*/ memset(str1,0,21); memset(str2,0,51); strncpy(str1,"Hello world!",20); strncpy(str2,"this is str2",50); printf("before modify: str1 is '%s'\n",str1); printf("before modify: str2 is '%s'\n",str2); if(pthread_create(&tid,NULL,thr_callback,str1)!=0) { perror("pthread_create"); exit(1); } pthread_join(tid,&retval); printf("after modify: str1 is '%s'\n",str1); printf("return value of thread is %d \n",(int)retval);
if(pthread_create(&tid,NULL,thr_callback,str2)!=0) { perror("pthread_create"); exit(1); } pthread_join(tid,&retval); printf("after modify: str2 is '%s'\n",str2); printf("return value of thread is %d \n",(int)retval); exit(0); }
pds@FSSR:~> gcc -o thr_test1 thr_test1.c -lpthread pds@FSSR:~> ./thr_test1 before modify: str1 is 'Hello world!' before modify: str2 is 'this is str2' after modify: str1 is 'new value' return value of thread is 0 after modify: str2 is 'new value' return value of thread is 0
/* example.c*/ #include <stdio.h> #include <pthread.h> void thread(char * k) { int i; char * ss; ss = k;
ss = (char *) calloc(sizeof(char),4); strcpy(ss,"abc"); k = ss; printf("in pthread ss is %s\n",ss); printf("This is a pthread.\n"); printf("in pthread k is %s\n",k);
}
int main(void) { pthread_t id; int i,ret; char * s;
printf("in main s is %s \n",s); ret=pthread_create(&id,NULL,(void *) thread,s); if(ret!=0){ printf ("Create pthread error!\n"); exit (1); }
printf("This is the main process.\n"); pthread_join(id,NULL); printf("at the end s is %s \n",s); return (0);
printf("This is the main process.\n"); pthread_join(id1,NULL); printf("the pthread1 is end.\n"); pthread_join(id2,NULL); printf("the pthread2 is end.\n"); printf("stack in main is %d\n",parg1.stack); printf(" heap in main is %s\n",parg1.heap); return (0);
}
以上为验证程序,但是不注释掉
//parg2.stack = 2; //parg2.heap = parg1.heap;
把parg2付给线程2时得到的结果和注释掉都用parg1赋值时的结果不同
注释掉时: This is a pthread1. stack in thread1 is 3 heap in thread1 is def This is a pthread2. stack in thread2 is 3 heap in thread2 is def This is the main process. the pthread1 is end. the pthread2 is end. stack in main is 3
不注释掉时: This is a pthread1. stack in thread1 is 3 heap in thread1 is def This is a pthread2. stack in thread2 is 2 heap in thread2 is ptm This is the main process. the pthread1 is end. the pthread2 is end. stack in thread2 is 3 heap in thread2 is ptm
More than just the global variables are shared. All threads within a process share the following: Process instructions Most data Open files (e.g., descriptors) Signal handlers and signal dispositions Current working directory User and group IDs
But each thread has its own Thread ID Set of registers, including program counter and stack pointer Stack (for local variables and return addresses) errno Signal mask Priority --------- UNIX Network Programming Volume 1 By W. Richard Stevens
这是从 < <unix环境高级编程>>中摘出来的一段话,描述了线程共享进程的哪些内容. A thread consists of the information necessary to represent an execution context within a process. This includes a thread ID that identifies the thread within a process, a set of register values, a stack, a scheduling priority and policy, a signal mask, an errno variable (recall Section 1.7), and thread-specific data (Section 12.6). Everything within a process is sharable among the threads in a process, including the text of the executable program, the program's global and heap memory, the stacks, and the file descriptors. ~~~~~~~~~~~~ 六楼的, 这个共享的stack是什么东东?~