我们都知道linux中创建新进程是系统调用fork,但实际上fork是clone功能的一部分,clone和fork的主要差别是传递了几个參数。clone隶属于libc。它的意义就是实现线程。

看一下clone函数:

int clone(int (*fn)(void * arg), void *stack, int flags, void * arg);

fn就是即将创建的线程要运行的函数,stack是线程使用的堆栈。

再来看一下clone和pthread_create的差别:linux中的pthread_create终于调用clone。

我们的目的不是为了介绍clone,而是探究clone中的上下文切换问题。

(1)进程切换:把执行的进程的CPU寄存器中的数据取出存放到内核态堆栈中,同一时候把要加载的进程的数据放入到寄存器中(硬件上下文)。还会把全部一切的状态信息进行切换。

(2)时间片轮转的方式使多个任务在同一颗CPU上运行变成了可能,但同一时候也带来了保存现场和载入现场的直接消耗(上下文切换会带来直接和间接两种因素影响程序性能的消耗。直接消耗包含:CPU寄存器须要保存和载入。系统调度器的代码须要运行,TLB实例须要又一次载入,CPU 的pipeline须要刷掉;间接消耗指的是多核的cache之间得共享数据。间接消耗对于程序的影响要看线程工作区操作数据的大小)。

(3)clone任务[1]:

  • Allocate data structures for thread representation
  • Initialize structures according to clone parameters
  • Set up kernel and user stack as well as argument for the thread function
  • Put the thread on the corresponding CPU core’s run queue
  • Notify target core via an interrupt so that the new thread will be scheduled

(4)我们在clone出线程时指定高的优先级,也许会降低因抢占而造成的上下文切花开销。

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <assert.h> #define N 4
#define M 30000 #define THREAD_NUM 4
#define POLICY SCHED_RR int nwait = 0;
volatile long long sum;
long loops = 6e3;
pthread_mutex_t mutex; void set_affinity(int core_id) {
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(core_id, &cpuset);
assert(pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset) == 0);
} void* thread_func(void *arg) {
//set_affinity((int)(long)arg);
for (int j = 0; j < M; j++) {
pthread_mutex_lock(&mutex);
nwait++;
for (long i = 0; i < loops; i++) // This is the key of speedup for parrot: the mutex needs to be a little bit congested.
sum += i;
pthread_mutex_unlock(&mutex);
for (long i = 0; i < loops; i++)
sum += i*i*i*i*i*i;
//fprintf(stderr, "compute thread %u %d\n", (unsigned)pthread_self(), sched_getcpu());
}
} int main() {
//set_affinity(23); pthread_t threads[THREAD_NUM], id;
pthread_attr_t attrs[THREAD_NUM];
struct sched_param scheds[THREAD_NUM], sched;
int idxs[THREAD_NUM];
int policy, i, ret; id = pthread_self();
ret = pthread_getschedparam(id, &policy, &sched);
assert(!ret && "main pthread_getschedparam failed!");
sched.sched_priority = sched_get_priority_max(POLICY);
ret = pthread_setschedparam(id, POLICY, &sched); //set policy and corresponding priority
assert(!ret && "main pthread_setschedparam failed!"); for (i = 0; i < THREAD_NUM; i++) {
idxs[i] = i; ret = pthread_attr_init(&attrs[i]);
assert(!ret && "pthread_attr_init failed!"); ret = pthread_attr_getschedparam(&attrs[i], &scheds[i]);
assert(!ret && "pthread_attr_getschedparam failed!"); ret = pthread_attr_setschedpolicy(&attrs[i], POLICY);
assert(!ret && "pthread_attr_setschedpolicy failed!"); scheds[i].sched_priority = sched_get_priority_max(POLICY); ret = pthread_attr_setschedparam(&attrs[i], &scheds[i]);
assert(!ret && "pthread_attr_setschedparam failed!"); ret = pthread_attr_setinheritsched(&attrs[i], PTHREAD_EXPLICIT_SCHED);
assert(!ret && "pthread_attr_setinheritsched failed!");
} for (i = 0; i < THREAD_NUM; i++) {
ret = pthread_create(&threads[i], &attrs[i], thread_func, &idxs[i]);
assert(!ret && "pthread_create() failed!");
} for (i = 0; i < THREAD_NUM; i++)
ret = pthread_join(threads[i], NULL); return 0;
}

我们让四个子线程和主线程都採取RR调度,并设置最高优先级,我们用VTune观察Preemption Context Switches是否会因此降低。

VTune现象:

如今设置最低优先级:

原来设置最低优先级能够降低Preemption Context Switches,可是添加了Synchronization Context Switches。

显然最高优先级执行用时少(4.470s,而最低优先级用时7.280s)。

REFERENCES:

[1] Balazs Gerofi, etc, Clone n(): Parallel Thread Creation for Upcoming Many-Core Architectures, 2012, IEEE International Conference on Cluster Computing.

具体解释clone函数的更多相关文章

  1. 实现一个clone函数,对javascript中的5种数据类型进行值复制

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 用三维的视角理解二维世界:完美解释meshgrid函数,三维曲面,等高线,看完你就懂了。...

    完美解释meshgrid函数,三维曲面,等高线 #用三维的视角理解二维世界 #完美解释meshgrid函数,三维曲面,等高线 import numpy as np import matplotlib. ...

  3. Linux Clone函数

    Linux Clone函数 之前某一次有过一次面试,问了内核中是怎么创建命名空间的? 下面就来扒一扒clone的精髓,以及如何通过它创建命名空间. 目录 Linux Clone函数 使用clone创建 ...

  4. js深入研究之扩展类,克隆对象,混合类(自定义的extend函数,clone函数,与augment函数)

    1.类扩展 /* EditInPlaceField类 */ /* 扩展函数 */ function extend(subClass, superClass) { var F = function() ...

  5. C++继承具体解释之二——派生类成员函数具体解释(函数隐藏、构造函数与兼容覆盖规则)

    在这一篇文章開始之前.我先解决一个问题. 在上一篇C++继承详解之中的一个--初探继承中,我提到了在派生类中能够定义一个与基类成员函数同名的函数,这样派生类中的函数就会覆盖掉基类的成员函数. 在谭浩强 ...

  6. 关于JS的clone()函数编写的一些问题

    问题讲述:用js 实现一个clone()克隆函数,该函数会把输入进去的不同类型值Number,String,Undefined,Boolean,Function,Null,Object,Array,R ...

  7. Swift具体解释之三----------函数(你想知道的都在这里)

    函数(你想知道的都在这里) 注:本文为作者自己总结.过于基础的就不再赘述 ,都是亲自測试的结果.如有错误或者遗漏的地方.欢迎指正.一起学习. 1. 函数的简单定义和调用 简单的无參函数就不再赘述 , ...

  8. clone函数

    http://blog.csdn.net/caianye/article/details/5947282 http://wenku.baidu.com/link?url=qnq7laYDYm1V8tl ...

  9. LoadRunner 函数大全之中文解释

    LoadRunner 函数大全之中文解释 // sapgui_table_set_column_selected 模拟用户 // 单击表中的列标题. int sapgui_table_set_colu ...

随机推荐

  1. android中用Intent传数据,如果用传递的是一个类,就将类实现Parcelable接口

    Parcelable,内存单位,跨进程使用,或者intent传递对象的时候使用.android中用Intent传数据,如果用传递的是一个对象,就将对象实现Parcelable接口,而不是将对象序列化. ...

  2. Pro ASP.NET Core MVC 第6版 第二章(后半章)

    增加动态输出 整个web应用平台的关注点在于构建并显示动态输出内容.在MVC里,控制器负责构建一些数据并将其传给视图.视图负责渲染成HTML. 从控制器向视图传递数据的一种方式是使用ViewBag 对 ...

  3. text-shadow的用法详解

    1.兼容性:text-shadow 和 box-shadow 这两个属性在主流现代浏览器上得到了很好的支持( > Chrome 4.0, > Firefox 3.5, > Safar ...

  4. [Windows Server 2012] SQL Server 备份和还原方法

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:SQL S ...

  5. JAVA环境变量配置后未变动配置失效处理

    环境: Windows 7 x64 配置方案来源于教程: http://www.mamicode.com/info-detail-563355.html 配置方案出现的问题: 正确配置JAVA环境变量 ...

  6. Hibernate框架之HQL查询与Criteria 查询的区别

    Hibernate框架提供了HQL查询和Criteria 查询.下面对这两种查询分别做个例子.也好对这两种查询方法有个大概的了解.就用房屋信息表做例子,查询所有房屋信息. HQL语句查询所有房屋信息: ...

  7. BackboneJS and UnderscoreJS

     介绍 来自API(backbone能做什么?) 当我们开发含有大量Javascript的web应用程序时,首先你需要做的事情之一便是停止向DOM对象附加数据. 通过复杂多变的jQuery选择符和回调 ...

  8. java正则表达式匹配字符

    假设要匹配${2}中间为数字的这个类型的变量String,则 Pattern p = Pattern.compile("\\$\\{\\d+\\}"); Matcher m = p ...

  9. MySQL学习笔记(十二)__连接查询(一)

    连接查询含义:又称多表查询,当查询的字段来自多个表时,就会用到连接查询 笛卡尔乘积现象:表1 有 m 行,表2 有 n 行,结果 = m*n 行发生原因:没有有效的连接条件如何避免:添加有效的连接条件 ...

  10. Java多线程学习笔记(四)——Thread类中方法介绍

    currentThread():返回代码正在被哪个线程调用. public class CurrentThreadWay { public static void main(String[] args ...