os/ossrv/genericopenlibs/cstdlib/TSTLIB/THELLOU.C
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/TSTLIB/THELLOU.C	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,228 @@
     1.4 +/*
     1.5 +* Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description:
    1.18 +* A variant of "Hello World" which reports various bits of its environment
    1.19 +* and returns an interesting exit status
    1.20 +* 
    1.21 +*
    1.22 +*/
    1.23 +
    1.24 +
    1.25 +
    1.26 +#include <stdio.h>
    1.27 +#include <stdlib.h>
    1.28 +#include <unistd.h>
    1.29 +#include <string.h>
    1.30 +#include <sys/wait.h>
    1.31 +#include "CTEST.H"
    1.32 +
    1.33 +#ifdef __WINS__
    1.34 +int triangle_recurse(char* prog, char* num, int progress)
    1.35 +	{
    1.36 +	printf("\nBut not under WINS where I'll be told 0\n");
    1.37 +	return 0;
    1.38 +	}
    1.39 +
    1.40 +int triangle_parallel(char* prog, char* num)
    1.41 +	{
    1.42 +	printf("\nBut not under WINS where I'll be told 0\n");
    1.43 +	return 0;
    1.44 +	}
    1.45 +
    1.46 +#else
    1.47 +
    1.48 +int triangle_recurse(char* prog, char* num, int progress)
    1.49 +	{
    1.50 +	int n;
    1.51 +	int pid, ret;
    1.52 +	char cmd[100];
    1.53 +	int fids[3];
    1.54 +
    1.55 +	n = atol(num);
    1.56 +	if (n<1)
    1.57 +		return 0;
    1.58 +
    1.59 +	sprintf(cmd, "%s do_triangle %d", prog, n-1);
    1.60 +	if (progress)
    1.61 +		{
    1.62 +		pid=popen3(cmd, "", 0, fids);
    1.63 +		if (pid<0)
    1.64 +			{
    1.65 +			fprintf(stderr, "Executing %s, ", cmd);
    1.66 +			perror("popen3 failed");
    1.67 +			return -1999999;
    1.68 +			}
    1.69 +		while (1)
    1.70 +			{
    1.71 +			int n=waitpid(pid, &ret, WNOHANG);
    1.72 +			if (n==pid)
    1.73 +				break;
    1.74 +			printf(".");
    1.75 +			fflush(stdout);
    1.76 +			sleep(1);
    1.77 +			}
    1.78 +		}
    1.79 +	else
    1.80 +		ret=system(cmd);
    1.81 +	return n+ret;
    1.82 +	}
    1.83 +
    1.84 +int triangle_parallel(char* prog, char* num)
    1.85 +	{
    1.86 +	int n;
    1.87 +	int pid, pid1, pid2, ret1, ret2, base, split;
    1.88 +	char cmd[100];
    1.89 +	int fids1[3], fids2[3];
    1.90 +	char* basep;
    1.91 +
    1.92 +	n = atol(num);
    1.93 +	if (n<1)
    1.94 +		return 0;
    1.95 +	basep=getenv("TRIANGLE_BASE");
    1.96 +	if (basep==0)
    1.97 +		return 0;
    1.98 +	base=atol(basep);
    1.99 +
   1.100 +	/* we have to add up the numbers base..n inclusive
   1.101 +	 */
   1.102 +	if (base==n)
   1.103 +		return n;
   1.104 +	if (base+1==n)
   1.105 +		return base+n;
   1.106 +
   1.107 +	/* At least 3 numbers, so split it into subtasks for child processes
   1.108 +	 */
   1.109 +	split = (n-base)/2;
   1.110 +
   1.111 +	sprintf(cmd, "%s do_trianglep %d", prog, base+split);
   1.112 +	pid1=popen3(cmd, "", 0, fids1);
   1.113 +	if (pid1<0)
   1.114 +		{
   1.115 +		fprintf(stderr, "Doing %d..%d of %d..%d", base+split, base, n, base);
   1.116 +		perror("popen3 failed");
   1.117 +		return -1999999;
   1.118 +		}
   1.119 +	
   1.120 +	sprintf(cmd, "%d", base+split+1);
   1.121 +	setenv("TRIANGLE_BASE", cmd, 1);
   1.122 +
   1.123 +	sprintf(cmd, "%s do_trianglep %d", prog, n);
   1.124 +	pid2=popen3(cmd, "", 0, fids2);
   1.125 +	if (pid2<0)
   1.126 +		{
   1.127 +		fprintf(stderr, "Doing %d..%d of %d..%d", n, base+split+1, n, base);
   1.128 +		perror("popen3 failed");
   1.129 +		return -1999999;
   1.130 +		}
   1.131 +	
   1.132 +	/* Now collect the results */
   1.133 +	ret1=-10000;
   1.134 +	ret2=-10000;
   1.135 +	for (n=0; n<2; n++)
   1.136 +		{
   1.137 +		int ret=-3999999;
   1.138 +		pid=wait(&ret);
   1.139 +		if (pid<0)
   1.140 +			{
   1.141 +			perror("waitpid failed");
   1.142 +			return -2999999;
   1.143 +			}
   1.144 +		if (pid==pid1)
   1.145 +			{
   1.146 +			pid1=-1;
   1.147 +			ret1=ret;
   1.148 +			}
   1.149 +		else if (pid==pid2)
   1.150 +			{
   1.151 +			pid2=-1;
   1.152 +			ret2=ret;
   1.153 +			}
   1.154 +		else
   1.155 +			printf("Unexpected pid %d (not %d or %d)\n", pid, pid1, pid2);
   1.156 +		}
   1.157 +	return ret1+ret2;
   1.158 +	}
   1.159 +
   1.160 +#endif
   1.161 +
   1.162 +/**
   1.163 +Usage:  THELLOU [triangle[p] <n>]
   1.164 +
   1.165 +@SYMTestCaseID          SYSLIB-STDLIB-CT-1058
   1.166 +@SYMTestCaseDesc	    Tests for printing to standard output stream
   1.167 +@SYMTestPriority 	    High
   1.168 +@SYMTestActions  	    Prints some stuff (looks at args, getenv("USER"), cwd) to stdout and stderr.
   1.169 +						Will compute triangle numbers by calling itself recursively, or parallel subdivison
   1.170 +@SYMTestExpectedResults Test must not fail
   1.171 +@SYMREQ                 REQ0000
   1.172 +*/		
   1.173 +int main (int argc, char *argv[])
   1.174 +	{
   1.175 +	char *user=getenv("USER");
   1.176 +	char cwd[MAXPATHLEN];
   1.177 +	int ret=3, i;
   1.178 +
   1.179 +	start_redirection_server();
   1.180 +	
   1.181 +	if (argc==3 && strcmp(argv[1],"do_triangle")==0)
   1.182 +		return triangle_recurse(argv[0], argv[2], 0);
   1.183 +
   1.184 +	if (argc==3 && strcmp(argv[1],"do_trianglep")==0)
   1.185 +		return triangle_parallel(argv[0], argv[2]);
   1.186 +
   1.187 +	if (user)
   1.188 +		{
   1.189 +		printf("Hello %s\n", user);
   1.190 +		ret=4;
   1.191 +		}
   1.192 +	else
   1.193 +		printf("Greetings.\n");
   1.194 +
   1.195 +	printf("I am process %d\n", getpid());
   1.196 +
   1.197 +	if (getcwd(cwd,sizeof(cwd))!=0)
   1.198 +		printf("I am speaking to you from %s\n", cwd);
   1.199 +
   1.200 +	printf("I have %d arguments: ", argc);
   1.201 +	for (i=0; i<argc; i++)
   1.202 +		printf(">%s< ", argv[i]);
   1.203 +	printf("\r\n");
   1.204 +
   1.205 +	printf("In a few moments I shall say %c to stderr:\n", 'a'+argc);
   1.206 +	fflush(stdout);
   1.207 +
   1.208 +	fprintf(stderr, "%c\n", 'a'+argc);
   1.209 +	fflush(stderr);
   1.210 +
   1.211 +	if (argc==3 && strcmp(argv[1],"triangle")==0)
   1.212 +		{
   1.213 +		printf("For my next trick I shall compute the triangle of %s: ", argv[2]);
   1.214 +		fflush(stdout);
   1.215 +		ret=triangle_recurse(argv[0], argv[2], 1);
   1.216 +		printf("it's %d\n", ret);
   1.217 +		}
   1.218 +
   1.219 +	if (argc==3 && strcmp(argv[1],"trianglep")==0)
   1.220 +		{
   1.221 +		printf("I shall now compute the triangle of %s using a parallel algorithm: ", argv[2]);
   1.222 +		fflush(stdout);
   1.223 +		setenv("TRIANGLE_BASE", "1", 0);
   1.224 +		ret=triangle_parallel(argv[0], argv[2]);
   1.225 +		printf("it's %d\n", ret);
   1.226 +		}
   1.227 +
   1.228 +	printf("Farewell...\nPress a key");
   1.229 +	getchar();
   1.230 +	return ret;
   1.231 +	}