Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix copyright headers
[simgrid.git] / buildtools / Cmake / prog_test / prog_AC_CHECK_MCSC.c
1 /* Copyright (c) 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <ucontext.h>
10
11 void child(void);
12
13 ucontext_t uc_child;
14 ucontext_t uc_main;
15
16
17
18 void child(void)
19 {
20     if (swapcontext(&uc_child, &uc_main) != 0)
21         exit(2);
22 }
23
24 int main(int argc, char *argv[])
25 {
26     FILE *fp;
27     void *stack;
28
29     /* the default is that it fails */
30     if ((fp = fopen("conftestval", "w")) == NULL)
31         exit(3);
32     fprintf(fp, "no\n");
33     fclose(fp);
34
35     /* configure a child user-space context */
36     if ((stack = malloc(64*1024)) == NULL)
37         exit(4);
38     if (getcontext(&uc_child) != 0)
39         exit(5);
40     uc_child.uc_link = NULL;
41     uc_child.uc_stack.ss_sp = (char *)stack+(32*1024);
42     uc_child.uc_stack.ss_size = 32*1024;
43     uc_child.uc_stack.ss_flags = 0;
44     makecontext(&uc_child, child, 0);
45
46     /* switch into the user context */
47     if (swapcontext(&uc_main, &uc_child) != 0)
48         exit(6);
49
50     /* Fine, child came home */
51     if ((fp = fopen("conftestval", "w")) == NULL)
52         exit(7);
53     fprintf(fp, "yes\n");
54     fclose(fp);
55
56     /* die successfully */
57     exit(0);
58         return 1;
59 }