linux 下进程和线程指定CPU运行
2015-12-27 22:35
691 查看
大概的介绍一下linux 的指定CPU运行,包括进程和线程,这个只是最基本的方法,看一下基本就会了,至于其他的进程间通信和线程同步的话,这里暂不做任何介绍。
算了,还是比较整体的介绍一下如何去学习这个吧。
linux下的top命令是可以查看当前的cpu的运行状态,具体参数自己去查,这里只介绍与标题相关的,按1可以查看系统有多少个CPU,以及每个CPU的运行状态。可是如何查看线程的CPU呢???top -Hp pid,pid就是你当前程序的进程号,如果是多线程的话,是可以查看进程内所有线程的CPU和内存使用情况。
pstree可以查看主次线程,同样的pstree -p pid。可以查看进程的线程情况。
taskset这个其实才是重点,可以查看以及设置当前进程或线程运行的CPU(设置亲和力)。
taskset -pc pid,查看当前进程的cpu,当然有的时候不只是一个,taskset -pc cpu_num pid ,cpu_num就是设置的cpu。
这样的话基本的命令和操作其实大家都知道了,接下来就是在代码中完成这些操作,并通过命令去验证代码的成功率。
进程制定CPU运行:
#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/sysinfo.h>
#include<unistd.h>
#define __USE_GNU
#include<sched.h>
#include<ctype.h>
#include<string.h>
int main(int argc, char* argv[])
{
//sysconf获取有几个CPU
int num = sysconf(_SC_NPROCESSORS_CONF);
int created_thread = 0;
int myid;
int i;
int j = 0;
//原理其实很简单,就是通过cpu_set_t进行位与操作
cpu_set_t mask;
cpu_set_t get;
if (argc != 2)
{
printf("usage : ./cpu num\n");
exit(1);
}
myid = atoi(argv[1]);
printf("system has %i processor(s). \n", num);
//先进行清空,然后设置掩码
CPU_ZERO(&mask);
CPU_SET(myid, &mask);
//设置进程的亲和力
if (sched_setaffinity(0, sizeof(mask), &mask) == -1)
{
printf("warning: could not set CPU affinity, continuing...\n");
}
while (1)
{
CPU_ZERO(&get);
//获取当前进程的亲和力
if (sched_getaffinity(0, sizeof(get), &get) == -1)
{
printf("warning: cound not get cpu affinity, continuing...\n");
}
for (i = 0; i < num; i++)
{
if (CPU_ISSET(i, &get))
{
printf("this process %d is running processor : %d\n",getpid(), i);
}
}
}
return 0;
}
进程设置CPU运行,其实只能是单线程。多线程设定CPU如下:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sched.h>
void *myfun(void *arg)
{
cpu_set_t mask;
cpu_set_t get;
char buf[256];
int i;
int j;
//同样的先去获取CPU的个数
int num = sysconf(_SC_NPROCESSORS_CONF);
printf("system has %d processor(s)\n", num);
for (i = 0; i < num; i++) {
CPU_ZERO(&mask);
CPU_SET(i, &mask);
//这个其实和设置进程的亲和力基本是一样的
if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) {
fprintf(stderr, "set thread affinity failed\n");
}
CPU_ZERO(&get);
if (pthread_getaffinity_np(pthread_self(), sizeof(get), &get) < 0) {
fprintf(stderr, "get thread affinity failed\n");
}
for (j = 0; j < num; j++)
{
if (CPU_ISSET(j, &get))
{
printf("thread %d is running in processor %d\n", (int)pthread_self(), j);
}
}
j = 0;
while (j++ < 100000000) {
memset(buf, 0, sizeof(buf));
}
}
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
pthread_t tid;
if (pthread_create(&tid, NULL, (void *)myfun, NULL) != 0)
{
fprintf(stderr, "thread create failed\n");
return -1;
}
pthread_join(tid, NULL);
return 0;
}
2015年12月27日23:04:37
算了,还是比较整体的介绍一下如何去学习这个吧。
linux下的top命令是可以查看当前的cpu的运行状态,具体参数自己去查,这里只介绍与标题相关的,按1可以查看系统有多少个CPU,以及每个CPU的运行状态。可是如何查看线程的CPU呢???top -Hp pid,pid就是你当前程序的进程号,如果是多线程的话,是可以查看进程内所有线程的CPU和内存使用情况。
pstree可以查看主次线程,同样的pstree -p pid。可以查看进程的线程情况。
taskset这个其实才是重点,可以查看以及设置当前进程或线程运行的CPU(设置亲和力)。
taskset -pc pid,查看当前进程的cpu,当然有的时候不只是一个,taskset -pc cpu_num pid ,cpu_num就是设置的cpu。
这样的话基本的命令和操作其实大家都知道了,接下来就是在代码中完成这些操作,并通过命令去验证代码的成功率。
进程制定CPU运行:
#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/sysinfo.h>
#include<unistd.h>
#define __USE_GNU
#include<sched.h>
#include<ctype.h>
#include<string.h>
int main(int argc, char* argv[])
{
//sysconf获取有几个CPU
int num = sysconf(_SC_NPROCESSORS_CONF);
int created_thread = 0;
int myid;
int i;
int j = 0;
//原理其实很简单,就是通过cpu_set_t进行位与操作
cpu_set_t mask;
cpu_set_t get;
if (argc != 2)
{
printf("usage : ./cpu num\n");
exit(1);
}
myid = atoi(argv[1]);
printf("system has %i processor(s). \n", num);
//先进行清空,然后设置掩码
CPU_ZERO(&mask);
CPU_SET(myid, &mask);
//设置进程的亲和力
if (sched_setaffinity(0, sizeof(mask), &mask) == -1)
{
printf("warning: could not set CPU affinity, continuing...\n");
}
while (1)
{
CPU_ZERO(&get);
//获取当前进程的亲和力
if (sched_getaffinity(0, sizeof(get), &get) == -1)
{
printf("warning: cound not get cpu affinity, continuing...\n");
}
for (i = 0; i < num; i++)
{
if (CPU_ISSET(i, &get))
{
printf("this process %d is running processor : %d\n",getpid(), i);
}
}
}
return 0;
}
进程设置CPU运行,其实只能是单线程。多线程设定CPU如下:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sched.h>
void *myfun(void *arg)
{
cpu_set_t mask;
cpu_set_t get;
char buf[256];
int i;
int j;
//同样的先去获取CPU的个数
int num = sysconf(_SC_NPROCESSORS_CONF);
printf("system has %d processor(s)\n", num);
for (i = 0; i < num; i++) {
CPU_ZERO(&mask);
CPU_SET(i, &mask);
//这个其实和设置进程的亲和力基本是一样的
if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) {
fprintf(stderr, "set thread affinity failed\n");
}
CPU_ZERO(&get);
if (pthread_getaffinity_np(pthread_self(), sizeof(get), &get) < 0) {
fprintf(stderr, "get thread affinity failed\n");
}
for (j = 0; j < num; j++)
{
if (CPU_ISSET(j, &get))
{
printf("thread %d is running in processor %d\n", (int)pthread_self(), j);
}
}
j = 0;
while (j++ < 100000000) {
memset(buf, 0, sizeof(buf));
}
}
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
pthread_t tid;
if (pthread_create(&tid, NULL, (void *)myfun, NULL) != 0)
{
fprintf(stderr, "thread create failed\n");
return -1;
}
pthread_join(tid, NULL);
return 0;
}
2015年12月27日23:04:37
相关文章推荐
- linux下opencv的安装
- linux解压缩基本命令使用
- linux 获取本机的所有IP地址
- CentOS6.5搭建本地yum仓库
- 嵌入式Linux系统开发学习步骤
- Linux进程及作业管理
- linux 信号处理机制简介
- linux命令总结(1)
- Linxu笔记(11): Linux系统管理常用命令
- Linux 笔记...文件和目录属性useradd、userdel、usermod 、passwd
- CentOS修改主机名hostname
- linux命令笔记su sudo df du
- 【Linux 驱动】Netfilter/iptables (八) Netfilter的NAT机制
- CentOS7下yum安装SAMBA全命令过程
- Strom安装流程
- 在CentOS 7上利用systemctl添加自定义系统服务
- ZooKeeper集群部署流程
- 8.linux文件相关命令
- Linux下 C++遍历目录下所有文件
- 嵌入式Linux操作系统简介