This case create NB_CPU-1 children processes to verify that the
child process which we asume to preempt the parent will run on
the same CPU with its parent.
But, it cann't make thing goes well always, some time it goes to
what you don't like.
So, just mask the CPU use set_affinity(), make sure parent and
child can always run on the same CPU.
[Note: this patch moves the non portable affinity code into common
header that needs to be ported to your favorite UNIX]
Signed-off-by: Wanlong Gao <[email protected]>
---
.../conformance/interfaces/sched_setparam/9-1.c | 79 ++++++--------------
1 files changed, 22 insertions(+), 57 deletions(-)
diff --git
a/testcases/open_posix_testsuite/conformance/interfaces/sched_setparam/9-1.c
b/testcases/open_posix_testsuite/conformance/interfaces/sched_setparam/9-1.c
index 349999c..811a80c 100644
--- a/testcases/open_posix_testsuite/conformance/interfaces/sched_setparam/9-1.c
+++ b/testcases/open_posix_testsuite/conformance/interfaces/sched_setparam/9-1.c
@@ -31,6 +31,7 @@
* not, the test fail.
*/
+#define _GNU_SOURCE
#include <sched.h>
#include <stdio.h>
#include <signal.h>
@@ -40,6 +41,7 @@
#include <stdlib.h>
#include <errno.h>
#include "posixtest.h"
+#include "affinity.h"
#ifdef BSD
# include <sys/types.h>
@@ -81,23 +83,16 @@ static int get_ncpu(void)
return ncpu;
}
-static void child_process(void)
+static void test_process(void)
{
- struct sched_param param;
-
- param.sched_priority = sched_get_priority_max(SCHED_FIFO);
- if (sched_setparam(getpid(), ¶m) != 0) {
- perror("An error occurs when calling sched_setparam()");
+ int rc;
+#if __linux__
+ rc = set_affinity(0);
+ if (rc < 0) {
+ perror("An error occurs when calling set_affinity()");
return;
}
-
- /* to avoid blocking */
- alarm(2);
- while (1);
-}
-
-static void test_process(void)
-{
+#endif
/* to avoid blocking */
alarm(2);
@@ -107,28 +102,18 @@ static void test_process(void)
}
}
-static void kill_children(int *child_pid)
-{
- int i;
-
- for (i = 0; i < nb_cpu; i++)
- kill(child_pid[i], SIGTERM);
-}
-
int main(void)
{
- int *child_pid, oldcount, newcount, shm_id, i, j;
+ int child_pid, oldcount, newcount, shm_id, i, j, rc = 0;
struct sched_param param;
key_t key;
-
- nb_cpu = get_ncpu();
-
- if (nb_cpu == -1) {
- printf("Can not get the number of CPUs of your machines.\n");
+#if __linux__
+ rc = set_affinity(0);
+ if (rc < 0) {
+ perror("An error occurs when calling set_affinity()");
return PTS_UNRESOLVED;
}
-
- child_pid = malloc(nb_cpu * sizeof(int));
+#endif
key = ftok("conformance/interfaces/sched_setparam/9-1.c",1234);
shm_id = shmget(key, sizeof(int), IPC_CREAT|0600);
@@ -155,31 +140,11 @@ int main(void)
return PTS_UNRESOLVED;
}
- for (i = 0; i < (nb_cpu - 1); i++) {
- child_pid[i] = fork();
- if (child_pid[i] == -1) {
- perror("An error occurs when calling fork()");
- for (j = 0; j < i; j++) {
- kill(child_pid[j], SIGTERM);
- }
- return PTS_UNRESOLVED;
- } else if (child_pid[i] == 0) {
-
- child_process();
-
- printf("This code should not be executed.\n");
- return PTS_UNRESOLVED;
- }
- }
-
- child_pid[i] = fork();
- if (child_pid[i] == -1) {
+ child_pid = fork();
+ if (child_pid == -1) {
perror("An error occurs when calling fork()");
- for (j = 0; j < i; j++) {
- kill(child_pid[j], SIGTERM);
- }
return PTS_UNRESOLVED;
- } else if (child_pid[i] == 0) {
+ } else if (child_pid == 0) {
test_process();
@@ -193,20 +158,20 @@ int main(void)
sched_get_priority_max(SCHED_FIFO)) / 2;
oldcount = *shmptr;
- if (sched_setparam(child_pid[i], ¶m) != 0) {
+ if (sched_setparam(child_pid, ¶m) != 0) {
perror("An error occurs when calling sched_setparam()");
- kill_children(child_pid);
+ kill(child_pid, SIGTERM);
return PTS_UNRESOLVED;
}
newcount = *shmptr;
if (newcount == oldcount) {
printf("The target process does not preempt the calling
process\n");
- kill_children(child_pid);
+ kill(child_pid, SIGTERM);
return PTS_FAIL;
}
printf("Test PASSED\n");
- kill_children(child_pid);
+ kill(child_pid, SIGTERM);
return PTS_PASS;
}
--
1.7.6
------------------------------------------------------------------------------
Get a FREE DOWNLOAD! and learn more about uberSVN rich system,
user administration capabilities and model configuration. Take
the hassle out of deploying and managing Subversion and the
tools developers use with it. http://p.sf.net/sfu/wandisco-d2d-2
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list