Process 进程间预定义的抢占点
我划分了许多子流程,并为每个子流程分配了优先级和核心。进程A以3秒的时间段执行,进程B以6秒的时间段执行。我希望它们以这样一种方式执行,即高优先级进程应该只在预定义点抢占低优先级进程,并尝试使用信号量实现。我在这两个进程中使用了相同的代码片段,在这两个进程中使用了不同的数组值 “bubblesort_desc()”按降序对数组排序并打印它。”bubblesort_asc()'按升序排序并打印Process 进程间预定义的抢占点,process,fork,semaphore,Process,Fork,Semaphore,我划分了许多子流程,并为每个子流程分配了优先级和核心。进程A以3秒的时间段执行,进程B以6秒的时间段执行。我希望它们以这样一种方式执行,即高优先级进程应该只在预定义点抢占低优先级进程,并尝试使用信号量实现。我在这两个进程中使用了相同的代码片段,在这两个进程中使用了不同的数组值 “bubblesort_desc()”按降序对数组排序并打印它。”bubblesort_asc()'按升序排序并打印 while(a<3) { printf("time in sort1.c: %d %ld\n"
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");
}
当一组排序列表正在打印时,一个进程不应该抢占另一个进程。但它的工作原理就像没有信号灯一样。我根据以下链接定义了信号量:
有人能告诉我是什么原因吗?有比信号量更好的替代方法吗?它的printf导致输出不明确。如果打印结果时没有“\n”,则会得到更准确的结果。但对于实时应用程序,最好避免使用printf语句。我使用trace cmd和kernelshark来可视化进程的行为。如何显示代码的相关部分,以便人们告诉您如何修复它/为您提供替代方案?
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