Skip to content
Snippets Groups Projects
Commit aa8d4f9d authored by antux18's avatar antux18
Browse files

Debut t3 (ne fonctionne pas encore tres bien).

parent 0d0ee3d6
No related merge requests found
......@@ -5,8 +5,61 @@
#include <sys/types.h>
#include <sys/wait.h>
void* runner(void* pid) {
printf("My TID is: %ld\nMy PID is: %d\n", pthread_self(), *((int*) pid));
pthread_exit(0);
}
pthread_t thread_gen(int* pid) {
pthread_t tid;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&tid, &attr, runner, (void*) pid);
return tid;
}
int main() {
// TODO: Fork multiple processes here
// TODO: Each process with a different number of threads
// TODO: Each thread should print the PID and TID
int cpid1, cpid2;
switch (cpid1 = fork()) {
case -1 :
perror("error");
exit(EXIT_FAILURE);
case 0 :
// Child process:
pthread_t tid1, tid2;
tid1 = thread_gen(&cpid1);
tid2 = thread_gen(&cpid1);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
exit(0);
default :
// Parent process:
int status;
if (wait(&status) == -1) {
perror("error");
}
}
switch (cpid2 = fork()) {
case -1 :
perror("error");
exit(EXIT_FAILURE);
case 0 :
// Child process:
pthread_t tid1, tid2, tid3;
tid1 = thread_gen(&cpid2);
tid2 = thread_gen(&cpid2);
tid3 = thread_gen(&cpid2);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
pthread_join(tid3, NULL);
exit(0);
default :
// Parent process:
int status;
if (wait(&status) == -1) {
perror("error");
}
}
return 0;
}
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment