Toggle navigation
Images.Elk.pl
zad3
#include
#include
#include
#include
#include
#define test_errno(msg) do{if (errno) {perror(msg); exit(EXIT_FAILURE);}} while(0)
pthread_t id[2];
int a = 0;
float newton (int x, int y)
{
if(x == 0 || x == y) { return 1; }
if(x>0 && y>0 && y>x) { return newton(x-1, y-1) + newton(x-1,y); }
}
void* watek(void* _arg)
{
while (a == 0) usleep (1000);
while (1) {
puts("c");
usleep (300000);
if (0 == pthread_join(id[1], NULL)) { break; }
puts("z");
usleep (300000);
if (0 == pthread_join(id[1], NULL)) { break; }
puts("e");
usleep (300000);
if (0 == pthread_join(id[1], NULL)) { break; }
puts("k");
usleep (300000);
if (0 == pthread_join(id[1], NULL)) { break; }
puts("a");
usleep (300000);
if (0 == pthread_join(id[1], NULL)) { break; }
puts("m ");
usleep (300000);
if (0 == pthread_join(id[1], NULL)) { break; } }
return NULL;
}
void* watek2(void* _arg)
{
int x,y;
printf ("Podaj k: ");
scanf ("%d", &x);
printf ("Podaj n: ");
scanf ("%d", &y);
a = 1;
printf ("Wynik to: %f\n", newton (x,y));
return NULL;
}
//------------------------------------------------------------------------
int main()
{
errno = pthread_create(&id[0], NULL, watek, NULL);
test_errno("Nie powiodło się pthread_create");
errno = pthread_create(&id[1], NULL, watek2, NULL);
test_errno("Nie powiodło się pthread_create");
/* oczekiwanie na zakończenie wszystkich wątków */
errno = pthread_join(id[1], NULL);
test_errno("pthread_join");
return EXIT_SUCCESS;
}
#include
#include
#include
#include
#include
#define test_errno(msg) do{if (errno) {perror(msg); exit(EXIT_FAILURE);}} while(0)
pthread_t id[2];
int a = 0;
float newton (int x, int y)
{
if(x == 0 || x == y) { return 1; }
if(x>0 && y>0 && y>x) { return newton(x-1, y-1) + newton(x-1,y); }
}
void* watek(void* _arg)
{
while (a == 0) usleep (1000);
while (1) {
puts("c");
usleep (300000);
if (0 == pthread_join(id[1], NULL)) { break; }
puts("z");
usleep (300000);
if (0 == pthread_join(id[1], NULL)) { break; }
puts("e");
usleep (300000);
if (0 == pthread_join(id[1], NULL)) { break; }
puts("k");
usleep (300000);
if (0 == pthread_join(id[1], NULL)) { break; }
puts("a");
usleep (300000);
if (0 == pthread_join(id[1], NULL)) { break; }
puts("m ");
usleep (300000);
if (0 == pthread_join(id[1], NULL)) { break; } }
return NULL;
}
void* watek2(void* _arg)
{
int x,y;
printf ("Podaj k: ");
scanf ("%d", &x);
printf ("Podaj n: ");
scanf ("%d", &y);
a = 1;
printf ("Wynik to: %f\n", newton (x,y));
return NULL;
}
//------------------------------------------------------------------------
int main()
{
errno = pthread_create(&id[0], NULL, watek, NULL);
test_errno("Nie powiodło się pthread_create");
errno = pthread_create(&id[1], NULL, watek2, NULL);
test_errno("Nie powiodło się pthread_create");
/* oczekiwanie na zakończenie wszystkich wątków */
errno = pthread_join(id[1], NULL);
test_errno("pthread_join");
return EXIT_SUCCESS;
}
Wyszukiwarka
Podobne podstrony:
zad3 tos
K1 07 08 zad3 rozwiazanie?gmaraK gr2 (2)
zad3 2
zad3
ZAD3
3 M1 DohnalJ MatusiakK ZAD3
zad3 2
PP Zad3
PKS W zad3 Opis Techniczny
zad3(seba)
zad3
więcej podobnych podstron