sl@0: /* sl@0: * Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * A variant of "Hello World" which reports various bits of its environment sl@0: * and returns an interesting exit status sl@0: * sl@0: * sl@0: */ sl@0: sl@0: sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include "CTEST.H" sl@0: sl@0: #ifdef __WINS__ sl@0: int triangle_recurse(char* prog, char* num, int progress) sl@0: { sl@0: printf("\nBut not under WINS where I'll be told 0\n"); sl@0: return 0; sl@0: } sl@0: sl@0: int triangle_parallel(char* prog, char* num) sl@0: { sl@0: printf("\nBut not under WINS where I'll be told 0\n"); sl@0: return 0; sl@0: } sl@0: sl@0: #else sl@0: sl@0: int triangle_recurse(char* prog, char* num, int progress) sl@0: { sl@0: int n; sl@0: int pid, ret; sl@0: char cmd[100]; sl@0: int fids[3]; sl@0: sl@0: n = atol(num); sl@0: if (n<1) sl@0: return 0; sl@0: sl@0: sprintf(cmd, "%s do_triangle %d", prog, n-1); sl@0: if (progress) sl@0: { sl@0: pid=popen3(cmd, "", 0, fids); sl@0: if (pid<0) sl@0: { sl@0: fprintf(stderr, "Executing %s, ", cmd); sl@0: perror("popen3 failed"); sl@0: return -1999999; sl@0: } sl@0: while (1) sl@0: { sl@0: int n=waitpid(pid, &ret, WNOHANG); sl@0: if (n==pid) sl@0: break; sl@0: printf("."); sl@0: fflush(stdout); sl@0: sleep(1); sl@0: } sl@0: } sl@0: else sl@0: ret=system(cmd); sl@0: return n+ret; sl@0: } sl@0: sl@0: int triangle_parallel(char* prog, char* num) sl@0: { sl@0: int n; sl@0: int pid, pid1, pid2, ret1, ret2, base, split; sl@0: char cmd[100]; sl@0: int fids1[3], fids2[3]; sl@0: char* basep; sl@0: sl@0: n = atol(num); sl@0: if (n<1) sl@0: return 0; sl@0: basep=getenv("TRIANGLE_BASE"); sl@0: if (basep==0) sl@0: return 0; sl@0: base=atol(basep); sl@0: sl@0: /* we have to add up the numbers base..n inclusive sl@0: */ sl@0: if (base==n) sl@0: return n; sl@0: if (base+1==n) sl@0: return base+n; sl@0: sl@0: /* At least 3 numbers, so split it into subtasks for child processes sl@0: */ sl@0: split = (n-base)/2; sl@0: sl@0: sprintf(cmd, "%s do_trianglep %d", prog, base+split); sl@0: pid1=popen3(cmd, "", 0, fids1); sl@0: if (pid1<0) sl@0: { sl@0: fprintf(stderr, "Doing %d..%d of %d..%d", base+split, base, n, base); sl@0: perror("popen3 failed"); sl@0: return -1999999; sl@0: } sl@0: sl@0: sprintf(cmd, "%d", base+split+1); sl@0: setenv("TRIANGLE_BASE", cmd, 1); sl@0: sl@0: sprintf(cmd, "%s do_trianglep %d", prog, n); sl@0: pid2=popen3(cmd, "", 0, fids2); sl@0: if (pid2<0) sl@0: { sl@0: fprintf(stderr, "Doing %d..%d of %d..%d", n, base+split+1, n, base); sl@0: perror("popen3 failed"); sl@0: return -1999999; sl@0: } sl@0: sl@0: /* Now collect the results */ sl@0: ret1=-10000; sl@0: ret2=-10000; sl@0: for (n=0; n<2; n++) sl@0: { sl@0: int ret=-3999999; sl@0: pid=wait(&ret); sl@0: if (pid<0) sl@0: { sl@0: perror("waitpid failed"); sl@0: return -2999999; sl@0: } sl@0: if (pid==pid1) sl@0: { sl@0: pid1=-1; sl@0: ret1=ret; sl@0: } sl@0: else if (pid==pid2) sl@0: { sl@0: pid2=-1; sl@0: ret2=ret; sl@0: } sl@0: else sl@0: printf("Unexpected pid %d (not %d or %d)\n", pid, pid1, pid2); sl@0: } sl@0: return ret1+ret2; sl@0: } sl@0: sl@0: #endif sl@0: sl@0: /** sl@0: Usage: THELLOU [triangle[p] ] sl@0: sl@0: @SYMTestCaseID SYSLIB-STDLIB-CT-1058 sl@0: @SYMTestCaseDesc Tests for printing to standard output stream sl@0: @SYMTestPriority High sl@0: @SYMTestActions Prints some stuff (looks at args, getenv("USER"), cwd) to stdout and stderr. sl@0: Will compute triangle numbers by calling itself recursively, or parallel subdivison sl@0: @SYMTestExpectedResults Test must not fail sl@0: @SYMREQ REQ0000 sl@0: */ sl@0: int main (int argc, char *argv[]) sl@0: { sl@0: char *user=getenv("USER"); sl@0: char cwd[MAXPATHLEN]; sl@0: int ret=3, i; sl@0: sl@0: start_redirection_server(); sl@0: sl@0: if (argc==3 && strcmp(argv[1],"do_triangle")==0) sl@0: return triangle_recurse(argv[0], argv[2], 0); sl@0: sl@0: if (argc==3 && strcmp(argv[1],"do_trianglep")==0) sl@0: return triangle_parallel(argv[0], argv[2]); sl@0: sl@0: if (user) sl@0: { sl@0: printf("Hello %s\n", user); sl@0: ret=4; sl@0: } sl@0: else sl@0: printf("Greetings.\n"); sl@0: sl@0: printf("I am process %d\n", getpid()); sl@0: sl@0: if (getcwd(cwd,sizeof(cwd))!=0) sl@0: printf("I am speaking to you from %s\n", cwd); sl@0: sl@0: printf("I have %d arguments: ", argc); sl@0: for (i=0; i%s< ", argv[i]); sl@0: printf("\r\n"); sl@0: sl@0: printf("In a few moments I shall say %c to stderr:\n", 'a'+argc); sl@0: fflush(stdout); sl@0: sl@0: fprintf(stderr, "%c\n", 'a'+argc); sl@0: fflush(stderr); sl@0: sl@0: if (argc==3 && strcmp(argv[1],"triangle")==0) sl@0: { sl@0: printf("For my next trick I shall compute the triangle of %s: ", argv[2]); sl@0: fflush(stdout); sl@0: ret=triangle_recurse(argv[0], argv[2], 1); sl@0: printf("it's %d\n", ret); sl@0: } sl@0: sl@0: if (argc==3 && strcmp(argv[1],"trianglep")==0) sl@0: { sl@0: printf("I shall now compute the triangle of %s using a parallel algorithm: ", argv[2]); sl@0: fflush(stdout); sl@0: setenv("TRIANGLE_BASE", "1", 0); sl@0: ret=triangle_parallel(argv[0], argv[2]); sl@0: printf("it's %d\n", ret); sl@0: } sl@0: sl@0: printf("Farewell...\nPress a key"); sl@0: getchar(); sl@0: return ret; sl@0: }