示例#1
0
文件: child.c 项目: cluosh/osue
/**
 * @brief Main child function
 * @details Exits after execution
 * @param id Number of the created child (starting with 1)
 * @param opts Parameters from command line, PRE: != null
 * @param ipipe Input pipe for a child process
 *        PRE: != null, size = 2 * int
 * @param opipe Output pipe for a child process
 *        PRE: != null, size = 2 * int
 */
void child_main(int id, struct options *opts, int *ipipe,
		int *opipe)
{
	char buff[SECRET_MAX];
	FILE *stream;

	/* Open input stream */
	stream = fdopen(ipipe[0], "r");
	if (stream == NULL)
		bail_out(EXIT_FAILURE, "fdopen");
	
	/* Read from input pipe */
	if (close(ipipe[1]) == -1)
		bail_out(EXIT_FAILURE, "close");
	if (quit == 0 && fread(buff, 1, opts->slen,
			       stream) == -1) {
		bail_out(EXIT_FAILURE, "read");
	}
	if (close(ipipe[0]) == -1)
		bail_out(EXIT_FAILURE, "close");

	/* Randomly alter string, verbose output */
	if (quit == 0 && opts->verbose) {
		(void) printf("child%02d: erhalten: %s\n",
			      id, buff);
	}
	alter_string(id, buff, opts->slen - 1);
	if (quit == 0 && opts->verbose) {
		(void) printf("child%02d: weiter  : %s\n",
			      id, buff);
	}

	/* Handle output */
	if (id < opts->num_childs) {
		if (close(opipe[0]) == -1)
			bail_out(EXIT_FAILURE, "close");
		/* Get file descriptor for writing */
		stream = fdopen(opipe[1], "w");
		if (stream == NULL)
			bail_out(EXIT_FAILURE, "fdopen");
		if (quit == 0 && fwrite(buff, 1, opts->slen,
					stream) == -1) {
			bail_out(EXIT_FAILURE, "write");
		}
		if (fflush(stream))
			bail_out(EXIT_FAILURE, "fflush");
		if (close(opipe[1]) == -1)
			bail_out(EXIT_FAILURE, "close");
	} else {
		(void) printf("child%02d: Ende    : %s\n",
			      id, buff);
	}

	/* Return failure on exit */
	if (quit == 1)
		exit(EXIT_FAILURE);
	exit(EXIT_SUCCESS);
}
示例#2
0
int main()
{
        alter_string ("Hello, world!\n");
};