MUP1+Øvelse+3.2

toc =Øvelse 1=

code format="cpp"
 * 1) include
 * 2) include
 * 3) include

using namespace std;

void* threadFuncA(void* n);      // vi definere den funktion der skal køres i de 2 tråde

struct DATA                      // Vi opretter en struct her med de data der skal bruges i trådene {   int counter; string threadMessage; };

int main {   DATA t1,t2; void* ret1, *ret2;                             // Vi opretter 2 void pointere til at joine trående t1.counter = 0, t1.threadMessage="thread 1"; t2.counter = 0, t2.threadMessage="thread 2"; pthread_t thID1, thID2; cout << "CreatingThreads" << endl; pthread_create(&thID1, NULL,threadFuncA,&t1);     // opretter tråd 1 pthread_create(&thID2, NULL, threadFuncA, &t2);   // opretter tråd 2 pthread_join(thID1, &ret1);                       // joiner tråd 1 pthread_join(thID2, &ret2);                       // joiner tråd 2 cout << "Terminating program" << endl; return 0; }

void* threadFuncA(void* n){ DATA threadCounter = *(DATA*)n;       // void pointeren N typecastes til Structen DATA

while(threadCounter.counter < 10) {       cout << "Hello #" < Så bliver programmet termineret
 * 2) hvorfor? -> Join sørger for at der ventes på at trådene er kørt færdig. så hvis den ikke kaldes bliver der termineret når trådene er oprettet.

Herunder ses programmet kørt op Target. code format="scheme" root@DevKit8000:~# ./FirstPthread CreatingThreads Hello #0 from thread 1 Hello #1 from thread 1 Hello #2 from thread 1 Hello #3 from thread 1 Hello #4 from thread 1 Hello #5 from thread 1 Hello #6 from thread 1 Hello #7 from thread 1 Hello #8 from thread 1 Hello #9 from thread 1 Hello #0 from thread 2 Hello #1 from thread 2 Hello #2 from thread 2 Hello #3 from thread 2 Hello #4 from thread 2 Hello #5 from thread 2 Hello #6 from thread 2 Hello #7 from thread 2 Hello #8 from thread 2 Hello #9 from thread 2 Terminating program root@DevKit8000:~#

code

=Øvelse 2=  code format="cpp" /* * main.cpp * * Created on: Feb 17, 2011 *     Author: stud */
 * 1) include 
 * 2) include

void* incrementer(void *arg) {   while (true) {       (*(unsigned int*)arg)++; sleep(1); } }

void* reader(void* arg) {   while (true) {       std::cout << *(int*)arg << std::endl; sleep(1); } }

int main {   unsigned int shared = 0; pthread_t t_inc, t_read; void *ret1, *ret2; pthread_create(&t_inc, NULL, incrementer, &shared); pthread_create(&t_read, NULL, reader, &shared); pthread_join(t_inc, &ret1); pthread_join(t_read, &ret2); return 0; } code

Output: Som det kan ses går det galt, da incrementer tråden får lov at køre 2 gange inden reader tråden kommer til.
 * Output || Forventet output ||
 * = 0 ||= 0 ||
 * = 2 ||= 1 ||
 * = 3 ||= 2 ||
 * = 4 ||= 3 ||
 * = 5 ||= 4 ||
 * = 6 ||= 5 ||
 * = 7 ||= 6 ||
 * = 8 ||= 7 ||
 * = 9 ||= 8 ||
 * = 10 ||= 9 ||

=Øvelse 3=

code format="cpp" /* * main.cpp * * Created on: Feb 17, 2011 *     Author: stud */


 * 1) include "Vector.h"
 * 2) include 
 * 3) include
 * 4) include 

Vector v;

void* writer(void *arg) {   while (true) {       if (!v.setAndTest(*(int*)arg)) std::cout << "ERROR!!!!1" << std::endl; sleep(1); } }

int main(int argc, const char* argv[]) {   if (argc < 2) return EXIT_FAILURE;

int t = atoi(argv[1]);

if (t > 100 || t < 1) return EXIT_FAILURE;

pthread_t thread[t]; for (int i = 0; i < t; ++i) { std::cout << "Creating thread " << i << " with ID: " << thread[i] << std::endl; pthread_create(&thread[i], NULL, writer, &i); }   void *ret[t]; for (int i = 0; i < t; ++i) { pthread_join(thread[i], &ret[i]); }

return EXIT_SUCCESS; } code

Output: code Creating thread 0 with ID: 16060404 Creating thread 1 with ID: 3217077952 Creating thread 2 with ID: 3077926496 Creating thread 3 with ID: 3217077908 Creating thread 4 with ID: 15983330 Creating thread 5 with ID: 3217077892 Creating thread 6 with ID: 1155976 Creating thread 7 with ID: 2137520 Creating thread 8 with ID: 16046300 Creating thread 9 with ID: 3217077780 code

=Øvelse 4= code format="cpp" /* * main.cpp * * Created on: Feb 17, 2011 *     Author: stud */


 * 1) include "Vector.h"
 * 2) include 
 * 3) include
 * 4) include 

Vector v; int interval;

void* writer(void *arg) {   while (true) {       if (!v.setAndTest(*(int*)arg)) std::cout << "ERROR!!!!1" << std::endl; usleep(interval); } }

int main(int argc, const char* argv[]) {   if (argc < 3) return EXIT_FAILURE;

int t = atoi(argv[1]); interval = atoi(argv[2]);

if (t > 100 || t < 1) return EXIT_FAILURE;

pthread_t thread[t]; for (int i = 0; i < t; ++i) { std::cout << "Creating thread " << i << " with ID: " << thread[i] << std::endl; // fejl skal være placeret efter pthread_create ellers er de ID'er vi for udskrevet bogus pthread_create(&thread[i], NULL, writer, &i); }   void *ret[t]; for (int i = 0; i < t; ++i) { pthread_join(thread[i], &ret[i]); }

return EXIT_SUCCESS; } code

Output fra target: code root@DevKit8000:~# ./VectorThread 10 500 Creating thread 0 with ID: 0 Creating thread 1 with ID: 0 Creating thread 2 with ID: 0 Creating thread 3 with ID: 1073885184 Creating thread 4 with ID: 3203349508 Creating thread 5 with ID: 1075830304 ERROR!!!!1 Creating thread 6 with ID: 1076757728 Creating thread 7 with ID: 3203349844 Creating thread 8 with ID: 1073884680 Creating thread 9 with ID: 0 code Vores ID'er som er printet her ser ikke rigtige ud.. vi fandt ud af at det var på grund af at vi udskriver ID'et før vi har created tråden og derfor er de bogus... dette er rettet nu. Når vi kører programmet på host, kan vi ikke fremprovokere fejl men når vi kører det på target støder vi tit på ovenstående. vi regner med det er pga. at CPU kraften er mindre på target så den ikke kan håndtere det lige så smooth som host'en

=hvad har vi lært= vi har lært at tråde kan skabe problemer for hinanden hvis de er afhængige af hinanden eller skal tilgå samme data ... altså shared data problematikken.