genericopenlibs/cstdlib/TSTLIB/THELLOU.C
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /*
       
     2 * Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 * A variant of "Hello World" which reports various bits of its environment
       
    16 * and returns an interesting exit status
       
    17 * 
       
    18 *
       
    19 */
       
    20 
       
    21 
       
    22 
       
    23 #include <stdio.h>
       
    24 #include <stdlib.h>
       
    25 #include <unistd.h>
       
    26 #include <string.h>
       
    27 #include <sys/wait.h>
       
    28 #include "CTEST.H"
       
    29 
       
    30 #ifdef __WINS__
       
    31 int triangle_recurse(char* prog, char* num, int progress)
       
    32 	{
       
    33 	printf("\nBut not under WINS where I'll be told 0\n");
       
    34 	return 0;
       
    35 	}
       
    36 
       
    37 int triangle_parallel(char* prog, char* num)
       
    38 	{
       
    39 	printf("\nBut not under WINS where I'll be told 0\n");
       
    40 	return 0;
       
    41 	}
       
    42 
       
    43 #else
       
    44 
       
    45 int triangle_recurse(char* prog, char* num, int progress)
       
    46 	{
       
    47 	int n;
       
    48 	int pid, ret;
       
    49 	char cmd[100];
       
    50 	int fids[3];
       
    51 
       
    52 	n = atol(num);
       
    53 	if (n<1)
       
    54 		return 0;
       
    55 
       
    56 	sprintf(cmd, "%s do_triangle %d", prog, n-1);
       
    57 	if (progress)
       
    58 		{
       
    59 		pid=popen3(cmd, "", 0, fids);
       
    60 		if (pid<0)
       
    61 			{
       
    62 			fprintf(stderr, "Executing %s, ", cmd);
       
    63 			perror("popen3 failed");
       
    64 			return -1999999;
       
    65 			}
       
    66 		while (1)
       
    67 			{
       
    68 			int n=waitpid(pid, &ret, WNOHANG);
       
    69 			if (n==pid)
       
    70 				break;
       
    71 			printf(".");
       
    72 			fflush(stdout);
       
    73 			sleep(1);
       
    74 			}
       
    75 		}
       
    76 	else
       
    77 		ret=system(cmd);
       
    78 	return n+ret;
       
    79 	}
       
    80 
       
    81 int triangle_parallel(char* prog, char* num)
       
    82 	{
       
    83 	int n;
       
    84 	int pid, pid1, pid2, ret1, ret2, base, split;
       
    85 	char cmd[100];
       
    86 	int fids1[3], fids2[3];
       
    87 	char* basep;
       
    88 
       
    89 	n = atol(num);
       
    90 	if (n<1)
       
    91 		return 0;
       
    92 	basep=getenv("TRIANGLE_BASE");
       
    93 	if (basep==0)
       
    94 		return 0;
       
    95 	base=atol(basep);
       
    96 
       
    97 	/* we have to add up the numbers base..n inclusive
       
    98 	 */
       
    99 	if (base==n)
       
   100 		return n;
       
   101 	if (base+1==n)
       
   102 		return base+n;
       
   103 
       
   104 	/* At least 3 numbers, so split it into subtasks for child processes
       
   105 	 */
       
   106 	split = (n-base)/2;
       
   107 
       
   108 	sprintf(cmd, "%s do_trianglep %d", prog, base+split);
       
   109 	pid1=popen3(cmd, "", 0, fids1);
       
   110 	if (pid1<0)
       
   111 		{
       
   112 		fprintf(stderr, "Doing %d..%d of %d..%d", base+split, base, n, base);
       
   113 		perror("popen3 failed");
       
   114 		return -1999999;
       
   115 		}
       
   116 	
       
   117 	sprintf(cmd, "%d", base+split+1);
       
   118 	setenv("TRIANGLE_BASE", cmd, 1);
       
   119 
       
   120 	sprintf(cmd, "%s do_trianglep %d", prog, n);
       
   121 	pid2=popen3(cmd, "", 0, fids2);
       
   122 	if (pid2<0)
       
   123 		{
       
   124 		fprintf(stderr, "Doing %d..%d of %d..%d", n, base+split+1, n, base);
       
   125 		perror("popen3 failed");
       
   126 		return -1999999;
       
   127 		}
       
   128 	
       
   129 	/* Now collect the results */
       
   130 	ret1=-10000;
       
   131 	ret2=-10000;
       
   132 	for (n=0; n<2; n++)
       
   133 		{
       
   134 		int ret=-3999999;
       
   135 		pid=wait(&ret);
       
   136 		if (pid<0)
       
   137 			{
       
   138 			perror("waitpid failed");
       
   139 			return -2999999;
       
   140 			}
       
   141 		if (pid==pid1)
       
   142 			{
       
   143 			pid1=-1;
       
   144 			ret1=ret;
       
   145 			}
       
   146 		else if (pid==pid2)
       
   147 			{
       
   148 			pid2=-1;
       
   149 			ret2=ret;
       
   150 			}
       
   151 		else
       
   152 			printf("Unexpected pid %d (not %d or %d)\n", pid, pid1, pid2);
       
   153 		}
       
   154 	return ret1+ret2;
       
   155 	}
       
   156 
       
   157 #endif
       
   158 
       
   159 /**
       
   160 Usage:  THELLOU [triangle[p] <n>]
       
   161 
       
   162 @SYMTestCaseID          SYSLIB-STDLIB-CT-1058
       
   163 @SYMTestCaseDesc	    Tests for printing to standard output stream
       
   164 @SYMTestPriority 	    High
       
   165 @SYMTestActions  	    Prints some stuff (looks at args, getenv("USER"), cwd) to stdout and stderr.
       
   166 						Will compute triangle numbers by calling itself recursively, or parallel subdivison
       
   167 @SYMTestExpectedResults Test must not fail
       
   168 @SYMREQ                 REQ0000
       
   169 */		
       
   170 int main (int argc, char *argv[])
       
   171 	{
       
   172 	char *user=getenv("USER");
       
   173 	char cwd[MAXPATHLEN];
       
   174 	int ret=3, i;
       
   175 
       
   176 	start_redirection_server();
       
   177 	
       
   178 	if (argc==3 && strcmp(argv[1],"do_triangle")==0)
       
   179 		return triangle_recurse(argv[0], argv[2], 0);
       
   180 
       
   181 	if (argc==3 && strcmp(argv[1],"do_trianglep")==0)
       
   182 		return triangle_parallel(argv[0], argv[2]);
       
   183 
       
   184 	if (user)
       
   185 		{
       
   186 		printf("Hello %s\n", user);
       
   187 		ret=4;
       
   188 		}
       
   189 	else
       
   190 		printf("Greetings.\n");
       
   191 
       
   192 	printf("I am process %d\n", getpid());
       
   193 
       
   194 	if (getcwd(cwd,sizeof(cwd))!=0)
       
   195 		printf("I am speaking to you from %s\n", cwd);
       
   196 
       
   197 	printf("I have %d arguments: ", argc);
       
   198 	for (i=0; i<argc; i++)
       
   199 		printf(">%s< ", argv[i]);
       
   200 	printf("\r\n");
       
   201 
       
   202 	printf("In a few moments I shall say %c to stderr:\n", 'a'+argc);
       
   203 	fflush(stdout);
       
   204 
       
   205 	fprintf(stderr, "%c\n", 'a'+argc);
       
   206 	fflush(stderr);
       
   207 
       
   208 	if (argc==3 && strcmp(argv[1],"triangle")==0)
       
   209 		{
       
   210 		printf("For my next trick I shall compute the triangle of %s: ", argv[2]);
       
   211 		fflush(stdout);
       
   212 		ret=triangle_recurse(argv[0], argv[2], 1);
       
   213 		printf("it's %d\n", ret);
       
   214 		}
       
   215 
       
   216 	if (argc==3 && strcmp(argv[1],"trianglep")==0)
       
   217 		{
       
   218 		printf("I shall now compute the triangle of %s using a parallel algorithm: ", argv[2]);
       
   219 		fflush(stdout);
       
   220 		setenv("TRIANGLE_BASE", "1", 0);
       
   221 		ret=triangle_parallel(argv[0], argv[2]);
       
   222 		printf("it's %d\n", ret);
       
   223 		}
       
   224 
       
   225 	printf("Farewell...\nPress a key");
       
   226 	getchar();
       
   227 	return ret;
       
   228 	}