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_affinity
Will 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 );