Exemplo n.º 1
0
int main(int argc, char *argv[])
{
    int loop_count;
    int sleep_secs;
    int buffer_size;

    const int ping_arg = 1;
    const int pong_arg = 2;
    const int loop_arg = 3;
    const int sleep_arg = 4;
    const int size_arg = 5;

    thread_init ping_init;
    thread_init pong_init;

    /* validity check parameter count */
    if (argc != NUM_ARGS)
    {
        printf("Bad parameter count %d\n", argc);
        printf("Positional parameters: ping & pong strings, loop count, sleep limit, buffer size\n");
        exit (ERR_RETURN);
    }

    /* the loop count is the third positional parameter */
    loop_count = atoi(argv[loop_arg]);  /* atoi converts string to integer */

    /* the sleep limit seconds is the fourth positional */
    sleep_secs = atoi(argv[sleep_arg]);  /* atoi converts string to integer */
    if (sleep_secs > MAX_SLEEP)
    {
        printf("Sleep of %d exceeds maximum %d\n", sleep_secs, MAX_SLEEP);
        exit (ERR_RETURN);
    }

    /* the buffer size is the fifth positional */
    buffer_size = atoi(argv[size_arg]);  /* atoi converts string to integer */

    /* This call is required to initialize the thread library */

    if (st_init() < 0) {
        perror("st_init");
        exit(1);
    }

    /* This call is required to initialize the bounded buffer.
     * The size (in characters) may be set to any value between
     * one and MAX_BUFFER (defined in BB.h).  The call must be
     * made AFTER the st_init() call and BEFORE any producers or
     * consumers using the bounded buffer are started.
     */

    if (BB_init(buffer_size) != 0)
    {
        printf("Buffer size initializer %d not in valid range\n", buffer_size);
        exit(-1);
    }

    /* Create a separate thread for each of ping and pong */

    /* create an initializer for pong thread */
    pong_init.my_call = argv[pong_arg];
    pong_init.my_seed = 0;  /* not used by pong */
    pong_init.count = 0;  /* not used by pong */
    pong_init.max_sleep = 0;  /* not used by pong */

    /* start it first so it will block first
     * The first parameter is the initial function executed in
     * the thread.  The second is the only allowed parameter to
     * the initial function and must be a pointer to any type.
     */

    if (st_thread_create(pong, &pong_init, 0, 0) == NULL)
    {
        perror("st_thread_create");
        exit(1);
    }

    /* create an initializer for ping thread */
    ping_init.my_call = argv[ping_arg];
    ping_init.my_seed = (unsigned int) getpid();  /* unique random seed each run */
    ping_init.count = loop_count;
    ping_init.max_sleep = sleep_secs;

    if (st_thread_create(ping, &ping_init, 0, 0) == NULL)
    {
        perror("st_thread_create");
        exit(1);
    }
    /* causes the main thread to exit with others still running */
    printf("Main thread exiting\n");
    fflush(stdout);
    st_thread_exit(NULL);
}
Exemplo n.º 2
0
ByteBuf*
BB_new(size_t capacity) 
{
    ByteBuf *self = (ByteBuf*)VTable_Make_Obj(&BYTEBUF);
    return BB_init(self, capacity);
}