加入收藏 | 设为首页 | 会员中心 | 我要投稿 云计算网_泰州站长网 (http://www.0523zz.com/)- 视觉智能、AI应用、CDN、行业物联网、智能数字人!
当前位置: 首页 > 服务器 > 搭建环境 > Windows > 正文

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

发布时间:2019-10-12 01:44:21 所属栏目:Windows 来源:Android资深架构师
导读:副标题#e# 我其实并不想讨论微内核的概念,也并不擅长去阐述概念,这是百科全书的事,但无奈最近由于鸿蒙的发布导致这个话题过火,也就经不住诱惑,加上我又一直比较喜欢操作系统这个话题,就来个老生常谈吧。 说起微内核,其性能往往因为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.  struct timeb t; 
  18.  ftime(&t); 
  19.  return 1000 * t.time + t.millitm; 
  20.   
  21. struct node { 
  22.  struct node *next; 
  23.  void *data; 
  24. }; 
  25.   
  26. void print_result() 
  27.  printf("%dn", total); 
  28.  exit(0); 
  29.   
  30. struct node *head = NULL; 
  31. struct node *current = NULL;  
  32. void insert(struct node *node) 
  33.  node->data = NULL; 
  34.  node->next = head; 
  35.  head = node; 
  36.   
  37. struct node* delete() 
  38.  struct node *tempLink = head; 
  39.   
  40.  head = head->next; 
  41.   
  42.  return tempLink; 
  43.   
  44. int empty() 
  45.  return head == NULL; 
  46.   
  47. static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 
  48.   
  49. static pthread_spinlock_t spin; 
  50.   
  51. int add_task() 
  52.  struct node *tsk = (struct node*) malloc(sizeof(struct node)); 
  53.   
  54.  pthread_spin_lock(&spin); 
  55.  if (timer || curr < count) { 
  56.  curr ++; 
  57.  insert(tsk); 
  58.  } 
  59.  pthread_spin_unlock(&spin); 
  60.   
  61.  return curr; 
  62.   
  63. // 强度可以调整,比如0xff->0xffff,CPU比较猛比较多的机器上做测试,将其调强些,否则队列开销会淹没模拟任务的开销。 
  64. void do_task() 
  65. {  
  66.  int i = 0, j = 2, k = 0; for (i = 0; i < 0xff; i++) { 
  67.  k += i/j; 
  68.  } 
  69.   
  70. void* func(void *arg) 
  71.  int ret; 
  72.   
  73.  while (1) { 
  74.  ret = add_task(); 
  75.  if (!timer && ret == count) { 
  76.  break; 
  77.  } 
  78.  } 
  79.   
  80. void* server_func(void *arg) 
  81.  while (timer || total != count) { 
  82.  struct node *tsk; 
  83.  pthread_spin_lock(&spin); 
  84.  if (empty()) { 
  85.  pthread_spin_unlock(&spin); 
  86.  continue; 
  87.  } 
  88.  if (timer && timer_start == 0) { 
  89.  struct itimerval tick = {0}; 
  90.  timer_start = 1; 
  91.  signal(SIGALRM, print_result); 
  92.  tick.it_value.tv_sec = 10; 
  93.  tick.it_value.tv_usec = 0; 
  94.  setitimer(ITIMER_REAL, &tick, NULL); 
  95.  } 
  96.  tsk = delete(); pthread_spin_unlock(&spin); 
  97.  do_task(); 
  98.   
  99.  free(tsk); 
  100.  total++; 
  101.  } end = gettime(); 
  102.   
  103.  printf("%lld %dn", end - start, total); 
  104.  exit(0); 
  105.   
  106. int main(int argc, char **argv) 
  107.  int err, i; 
  108.  int tcnt; 
  109.  pthread_t tid, stid; 
  110.   
  111.  count = atoi(argv[1]); 
  112.  tcnt = atoi(argv[2]); 
  113.  if (argc == 4) { 
  114.  timer = 1; 
  115.  } 
  116.   
  117.  pthread_spin_init(&spin, PTHREAD_PROCESS_PRIVATE); 
  118.  // 创建服务线程 
  119.  err = pthread_create(&stid, NULL, server_func, NULL); 
  120.  if (err != 0) { 
  121.  exit(1); 
  122.  } 
  123.  start = gettime(); 
  124.  // 创建工作线程 
  125.  for (i = 0; i < tcnt; i++) { 
  126.  err = pthread_create(&tid, NULL, func, NULL); 
  127.  if (err != 0) { 
  128.  exit(1); 
  129.  } 
  130.  } 
  131.  sleep(3600); 
  132.   
  133.  return 0; 

(编辑:云计算网_泰州站长网)

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

热点阅读