Captain_Obvious
Neu angemeldet
Ich komme mal wieder nicht wirklich weiter und zwar diesmal mit pthreads ich vermute der Fehler ist jedes mal der selbe und wahrscheinlich ziemlich dämlich aber ich hab ihn bisher nicht entdecken können wenn jemand eine Idee hat wäre ich für einen Tipp dankbar
Aufgabenstellung:
http://pastebin.com/A2N5rXFM
output vom gcc:
Aufgabenstellung:
globale Variabelen i und j, i wird mit 1 initiallisiert Hauptthread soll zwei threads starten die jeweils i mitgegeben bekommen
thread 1:
Ausgabe tread id
3sec warten
Ausgabe var i und j
thread 2:
Ausgabe tread id
Endlosschleife i++ und j++ anschließende Ausgabe und 1 sec blokieren
Hauptthread:
Ausgabe Thread ID + Thread id der beiden anderen threads
warten auf thread 1
Ausgabe i und j
kill thread 2
[src=c]#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
int j=1;
//void Kind1 ();
//void Kind2 ();
int thread1 (int *i);
int thread2 (int *i);
int TID1, TID2; // Thread IDs der Kinder
void main (void){
//int thread1,thread2;
int *i=1;
int x;
int pthread_create(pthread_t *thread, NULL, int thread1 (int *i));
int pthread_join (pthread_t thread, TID1);
int pthread_create(pthread_t *thread, NULL, int thread2 (int *i));
int pthread_cancel(pthread_t thread);
printf("Eltern PID = %i\n",system("getpid"));
printf("Kind1 Thread ID = %i\n", TID1);
printf("Kind2 Thread ID = %i\n", TID2);
for (x=0;x<3;x++)
{
sleep(1);
printf("Var i = %i\n",i);
system("ps");
};
//kill (pid_2);
}
int thread1 (int *i)
{
printf("Hier ist Thread 1\n");
printf("PID = %i\n",system("getpid"));
//int i;
sleep(3);
printf("Schlafen beendet\n");
printf("var i = %i\n", i);
printf("var j = %i\n", j);
TID1 = pthread_self();
return TID1;
}
int thread2 (int *i)
{
printf("Eltern PID = %i\n",system("getppid"));
printf("Hier ist Thread 1\n");
printf("PID = %i\n",system("getpid"));
int y=1;
//int i;
do {
i++;
j++;
}
while (y=1);
printf("var i = %i\n", i);
printf("var j = %i\n", j);
wait(1);
TID2 = pthread_self();
return TID2;
}[/src]
http://pastebin.com/A2N5rXFM
output vom gcc:
gcc -o BSY2-03 BSY2-03.c -pthread
BSY2-03.c: In function ‘main’:
BSY2-03.c:16:10: warning: initialization makes pointer from integer without a cast [enabled by default]
int *i=1;
^
In file included from /usr/include/unistd.h:226:0,
from BSY2-03.c:4:
BSY2-03.c:18:41: error: expected declaration specifiers or ‘...’ before ‘(’ token
int pthread_create(pthread_t *thread, NULL, int thread1 (int *i));
^
BSY2-03.c:19:39: error: expected declaration specifiers or ‘...’ before ‘TID1’
int pthread_join (pthread_t thread, TID1);
^
In file included from /usr/include/unistd.h:226:0,
from BSY2-03.c:4:
BSY2-03.c:20:41: error: expected declaration specifiers or ‘...’ before ‘(’ token
int pthread_create(pthread_t *thread, NULL, int thread2 (int *i));
^
BSY2-03.c:28:5: warning: format ‘%i’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
printf("Var i = %i\n",i);
^
BSY2-03.c: In function ‘thread1’:
BSY2-03.c:41:3: warning: format ‘%i’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
printf("var i = %i\n", i);
^
BSY2-03.c: In function ‘thread2’:
BSY2-03.c:59:3: warning: format ‘%i’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
printf("var i = %i\n", i);