Allocate cpu cores to processes

Source: Internet
Author: User

New programs adopt producer and consumer models. The producer speed is very fast, data is almost in the memory, and processing is very fast. Consumers need frequent I/O. Therefore, we plan to allocate different cores to producers and consumers.

The producer only needs one core, and the rest is allocated to the consumer.

I checked this information and the shell side has the taskset command available.

Taskset:

-P: The pid of the running process

-The specified cpu is displayed in the c list.

-V version

-H help

 

Usage: I. e.

# Taskset-cp 1, 2, 3 4321

# Taskset-c 1, 2, 3-5/usr/bin/XXX

 

Linux provides two functions

  • sched_set_affinity()(Used to modify the bit mask)
  • sched_get_affinity()(Used to view the current bit mask)

cpu_affinityWill be passed to the sub-thread, so it should be called as appropriatesched_set_affinity

Each core corresponds to a mask, and the corresponding bit is set to 1

 

Set CPU affinity for each thread)

  cpu_set_t mask;   /* CPU_ZERO initializes all the bits in the mask to zero. */        CPU_ZERO( &mask );   /* CPU_SET sets only the bit corresponding to cpu. */        CPU_SET( created_thread, &mask );   /* sched_setaffinity returns 0 in success */        if( sched_setaffinity( 0, sizeof(mask), &mask ) == -1 )   {      printf("WARNING: Could not set CPU Affinity, continuing...\n");   }

Obtain the number of system Processors

Int NUM_PROCS = sysconf (_ SC _NPROCESSORS_CONF );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.