没有合适的资源?快使用搜索试试~ 我知道了~
3-同步与并发-同步1
需积分: 0 0 下载量 85 浏览量
2022-08-03
12:13:32
上传
评论
收藏 21.51MB PDF 举报
温馨提示
试读
43页
编译:来自CSAPP第7章链接94959697
资源详情
资源评论
资源推荐
1
2019年10月
操作系统
Operating Systems
刘 川 意
副教授
哈尔滨工业大学(深圳)
2
Module 3:
同步
(
Synchronization
)
1.
信号的介绍 (
Introduction to Signals
)
CSAPP3e: 8.5
2.
同步(
Synchronization
)
CSAPP3e: 12.4-12.7
3
Shared Variables in Threaded C Programs
! Question: Which variables in a threaded C program are shared?
! The answer is not as simple as “global variables are shared” and
“stack variables are private”
! Definition: A variable x is shared if and only if multiple threads reference
some instance of x.
! Requires answers to the following questions:
! What is the memory model for threads?
! How are instances of variables mapped to memory?
! How many threads might reference each of these instances?
4
Threads Memory Model
! Conceptual model(概念模型):
! Multiple threads run within the context of a single process
! Each thread has its own separate thread context
4 Thread ID, stack, stack pointer, PC, condition codes, and GP registers
! All threads share the remaining process context
4 Code, data, heap, and shared library segments of the process virtual
address space
4 Open files and installed handlers
! Operationally, this model is not strictly enforced:
! Register(寄存器) values are truly separate and protected, but virtual
memory is always shared
! Any thread can read and write the stack of any other thread
The mismatch between the conceptual
(概念)
and operation
(操作)
model is a
source of confusion and errors
5
Example Program to Illustrate Sharing
6. char **ptr; /* global var */
33. int main()
34. {
35. long i;
36. pthread_t tid;
37. char *msgs[2] = {
38. "Hello from foo",
39. "Hello from bar"
40. };
41. ptr = msgs;
42. for (i = 0; i < 2; i++)
43. Pthread_create(&tid,
44. NULL,
45. thread,
46. (void *)i);
47. Pthread_exit(NULL);
48. }
7. void *thread(void *vargp)
8. {
9. long myid = (long)vargp;
10. static int cnt = 0;
11. printf("[%ld]: %s (cnt=%d)\n",
12. myid, ptr[myid], ++cnt);
13. return NULL;
14. }
Peer threads reference main thread’s stack
indirectly through global ptr variable
sharing.c
剩余42页未读,继续阅读
7323
- 粉丝: 22
- 资源: 327
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Python 程序语言设计模式思路-行为型模式:策略模式:将算法封装成独立的类,并使它们可以互相替换及支付模式数据压缩
- main.py
- Last Loaded Test.DBK
- Screenshot_20240520_163011.jpg
- ubuntu-python3-whisper-tornado docker镜像 Dockerfile
- ubuntu-python3-whisper-tornado docker镜像07
- 新录音 8.m4a
- ubuntu-python3-whisper-tornado docker镜像
- ubuntu-python3-whisper-tornado docker镜像
- ubuntu-python3-whisper-tornado docker镜像09
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0