Exemplo n.º 1
0
/*
 * The main program to "drive" the pipeline...
 */
int main (int argc, char *argv[])
{
    pipe_t my_pipe;
    long value, result;
    int status;
    char line[128];

    pipe_create (&my_pipe, 10);
    printf ("Enter integer values, or \"=\" for next result\n");

    while (1) {
        printf ("Data> ");
        if (fgets (line, sizeof (line), stdin) == NULL) exit (0);
        if (strlen (line) <= 1) continue;
        if (strlen (line) <= 2 && line[0] == '=') {
            if (pipe_result (&my_pipe, &result))
                printf ("Result is %ld\n", result);
            else
                printf ("Pipe is empty\n");
        } else {
            if (sscanf (line, "%ld", &value) < 1)
                fprintf (stderr, "Enter an integer value\n");
            else
                pipe_start (&my_pipe, value);
        }
    }
}
Exemplo n.º 2
0
int main(int argc, char *argv[]) {
	pipe_t pipe;
	int value, result;
	char line[128];

	pipe_create(&pipe, 2);
	printf("Enter inter value, or '=' for next result\n");

	while (1) {
		printf("Data> ");
		E_TEST(NULL, fgets(line, sizeof(line), stdin));
		if (strlen(line) <= 1) continue;
		if (strlen(line) <= 2 && line[0] == '=') {
			if (pipe_result(&pipe, &result))
				printf("Result is %d\n", result);
			else
				printf("Pipe is empty\n");
		} else {
			if (sscanf(line, "%d", &value) < 1)
				fprintf(stderr, "Enter an integer value\n");
			else 
				pipe_start(&pipe, value);
		}
	}
	return 0 ;
}
Exemplo n.º 3
0
Arquivo: pipe.c Projeto: Ace-Tang/APUE
int main()
{
	pipe_t mypipe;
	long value, result;
	int status, len;
	char line[128];
	stage_t *stage, *stage_next;

	pipe_create(&mypipe, 10);
	printf("Enter integer value, or \"=\" for result, enter quit to leave\n");

	while(1)
	{
		printf("Data > ");
		if (fgets(line, sizeof line, stdin) == NULL || my_strcmp("quit", line))
			break;
		len = strlen(line);
		if (len <= 1)
			continue;
		if (len <= 2 && line[0] == '=')
		{
			if (pipe_result(&mypipe, &result))
				printf("result is %ld\n", result);
			else 
				printf("pipe is empty\n");
		}
		else
		{
			status = sscanf(line, "%ld", &value);
			if (status < 1)
				printf("please enter integer\n");
			else
				pipe_start(&mypipe, value);
		}
	}

	for (stage = mypipe.head; stage != NULL;)
	{
		stage_next = stage->next;
		free(stage);
		printf("free stage\n");
		stage = stage_next;
	}

	printf("end of pipe\n");
}
Exemplo n.º 4
-1
/* service_main */
static VOID WINAPI service_main(DWORD argc, LPTSTR *argv) {
    config_t        conf;
    void*           net;
    void*           threads;
    void*           pipe;

    h_service = RegisterServiceCtrlHandler(LDMSVC_SERVICE_NAME, handler);
    if(h_service == NULL) {
        return;
    }

    set_state1(SERVICE_START_PENDING);        // SERVICE_START_PENDING

    // open the heap
    if(!heap_start()) {
        dout("Failed to create the heap\n");
        set_state2(SERVICE_STOPPED, 1);
        return;
    }

    // parse configuration
    if(!config_parse_args(&conf, argc, argv)) {
        heap_stop();
        set_state2(SERVICE_STOPPED, 1);
        return;
    }

    if(0) {
        dout(va("PORT: %u\n", conf.port));
        dout(va("PIPE: %s\n", conf.pipe));
        dout(va("MAXC: %u\n", conf.maxconn));
    }

    // open network
    if((net = net_start()) == NULL) {
        heap_stop();
        set_state2(SERVICE_STOPPED, 1);
        return;
    }

    // open the pipe
    if((pipe = pipe_start(conf.pipe)) == NULL) {
        net_stop(net);
        heap_stop();
        set_state2(SERVICE_STOPPED, 1);
        return;
    }

    // connect the pipe
    if(!pipe_open(pipe)) {
        pipe_stop(pipe);
        net_stop(net);
        heap_stop();
        set_state2(SERVICE_STOPPED, 1);
        return;
    }

    // start threads
    if((threads = threads_start(net, pipe, conf.maxconn)) == NULL) {
        pipe_stop(pipe);
        net_stop(net);
        heap_stop();
        set_state2(SERVICE_STOPPED, 1);
        return;
    }

    set_state1(SERVICE_RUNNING);              // SERVICE_RUNNING

    while(svc_state == SERVICE_RUNNING) {
        if(!net_is_ready(net)) {
            net_bind(net, NULL, conf.port);
        }

        if(!threads_think(threads)) {
            break;
        }

        Sleep(1);
    }

    set_state1(SERVICE_STOP_PENDING);         // SERVICE_STOP_PENDING

    // close everything here
    threads_stop(threads);
    pipe_stop(pipe);
    net_stop(net);
    heap_stop();

    set_state2(SERVICE_STOPPED, 0);           // SERVICE_STOPPED
}