나는 많은 하위 프로세스를 분기하고 각 프로세스에 우선 순위와 핵심을 할당했습니다. Porcess A는 3 초의 주기로 실행되고 B는 6 초의 주기로 실행됩니다. 우선 순위가 높은 프로세스가 우선 순위가 낮은 프로세스를 미리 정의 된 지점에서만 선점하고 세마포를 사용하여 프로세스를 수행하도록합니다. 두 개의 다른 배열 값을 가진 두 프로세스 내에서 동일한 코드 스 니펫을 사용했습니다.프로세스 중 미리 정의 된 선점
'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
누구나 그 이유가 무엇인지 말해 줄 수 있습니까? 세마포어보다 나은 대안이 있습니까?
사람들이 코드를 수정하고 대안을 제공하는 방법을 알려줄 수 있도록 코드의 관련 부분을 표시하는 것은 어떻습니까? – Mat