predefined preemption points among processes

烈酒焚心 提交于 2019-12-11 19:36:50

问题


I have forked many child processes and assigned priority and core to each of them. Porcess A executes at period of 3 sec and process B at a period of 6 sec. I want them to execute in such a way that the higher priority processes should preempt lower priority ones only at predefined points and tried to acheive it with semaphores. I have used this same code snippets within the 2 processes with different array values in both.

'bubblesort_desc()' sorts the array in descending order and prints it. 'bubblesort_asc()' sorts in ascending order and prints.

while(a<3)  
{
printf("time in sort1.c: %d %ld\n", (int)request.tv_sec, (long int)request.tv_nsec);
int array[SIZE] = {5, 1, 6 ,7 ,9};

semaphore_wait(global_sem);
     bubblesort_desc(array, SIZE);
semaphore_post(global_sem);

semaphore_wait(global_sem);
    bubblesort_asc(array, SIZE); 
semaphore_post(global_sem);

semaphore_wait(global_sem);     
a++;

request.tv_sec = request.tv_sec + 6;
request.tv_nsec = request.tv_nsec; //if i add 1ms here like an offset to the lower priority one, it works.  

semaphore_post(global_sem);
semaphore_close(global_sem);    //close the semaphore file

//sleep for the rest of the time after the process finishes execution until the period of 6
clk = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &request, NULL);
if (clk != 0 && clk != EINTR)
    printf("ERROR: clock_nanosleep\n");

}

I get the output like this whenever two processes get activated at the same time. For example at time units of 6, 12,..

time in sort1.c: 10207 316296689
time now in sort.c: 10207 316296689
9
99
7
100
131
200
256
6
256
200
5
131
100
99
1
1
5
6
7
9

One process is not supposed to preempt the other while one set of sorted list is printing. But it's working as if there are no semaphores. I defined semaphores as per this link: http://linux.die.net/man/3/pthread_mutexattr_init

Can anyone tell me what can be the reason for that? Is there a better alternative than semaphores?


回答1:


Its printf that's causing the ambiguous output. If the results are printed without '\n' then we get a more accurate result. But its always better to avoid printf statements for real time applications. I used trace-cmd and kernelshark to visualize the behaviour of the processes.



来源:https://stackoverflow.com/questions/17910755/predefined-preemption-points-among-processes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!