-2

I have written a code to make 7 process in addition to the parent, so the sum is 8 ... I managed to make each process tied to a different thread .. ie I have intel core-i7 ..which have 4 cores X 2 threads/core = 8 threads ... now 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 ?? could we have some thing like

get_me_out_of_the_current_queue_ so_that_the_sched_puts_me_in_the_ specified_queue_next_time()

my code is :

       #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 ;
   ////////////////////////////////////////
   }
   /////////////////////////////////////////////////




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

I know that the while() loops at the beginning of each process specific code (inside if branch) may guarantee that, but i guess that is due to the delay of time , which is not a robust solution ... please tell me what's the correct way to solve that ...

second point I'd like to ask about :

each process of the 8 processes will run on different core , but during interprocess communication like when i continue and create pipes so that the processes communicate through the parent like the middle point "like a star topology" 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 ?

2 Answers2

1

In short -- you can't, because whatever core you've asked for might already be busy doing something else. All that setting affinity does is tell the OS you only want the process scheduled on that particular CPU. As a result it is actually more likely to get your task delayed where the default affinity (any CPU) would schedule it on the first available.

You could raise the process priority (i.e. realtime scheduling), which would cause the process to preempt anything already running on the CPU, which might be more in line with what you were looking to do.

That being said, you risk locking up your system entirely, if the "while" loops never end they will be stuck in a tight loop and nothing else will get scheduled (not even your shell) so you won't be able to stop them.

Joe Hickey
  • 810
  • 7
  • 8
  • If i can skip current time slice after setting the affinity..ill garantee next time slice will be on the designated cpu ..even by coincidece initially it was running on the designated cpu...that case skip will be a wast of time but it will garantee next function call will be executed on the designated cpu..its a compromise between the skipped slice time and garantee that my rest of process code will run on the designated cpu..thats the point – AbdAllah Talaat Dec 20 '17 at 17:36
0

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 do say, in part:

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.

That does not promise that such a migration will happen before that process's current time slice (if it has one) expires, nor that it will immediately start running on one of the specified CPUs, but that's moot, because how would you be able to tell?

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 ?

Obviously, a process cannot do anything at all while it is not currently scheduled on a CPU. In fact, it cannot even conceive (metaphorically) a want to do anything. But overall, yes, it is the OS's role to mediate time sharing and IPC.

This hasn't anything in particular to do with CPU affinity or the number of physical execution units in the system. All of it works fine on a single-core machine (though setting CPU affinity is pointless on such a machine).

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • Thanks for you answer .. the main purpose of making separate process run on different cpu was not avoiding communication problems that may appear...the second question was a side affect i feared its happening ... but now consider my question .... is there a call to some function that after executed it gets the process skip its remaining time slice so that next time even after delay it executes on correct processor...i'l remove the while loops at that time...thnx – AbdAllah Talaat Dec 20 '17 at 16:45
  • I repeat: *how would you tell* whether the behavior is as you hope? The parent process sets the CPU affinity for a child process, and certainly the child's subsequent time slices are scheduled one the designated CPUs. But how does either process know whether the child was executing on a different CPU at the time the call was handled? You seem to be worrying about something that you cannot measure and that doesn't even matter. – John Bollinger Dec 20 '17 at 16:51
  • Even if the child was executing on the designated cpu by coincidence ... getting it one time ( even it was redundant ) but garantee it execute on next slice on the designated cpu...it will be fine .. i donot like to do check .. say getting it out of cpu3 queue and executing it next time slice on the same cpu3 which by coincidence is also cpu3 will not cause any problem... its a matter of comprimise between time and my target.. to place that function it can be in the branch after checking pid ... not common part executed by all process till reaching the ifelse ladder..that was my point – AbdAllah Talaat Dec 20 '17 at 17:06
  • May the post title be modified to reflect that : how do we get the process out of the cpu queue ..specifying which process is a matter of where in the code we write such function..in the begaining of the ifelsr ladder for each branch..also please note i made a a mask array not a single mask.. and note that which process gets which mask is an if condition inside while loop in the common part after fork.. icould do such block before forking so i had to stop processing by a shm till the parent set its mask in the sched ..when done i cant garantee opening shm will leave it on its cpu – AbdAllah Talaat Dec 20 '17 at 17:17
  • I repeat once more: *how would you tell?* I'm not saying that you should try to check, but rather suggesting that there is no way you could do so. It is pointless to be concerned about such behavior details if indeed they have no measurable effect. – John Bollinger Dec 20 '17 at 17:40
  • Thanks ... for your advice.. i was really misunderstanding what u was trying to say... i thought u wanted me to check first ....thnx – AbdAllah Talaat Dec 20 '17 at 17:59