进程之间的预定义抢占点 [英] predefined preemption points among processes

查看:100
本文介绍了进程之间的预定义抢占点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我分叉了许多子进程,并为每个子进程分配了优先级和核心.流程A的执行时间为3秒,流程B的执行时间为6秒.我希望它们以这样一种方式执行:较高优先级的进程应仅在预定义的点抢占较低优先级的进程,并尝试通过信号量来实现.我在两个进程中都使用了相同的代码片段,但两个代码中的数组值都不相同.

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()'对数组进行降序排序并打印. "bubblesort_asc()"以升序排序并打印.

'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");

}

无论何时同时激活两个进程,我都会得到这样的输出.例如,以6、12,..

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

在打印一组排序的列表时,一个进程不应该抢占另一个进程.但这似乎没有信号灯.我根据此链接定义了信号灯: http://linux.die.net/man/3/pthread_mutexattr_init

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?

推荐答案

它的printf导致输出模棱两可.如果打印的结果中没有'\ n',那么我们将获得更准确的结果.但是,对于实时应用程序,最好避免使用printf语句.我使用trace-cmd和kernelshark可视化了进程的行为.

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.

这篇关于进程之间的预定义抢占点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆