【发布时间】:2013-07-28 17:19:07
【问题描述】:
我已经分叉了许多子进程,并为每个子进程分配了优先级和核心。进程 A 以 3 秒的周期执行,进程 B 的周期为 6 秒。我希望它们以这样一种方式执行,即较高优先级的进程应该仅在预定义的点抢占较低优先级的进程,并尝试使用信号量来实现它。我在两个进程中使用了相同的代码 sn-ps,两个进程中的数组值不同。
'bubblesort_desc()' 以降序对数组进行排序并打印出来。 'bubblesort_asc()' 按升序排序并打印。
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、..
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
谁能告诉我这是什么原因?有比信号量更好的选择吗?
【问题讨论】:
-
如何显示代码的相关部分,以便人们告诉您如何修复它/为您提供替代方案?