Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One core exclusively for my process [duplicate]

Possible Duplicate:
how to set CPU affinity of a particular pthread?

Is there a way in Linux to disable one core for all processes except one process? I would like to have one core reserved only and only for my process.

Expected behavior is as follows:

  1. Processes which will be spawned after my process, should not see this core and use the others.
  2. When my process is spawned, all processes which are utilizing this core, should be switched to other cores.
like image 802
Kornel Szymkiewicz Avatar asked Jan 30 '12 22:01

Kornel Szymkiewicz


People also ask

Can multiple processes run on one core?

Short answer, yes. A single core cpu(a processor), can run 2 or more threads simultaneously. These threads may belong to the one program, or they may belong different programs and thus processes. This type of multithreading is called Simultaneous MultiThreading(SMT).

How many cores is a process using?

As a general rule, 1 process only uses 1 core. Actually, 1 thread can only be executed by 1 core. If you have a dual core processor, it is literally 2 CPUs stuck together in the same pc. These are called physical processors.

How do I dedicate a core to a single program in Linux?

If you want to prevent this and dedicate a whole CPU core to a particular program, you can use "isolcpus" kernel parameter, which allows you to reserve the CPU core during boot. Add the kernel parameter "isolcpus=" to the boot loader during boot or GRUB configuration file.

Can two processes run simultaneously?

A multitasking operating system may just switch between processes to give the appearance of many processes executing simultaneously (that is, in parallel), though in fact only one process can be executing at any one time on a single CPU (unless the CPU has multiple cores, then multithreading or other similar ...


1 Answers

Yes, there is. You want to create two cpusets, one with your isolated CPU and the other with all the rest of the CPUs. Assign your special process to the isolated cpuset and all the rest of the processes to the other cpuset.

Here is a simple example script that will do it:

mkdir /cpuset  mount -t cpuset none /cpuset/ cd /cpuset  mkdir sys                                   # create sub-cpuset for system processes /bin/echo 0-2 > sys/cpuset.cpus             # assign cpus (cores) 0-2 to this set                                             # adjust if you have more/less cores /bin/echo 1 > sys/cpuset.cpu_exclusive /bin/echo 0 > sys/cpuset.mems       mkdir rt                                    # create sub-cpuset for my process /bin/echo 3 > rt/cpuset.cpus                # assign cpu (core) 3 to this cpuset                                             # adjust this to number of cores-1 /bin/echo 1 > rt/cpuset.cpu_exclusive /bin/echo 0 > rt/cpuset.mems /bin/echo 0 > rt/cpuset.sched_load_balance /bin/echo 1 > rt/cpuset.mem_hardwall  # move all processes from the default cpuset to the sys-cpuset for T in `cat tasks`; do echo "Moving " $T; /bin/echo $T > sys/tasks; done 

Now start your process and find out its PID and go:

/bin/echo $PID > /cpuset/rt/tasks 

If you want to revert these changes, just restart your system or do:

# move tasks back from sys-cpuset to root cpuset for T in `cat /cpuset/sys/tasks`; do echo "Moving " $T; /bin/echo $T > /cpuset/tasks; done # remove sys-cpuset rmdir /cpuset/sys # move tasks back from rt-cpuset to root cpuset for T in `cat /cpuset/rt/tasks`; do echo "Moving " $T; /bin/echo $T > /cpuset/tasks; done # remove rt-cpuset rmdir /cpuset/rt # unmount and remove /cpuset umount /cpuset rmdir /cpuset 

Here is the man page: http://www.kernel.org/doc/man-pages/online/pages/man7/cpuset.7.html

There are also more complicated shell wrappers that can help you automate this, such as cset. See: http://web.archive.org/web/20120428093126/http://www.suse.com/documentation/slerte_11/slerte_tutorial/data/slerte_tutorial.html

like image 191
gby Avatar answered Sep 18 '22 00:09

gby