|
| 1 | +/* |
| 2 | + * This code forks 10 children and checks that those run on the same |
| 3 | + * CPU core as the parent. Since we're normally doing CPU pinning, |
| 4 | + * this should be true and the program generates a WRONG-ANWSER. |
| 5 | + * If there's a mismatch the program will trigger a RUNTIME-ERROR. |
| 6 | + * |
| 7 | + * @EXPECTED_RESULTS@: WRONG-ANSWER |
| 8 | + */ |
| 9 | + |
| 10 | +#include <errno.h> |
| 11 | +#include <unistd.h> |
| 12 | +#include <sched.h> |
| 13 | +#include <stdio.h> |
| 14 | +#include <string.h> |
| 15 | +#include <sys/wait.h> |
| 16 | + |
| 17 | +extern int errno; |
| 18 | + |
| 19 | +int main() |
| 20 | +{ |
| 21 | + unsigned int parent_cpu, cpu; |
| 22 | + getcpu(&parent_cpu, NULL); |
| 23 | + printf("Parent on CPU %d\n", parent_cpu); |
| 24 | + fflush(NULL); |
| 25 | + |
| 26 | + for(int i=0; i<10; i++) { |
| 27 | + if ( fork()==0 ) { |
| 28 | + // We're in the child |
| 29 | + getcpu(&cpu, NULL); |
| 30 | + printf("Child %d on CPU %d\n", i, cpu); |
| 31 | + fflush(NULL); |
| 32 | + if ( cpu!=parent_cpu ) { |
| 33 | + printf("CPU MISMATCH!\n"); |
| 34 | + fflush(NULL); |
| 35 | + return 1; |
| 36 | + } |
| 37 | + return 0; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + // Wait for all our children |
| 42 | + do { |
| 43 | + pid_t pid; |
| 44 | + int status; |
| 45 | + pid = wait(&status); |
| 46 | + if ( pid==-1 ) { |
| 47 | + if ( errno==ECHILD ) { |
| 48 | + printf("Done waiting for all children\n"); |
| 49 | + } else { |
| 50 | + printf("Error in wait: %s\n", strerror(errno)); |
| 51 | + } |
| 52 | + fflush(NULL); |
| 53 | + break; |
| 54 | + } |
| 55 | + if ( pid>0 ) { |
| 56 | + if ( !WIFEXITED(status) ) { |
| 57 | + printf("Child pid %d terminated abnormally\n", (int)pid); |
| 58 | + fflush(NULL); |
| 59 | + return 1; |
| 60 | + } |
| 61 | + if ( WEXITSTATUS(status)!=0 ) { |
| 62 | + printf("Child pid %d terminated with exit status %d\n", |
| 63 | + (int)pid, (int)WEXITSTATUS(status)); |
| 64 | + return 1; |
| 65 | + } |
| 66 | + printf("Child pid %d terminated\n", (int)pid); |
| 67 | + fflush(NULL); |
| 68 | + } |
| 69 | + } while ( 1 ); |
| 70 | + |
| 71 | + return 0; |
| 72 | +} |
0 commit comments