加入收藏 | 设为首页 | 会员中心 | 我要投稿 安阳站长网 (https://www.0372zz.cn/)- 高性能计算、分布式云、混合云存储、云计算、视频终端!
当前位置: 首页 > 服务器 > 搭建环境 > Windows > 正文

被神话的Linux, 一文带你看清Linux在多核可扩展性设计上的不足

发布时间:2019-10-11 09:52:51 所属栏目:Windows 来源:Android资深架构师
导读:我其实并不想讨论微内核的概念,也并不擅长去阐述概念,这是百科全书的事,但无奈最近由于鸿蒙的发布导致这个话题过火,也就经不住诱惑,加上我又一直比较喜欢操作系统这个话题,就来个老生常谈吧。 说起微内核,其性能往往因为IPC饱受诟

相对的,微内核采用将请求通过IPC发送到专门的服务进程,模拟代码如下:

  1. #include <pthread.h> 
  2. #include <signal.h> 
  3. #include <stdio.h> 
  4. #include <unistd.h> 
  5. #include <stdlib.h> 
  6. #include <errno.h> 
  7. #include <sys/time.h> 
  8. static int count = 0; 
  9. static int curr = 0; 
  10.   
  11. long long end, start; 
  12. int timer = 0; 
  13. int timer_start = 0; 
  14. static int total = 0; 
  15.   
  16. long long gettime() 
  17. { 
  18.  struct timeb t; 
  19.  ftime(&t); 
  20.  return 1000 * t.time + t.millitm; 
  21. } 
  22.   
  23. struct node { 
  24.  struct node *next; 
  25.  void *data; 
  26. }; 
  27.   
  28. void print_result() 
  29. { 
  30.  printf("%dn", total); 
  31.  exit(0); 
  32. } 
  33.   
  34. struct node *head = NULL; 
  35. struct node *current = NULL;  
  36. void insert(struct node *node) 
  37. { 
  38.  node->data = NULL; 
  39.  node->next = head; 
  40.  head = node; 
  41. } 
  42.   
  43. struct node* delete() 
  44. { 
  45.  struct node *tempLink = head; 
  46.   
  47.  head = head->next; 
  48.   
  49.  return tempLink; 
  50. } 
  51.   
  52. int empty() 
  53. { 
  54.  return head == NULL; 
  55. } 
  56.   
  57. static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 
  58.   
  59. static pthread_spinlock_t spin; 
  60.   
  61. int add_task() 
  62. { 
  63.  struct node *tsk = (struct node*) malloc(sizeof(struct node)); 
  64.   
  65.  pthread_spin_lock(&spin); 
  66.  if (timer || curr < count) { 
  67.  curr ++; 
  68.  insert(tsk); 
  69.  } 
  70.  pthread_spin_unlock(&spin); 
  71.   
  72.  return curr; 
  73. } 
  74.   
  75. // 强度可以调整,比如0xff->0xffff,CPU比较猛比较多的机器上做测试,将其调强些,否则队列开销会淹没模拟任务的开销。 
  76. void do_task() 
  77. {  
  78.  int i = 0, j = 2, k = 0; for (i = 0; i < 0xff; i++) { 
  79.  k += i/j; 
  80.  } 
  81. } 
  82.   
  83. void* func(void *arg) 
  84. { 
  85.  int ret; 
  86.   
  87.  while (1) { 
  88.  ret = add_task(); 
  89.  if (!timer && ret == count) { 
  90.  break; 
  91.  } 
  92.  } 
  93. } 
  94.   
  95. void* server_func(void *arg) 
  96. { 
  97.  while (timer || total != count) { 
  98.  struct node *tsk; 
  99.  pthread_spin_lock(&spin); 
  100.  if (empty()) { 
  101.  pthread_spin_unlock(&spin); 
  102.  continue; 
  103.  } 
  104.  if (timer && timer_start == 0) { 
  105.  struct itimerval tick = {0}; 
  106.  timer_start = 1; 
  107.  signal(SIGALRM, print_result); 
  108.  tick.it_value.tv_sec = 10; 
  109.  tick.it_value.tv_usec = 0; 
  110.  setitimer(ITIMER_REAL, &tick, NULL); 
  111.  } 
  112.  tsk = delete(); pthread_spin_unlock(&spin); 
  113.  do_task(); 
  114.   
  115.  free(tsk); 
  116.  total++; 
  117.  } end = gettime(); 
  118.   
  119.  printf("%lld %dn", end - start, total); 
  120.  exit(0); 
  121. } 
  122.   
  123. int main(int argc, char **argv) 
  124. { 
  125.  int err, i; 
  126.  int tcnt; 
  127.  pthread_t tid, stid; 
  128.   
  129.  count = atoi(argv[1]); 
  130.  tcnt = atoi(argv[2]); 
  131.  if (argc == 4) { 
  132.  timer = 1; 
  133.  } 
  134.   
  135.  pthread_spin_init(&spin, PTHREAD_PROCESS_PRIVATE); 
  136.  // 创建服务线程 
  137.  err = pthread_create(&stid, NULL, server_func, NULL); 
  138.  if (err != 0) { 
  139.  exit(1); 
  140.  } 
  141.  start = gettime(); 
  142.  // 创建工作线程 
  143.  for (i = 0; i < tcnt; i++) { 
  144.  err = pthread_create(&tid, NULL, func, NULL); 
  145.  if (err != 0) { 
  146.  exit(1); 
  147.  } 
  148.  } 
  149.  sleep(3600); 
  150.   
  151.  return 0; 
  152. } 

(编辑:安阳站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读