LinuxForum.Fans
Тема

С, Многопоточность, незапланированное завершение

Автор: nik2281 3 сообщений 1776 просмотров
#279289

Всем привет. Вкратце, программа по плану должна работать, пока ей не надоест с завершением по нажатию комбинации клавиш Ctrl+C) Курсируют 5 корабликов между 5 пунктами. При выполнении программы почему-то не происходит зацикливание потока AddPeople. Вместо этого происходит завершение приложения через main. Также, все остальные потоки работают в данном режиме (бесконечных итераций) лишь то время, в котором "спит" AddPeople (unsleep(20000000)). В чём может быть проблема? Заранее спасибо.

text

#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#include<stdlib.h>
#include<time.h>

pthread_t tid1, tid2, tid3, tid4, tid5, tid6, tid7, tid7, tid8, tid9, tid10, tid11; pthread_mutex_t q, con, scp[5];

int queue[5], sIsIn[5], sP[5];

void GetDestination(int c, int* x, int* y) { *y = 5; if (c == 1) *x = 4; if (c == 2) *x = 16; if (c == 3) *x = 27; if (c == 4) *x = 39; if (c == 5) *x = 52; }

void Draw() { int i; pthread_mutex_lock(&con); printf("\033[2J\n"); printf("\033[4;4H\033[31mo"); printf("\033[3;2H\033[0mOmsk"); printf("\033[6;16H\033[33mo"); printf("\033[7;14H\033[0mTara"); printf("\033[4;27H\033[37mo"); printf("\033[3;25H\033[0mTobolsk"); printf("\033[6;39H\033[39mo"); printf("\033[7;36H\033[0mKhanty-Mansiisk"); printf("\033[4;52H\033[36mo"); printf("\033[3;50H\033[0mSalehard"); printf("\033[5;0H\033[44m"); for (i = 0; i < 55; ++i){ printf(" ");} printf("\033[0m"); fflush(stdout); pthread_mutex_unlock(&con); }

void ShowStatus(int f) { int x = 0, y = 0; pthread_mutex_lock(&con); printf("\033[0m\n"); fflush(stdout); if (f == 1) { GetDestination(1, &x, &y); printf("\033[%d;%dH", y - 1, x + 2); printf(" "); printf("\033[%d;%dH",y - 1, x + 2); printf("%d", queue[0]); } if (f == 1) { GetDestination(2, &x, &y); printf("\033[%d;%dH", y + 1, x - 3); printf(" "); printf("\033[%d;%dH",y + 1, x - 3); printf("%d", queue[1]); } if (f == 1) { GetDestination(3, &x, &y); printf("\033[%d;%dH", y - 1, x + 2); printf(" "); printf("\033[%d;%dH",y - 1, x + 2); printf("%d", queue[2]); } if (f == 1) { GetDestination(4, &x, &y); printf("\033[%d;%dH", y + 1, x + 2); printf(" "); printf("\033[%d;%dH",y + 1, x + 2); printf("%d", queue[3]); } if (f == 1) { GetDestination(5, &x, &y); printf("\033[%d;%dH", y - 1, x + 2); printf(" "); printf("\033[%d;%dH",y - 1, x + 2); printf("%d", queue[4]); } fflush(stdout); pthread_mutex_unlock(&con); }

void* AddPeople(void* arg) { do { pthread_mutex_lock(&q); srand(time(NULL)); queue[0] += rand()%7 + 2; queue[1] += rand()%7 + 2; queue[2] += rand()%7 + 1; queue[3] += rand()%7 + 3; queue[4] += rand()%7 + 1; ShowStatus(1); ShowStatus(2); ShowStatus(3); ShowStatus(4); ShowStatus(5); pthread_mutex_unlock(&q); usleep(20000000); } while(1); }

void prepare(int a, int b, int* dx) { if (a > b) { *dx = -1; } else { *dx = 1; } }

void go(int* x, int* y, int* dx, int a, int b, int n) { int pos[2]; GetDestination(a, x, y); GetDestination(b, &pos[0], &pos[1]); //printf("\033[0;10H\033[0m%d %d", pos[0], pos[1]); while(*x != pos[0]) { pthread_mutex_lock(&con); printf("\033[%d;%dH", *y, *x - 1); printf("\033[31;44m"); printf("-%d", n); fflush(stdout); pthread_mutex_unlock(&con); usleep(200000); pthread_mutex_lock(&con); printf("\033[%d;%dH", *y, *x - 1); printf("\033[34;44m"); printf("-%d", n); fflush(stdout); pthread_mutex_unlock(&con); *x+=*dx; } }

void ship1(void* arg) { int x, y, a, b, dx; a = 1; b = 5; while(1) { pthread_mutex_lock(&scp[0]); prepare(a, b, &dx); go(&x, &y, &dx, a, b, 1); sIsIn[0] = b; a = b; pthread_mutex_unlock(&scp[0]); do { srand(time(NULL)); b = rand()%4 +1; } while (a == b); usleep(200000); } }

void PeopleSal(void* arg) { int i; while(1) { for (i = 0; i < 5; ++i) { pthread_mutex_lock(&scp[4]); if (sIsIn[i] == 4) { sP[i] = 0; while ((sP[i] < 7) && (queue[4] > 0)) { queue[4]--; ShowStatus(5); sP[i]++; } sIsIn[i] = 0; } pthread_mutex_unlock(&scp[4]); } } }

void main() { int i; printf("\033[2J\033[11;0H"); for (i = 0; i < 5; ++i) { queue[i] = 20; sIsIn[i] = 0; sP[i] = 0; } Draw(); pthread_create(&tid1, NULL, AddPeople, (void*)1); pthread_create(&tid2, NULL, (void*)PeopleSal, (void*)2); pthread_create(&tid6, NULL, (void*)ship1, (void*)7); usleep(1000000); pthread_mutex_init(&q, NULL); pthread_mutex_init(&con, NULL); usleep(20000000); printf("\033[10;0H\033[0m"); }

#279300

пока не выкинешь все дубликаты ИМХО вряд ли кому-то захочется в этом копаться...

#279305

И то верно..Исправил.

Войдите или зарегистрируйтесь, чтобы ответить.