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 ?