如何确保代码在指定线程上调用 sched_setaffinity() 运行 之后立即等待 cpu 队列中的下一回合?

How to ensure Code immediatly after a call to sched_setaffinity() run on the specified thread before waiting for next turn at the cpu queue?

除了 parent 之外,我还编写了一个代码来创建 7 个进程,所以总和是 8 ... -i7 ..有 4 个核心 X 2 threads/core = 8 个线程...现在我的问题是如何确保在调用 sched_setaffinity() 之后进程将继续 运行ning在指定的处理器上,不会等到指定 cpu 的队列中的下一个回合?我们可以有一些像

这样的东西吗

get_me_out_of_the_current_queue_ so_that_the_sched_puts_me_in_the_ specified_queue_next_time()

我的代码是:

       #define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <sched.h>
#include <sys/shm.h>

int main()
{
   //code here in parent only , before any child start ///


   /////Declaration section////////
   pid_t ProcessIDs[8];
   cpu_set_t ThreadArray[8];
   int i ;
   int j ;
   int k ;
   int InternalPID;
   ////////////////////////////////


   /////Initialization section/////
   i=1;
   j=0;
   k=0;
   InternalPID = 0;

   for (j=0;j<8;j++)
   {
   ProcessIDs[j] = 0;
   }

   for (j=0;j<8;j++)
   {
   CPU_ZERO(&ThreadArray[j]);
   }

   for (j=0;j<8;j++)
   {
   CPU_SET(j+1,&ThreadArray[j]);
   }
   /////////////////////////////////



///////// shm ///////////////////////////////
int  shmid ;
int err;
char *shm;
shmid = shmget(IPC_PRIVATE,8 ,IPC_CREAT | IPC_EXCL | 0666 );
shm=shmat(shmid,0, IPC_CREAT | IPC_EXCL | 0666  );
if (shmid > -1)
{
printf("shared memory created succefully\n");
}
int m =0;
for(m=0 ;m<8;m++)
{
    shm[m]='N';
}
//////////////////////////////////////////////



   /////// Parent only - children don't exist//////


   ProcessIDs[0] = getpid();
   while( (getpid() == ProcessIDs[0] ) & (i < 8) )
   {
   ProcessIDs[i] = fork();
   i++;
   }

    ////////////////////////////////////////////////

    ////////parent only - children exist////////////
    if(getpid() == ProcessIDs[0])
    {
    for(k=0;k<8;k++)
    {
    sched_setaffinity(ProcessIDs[k],sizeof(cpu_set_t),&ThreadArray[k]);
    shm[k] = 'Y';
    sleep(2);
    }
    }
    ////////////////////////////////////////////////


   ///////////////////parent and children////
   for(k=1;k<8;k++)
   {
    if(ProcessIDs[k] == 0){
    InternalPID = k;
    break;
    }
   }
   //////////////////////////////////////////////

   //////////Process Specific code //////////////

   if (InternalPID == 0)
   {
   ////// Parent only Rest of Code ////////
   while(shm[0] != 'Y');
   printf("hello for parent %i.. \n",InternalPID);
   return 0 ;
   ////////////////////////////////////////
   }
   else if (InternalPID == 1)
   {
   ////////////// child 1 /////////////////
   while(shm[1] != 'Y');
   printf("hello for child  %i.. \n", InternalPID);
   return 0 ;
   ////////////////////////////////////////
   }
   else if (InternalPID == 2)
   {
   ////////////// child 2 /////////////////
   while(shm[2] != 'Y');
   printf("hello for child  %i.. \n", InternalPID);
   return 0 ;
   ////////////////////////////////////////
   }
   else if (InternalPID == 3)
   {
   ////////////// child 3 /////////////////
   while(shm[3] != 'Y');
   printf("hello for child  %i.. \n", InternalPID);
   return 0 ;
   ////////////////////////////////////////
   }
   else if (InternalPID == 4)
   {
   ////////////// child 4 /////////////////
   while(shm[4] != 'Y');
   printf("hello for child  %i.. \n", InternalPID);
   return 0 ;
   ////////////////////////////////////////
   }
   else if (InternalPID == 5)
   {
   ////////////// child 5 /////////////////
   while(shm[5] != 'Y');
   printf("hello for child  %i.. \n", InternalPID);
   return 0 ;
   ////////////////////////////////////////
   }
   else if (InternalPID == 6)
   {
   ////////////// child 6 /////////////////
   while(shm[6] != 'Y');
   printf("hello for child  %i.. \n", InternalPID);
   ////////////////////////////////////////
   }
   else if (InternalPID == 7)
   {
   ////////////// child 7 /////////////////
   while(shm[7] != 'Y');
   printf("hello for child  %i.. \n", InternalPID);
   return 0 ;
   ////////////////////////////////////////
   }
   /////////////////////////////////////////////////




   ///////////////////////////////////////////////////////
}

我知道 while() 在每个进程特定代码的开头循环 (在 if 分支内)可能会保证,但我想这是由于时间延迟,这不是一个可靠的解决方案......请告诉我解决该问题的正确方法是什么......

第二点我想问一下:

8 个进程中的每个进程将 运行 在不同的核心上,但是在进程间通信期间,就像我继续并创建管道以便进程通过 parent 像中间点 "like a star topology" 如果 child 说 child 想要 communicate-during 它是会话-到 parent-when 它仍然在它的 cpu 队列中当前 运行ning 不像 childA ... OS 的作用是让所有八个进程感觉好像每个进程目前都只有 cpu ?

简而言之 -- 你不能,因为无论你要求什么核心,都可能已经忙于做其他事情。设置 affinity 所做的只是告诉 OS 你只希望在那个特定的 CPU 上安排进程。因此,实际上更有可能延迟您的任务,因为默认亲和力(任何 CPU)会将其安排在第一个可用的位置。

您可以提高进程优先级(即实时调度),这将导致进程抢占 CPU 上已经 运行 的任何内容,这可能更符合您的要求去做。

话虽这么说,但如果 "while" 循环永远不会结束,它们将陷入一个紧密的循环中,并且不会安排其他任何事情(即使是您的 shell ) 所以你将无法阻止他们。

my problem how could I ensure that after a call to sched_setaffinity() the process will continue running on the specified processor and will not wait till it's next turn in the specified cpu's queue ?

The docs 部分说:

If the process specified by pid is not currently running on one of the CPUs specified in mask, then that process is migrated to one of the CPUs specified in mask.

这不保证在该进程的当前时间片(如果有的话)到期之前会发生这样的迁移,也不保证它会在指定的 CPU 之一上立即开始 运行 s,但这没有实际意义,因为你怎么知道?

what happens if a child say child a wants to communicate-during it's session- to parent-when it's still in it's cpu queue not currently running like the childA ... is that the role of the OS to make all eight process feel as if each one is currently having the cpu alone ?

显然,当前未在 CPU 上调度的进程根本无法执行任何操作。事实上,它甚至无法想象(比喻地)想要做任何事情。但总的来说,是的,调解分时和 IPC 是 OS 的角色。

这与 CPU 亲和力或系统中物理执行单元的数量没有任何特别关系。所有这些都在单核机器上运行良好(尽管在这样的机器上设置 CPU affinity 是没有意义的)。