Synchronize the height of iframe and parent

本文介绍了一种通过JavaScript调整iframe元素高度的方法,确保iframe能够自适应其内容高度,避免出现滚动条的同时保持良好的页面布局。
Just a test!

Tips:
o.height = ifr.h;
can not be changed with:
o.height = o.h;

Parent[iframe.html]
  1. <!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <html>
  3. <head>
  4. <title> New Document </title>
  5. <style>
  6. body {
  7.     padding: 0px;
  8.     margin: 0px;
  9. }
  10. </style>
  11. <script>
  12. var h;
  13. </script>
  14. </head>
  15. <body onload="test()">
  16. <table border="1">
  17. <tr>
  18. <td>There is a test</td>
  19. </tr>
  20. <tr>
  21. <td><iframe id="ifr" src="content.html" width="100%" frameborder="0" scrolling="no"></iframe></td>
  22. </tr>
  23. </table>
  24. </body>
  25. <script>
  26. function test() {
  27.     var o = document.getElementById("ifr");
  28.     o.height = ifr.h;
  29. }
  30. </script>
  31. </html>
Iframe[content.html]
  1. test<br/>
  2. test<br/>
  3. test<br/>
  4. test<br/>
  5. test<br/>
  6. test<br/>
  7. test<br/>
  8. test<br/>
  9. test<br/>
  10. test<br/>
  11. test<br/>
  12. test<br/>
  13. test<br/>
  14. test<br/>
  15. test<br/>
  16. test<br/>
  17. test<br/>
  18. test<br/>
  19. test<br/>
  20. test<br/>
  21. test<br/>
  22. test<br/>
  23. test<br/>
  24. test<br/>
  25. test<br/>
  26. test<br/>
  27. test<br/>
  28. test<br/>
  29. test<br/>
  30. test<br/>
  31. test<br/>
  32. test<br/>
  33. test<br/>
  34. test<br/>
  35. test<br/>
  36. test<br/>
  37. test<br/>
  38. test<br/>
  39. test<br/>
  40. test<br/>
  41. test<br/>
  42. test<br/>
  43. test<br/>
  44. test<br/>
  45. test<br/>
  46. test<br/>
  47. test<br/>
  48. test<br/>
  49. test<br/>
  50. test<br/>
  51. test<br/>
  52. test<br/>
  53. test<br/>
  54. test<br/>
  55. test<br/>
  56. test<br/>
  57. test<br/>
  58. test<br/>
  59. test<br/>
  60. test<br/>
  61. test<br/>
  62. test<br/>
  63. test<br/>
  64. test<br/>
  65. test<br/>
  66. test<br/>
  67. test<br/>
  68. test<br/>
  69. test<br/>
  70. test<br/>
  71. test<br/>
  72. test<br/>
  73. test<br/>
  74. test<br/>
  75. test<br/>
  76. test<br/>
  77. test<br/>
  78. test<br/>
  79. test<br/>
  80. End<br/>
  81. <script>
  82. var h;
  83. window.onload = function() {
  84.     h = document.body.scrollHeight;
  85. }
  86. </script>

====================================================================================
a simple method to realize it.

function test() {
    var o = document.getElementById("ifr");
    o.height = o.Document.body.scrollHeight;
}

window.onload = test;

caution: the "o.Document" not "o.document".
<think>嗯,用户想了解xv6中线程同步的作业内容和示例。首先,我需要回忆一下xv6相关的资料,特别是作业部分。根据提供的引用,特别是引用[2]和[3],用户提到的作业涉及到哈希表的线程和锁机制。用户可能想知道具体的实现方法和例子,比如锁的使用步骤,或者代码修改的部分。 用户的问题是关于xv6作业中的线程同步实现和例子,所以需要结合引用[3]中的内容。那里提到在ph.c中添加锁数组,初始化锁,以及在insert操作时加锁。但即使这样,单线程和双线程的时间差距还是很大,可能还存在问题。这说明作业可能涉及到锁的正确使用,或者需要更优化的同步机制。 我应该分步骤回答,先说明作业的背景,然后详细实现步骤,比如声明锁变量、初始化、在操作中加锁。然后给出示例代码,这部分可能需要参考ph.c的修改,比如在put函数里加锁。接着讨论可能存在的问题,比如锁的粒度太粗导致性能问题,或者需要更细粒度的锁,比如每个哈希桶一个锁。 另外,用户可能需要了解实验结果,比如未加锁时结果错误,加锁后正确但性能不佳,这时候如何优化。可能引用[3]中提到的时间差距大,所以需要分析原因,比如锁的竞争激烈,或者哈希函数设计不合理导致冲突多。 最后,生成相关的问题,比如如何调试线程同步问题,或者xv6中其他同步机制如信号量的使用。这些可以帮助用户进一步学习。</think>### xv6 线程同步作业实现与示例分析 #### 一、作业背景 在xv6的线程同步作业中(如引用[3]所述),学生需要通过修改`ph.c`代码实现线程安全的哈希表操作。核心目标是通过锁机制解决多线程竞争问题,同时观察单线程/多线程性能差异。 #### 二、实现步骤 1. **声明锁变量数组** ```c pthread_mutex_t locks[NBUCKET]; // 每个哈希桶对应一个锁 ``` 2. **初始化锁数组** ```c int main() { for (int i = 0; i < NBUCKET; i++) pthread_mutex_init(&locks[i], NULL); // ...启动线程 } ``` 3. **插入操作加锁** ```c void put(int key, int value) { int idx = hash(key); pthread_mutex_lock(&locks[idx]); // 获取对应桶的锁 insert(key, value, &table[idx], table[idx]); pthread_mutex_unlock(&locks[idx]); } ``` #### 三、实验结果分析(引用[3]) 未加锁时多线程运行结果: ``` 1: put time = 0.003632 0: put time = 0.003681 1: 0 keys missing 0: 0 keys missing completion time = 0.003836 // 双线程未正确同步 ``` 加锁后改进结果: ``` 1: put time = 0.006963 0: put time = 0.007013 1: 0 keys missing 0: 0 keys missing completion time = 0.007196 // 正确但性能下降 ``` #### 四、关键问题与优化方向 1. **锁粒度问题**:全局锁会导致线程阻塞,应采用细粒度锁(如每个哈希桶独立锁) 2. **哈希函数设计**:不均匀的分布会导致锁竞争加剧 3. **性能调优**:可通过减少临界区范围(如仅保护链表操作)提升并发度 #### 五、完整代码示例 ```c // 在struct entry前声明锁数组 #define NBUCKET 5 pthread_mutex_t locks[NBUCKET]; // 初始化代码段 for (int i = 0; i < NBUCKET; i++) { pthread_mutex_init(&locks[i], NULL); } // 修改后的put函数 void put(int key, int value) { int idx = key % NBUCKET; pthread_mutex_lock(&locks[idx]); struct entry *e = malloc(sizeof(struct entry)); e->key = key; e->value = value; e->next = table[idx]; table[idx] = e; pthread_mutex_unlock(&locks[idx]); } ``` #### 六、扩展思考 实验结果中的时间差距表明(引用[3]),即使正确实现了锁机制,仍需考虑: - 锁竞争与CPU核心数的关系 - 伪共享(False Sharing)对缓存的影响 - 无锁数据结构(Lock-free)的可行性
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值