Linux中,并行和并发的区别

并行:指的是在某一时刻能够同时执行多个进程。由于每核心CPU在某一时刻都只能执行一个进程,所以要同时执行多个进程,必须要使用多核CPU才能达到真正的并行。这里的同时,指的是同一时刻或并列的意思。

并发:指的是能够同时处理多个任务。这里的同时,指的不是同一时刻,更像是一种共存的含义。广义的并发指的是同时有多个任务到达,或者说,站在某个时间点去看,同时有多个任务存放在那里,这些任务都要被处理。

总结并行和并发:

1、并行描述的是能同时一次性处理多个任务的能力。并发描述的是有多个任务等待着要被处理这件事。

2、只有多核CPU才能实现真正的并行,才具有真正的高效率。单核CPU处理多进程通过进程切换实现,是伪并行,效率反而会比串行方式更低,因为进程切换会消耗资源和时间。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
计算圆周率是一个计算密集型任务,可以使用并行计算和超线程来提高计算效率。下面分别介绍并行计算、串行计算和超线程计算的方法。 1. 并行计算 并行计算是指将任务划分为多个子任务,每个子任务在不同的处理器上并行执行,最终将所有子任务的结果合并得到最终结果。在计算圆周率的过程,可以将生成随机点的任务划分为多个子任务,每个子任务在不同的线程并行执行。最后,将所有子任务的结果合并,得到最终的圆周率值。 下面是一个使用 Python 进行并行计算的例子: ```python import random from multiprocessing import Pool def generate_points(n): points_inside_circle = 0 for i in range(n): x, y = random.uniform(-1, 1), random.uniform(-1, 1) if x**2 + y**2 <= 1: points_inside_circle += 1 return points_inside_circle def estimate_pi_parallel(num_tasks, num_points_per_task): pool = Pool(processes=num_tasks) num_points_inside_circle = sum(pool.map(generate_points, [num_points_per_task] * num_tasks)) return 4 * num_points_inside_circle / (num_tasks * num_points_per_task) print(estimate_pi_parallel(4, 1000000)) ``` 这个例子,我们将任务划分为 4 个子任务,在 4 个进程并行执行。每个子任务生成 1000000 个随机点,统计落在内切圆内的点数,并返回最终的圆周率值。使用 `multiprocessing.Pool` 可以方便地创建进程池,并通过 `map` 方法将任务分配到不同的进程执行。 2. 串行计算 串行计算是指将任务顺序执行,每个任务执行完毕后再执行下一个任务。在计算圆周率的过程,可以顺序生成随机点,统计落在内切圆内的点数,最后计算圆周率值。 下面是一个使用 Python 进行串行计算的例子: ```python import random def estimate_pi_serial(num_points): num_points_inside_circle = 0 for i in range(num_points): x, y = random.uniform(-1, 1), random.uniform(-1, 1) if x**2 + y**2 <= 1: num_points_inside_circle += 1 return 4 * num_points_inside_circle / num_points print(estimate_pi_serial(4000000)) ``` 这个例子,我们顺序生成 4000000 个随机点,统计落在内切圆内的点数,并返回最终的圆周率值。 3. 超线程计算 超线程是指在单个物理处理器上模拟出多个逻辑处理器,从而提高计算效率。在计算圆周率的过程,可以利用超线程将多个线程调度到单个物理处理器上执行,从而提高计算效率。 下面是一个使用 Python 进行超线程计算的例子: ```python import random import threading class GeneratePointsThread(threading.Thread): def __init__(self, num_points): super().__init__() self.num_points = num_points self.num_points_inside_circle = 0 def run(self): for i in range(self.num_points): x, y = random.uniform(-1, 1), random.uniform(-1, 1) if x**2 + y**2 <= 1: self.num_points_inside_circle += 1 def estimate_pi_hyperthreading(num_threads, num_points_per_thread): threads = [GeneratePointsThread(num_points_per_thread) for i in range(num_threads)] for thread in threads: thread.start() for thread in threads: thread.join() num_points_inside_circle = sum([thread.num_points_inside_circle for thread in threads]) return 4 * num_points_inside_circle / (num_threads * num_points_per_thread) print(estimate_pi_hyperthreading(4, 1000000)) ``` 这个例子,我们创建 4 个线程,每个线程生成 1000000 个随机点,统计落在内切圆内的点数。使用 `threading.Thread` 可以方便地创建线程,并通过 `start` 和 `join` 方法启动和等待线程执行完毕。最后,将所有线程的结果合并,得到最终的圆周率值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一直在努力学习的菜鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值