The following exercises needs for you to access a Unix system. A Linux operating system would be better (because I didn't try these exercises on Solaris) but probably not mandatory.
Hello World
pthread_create
.-lpthreads
flag in the compiler options), understand
it and make it work:
#include <pthread.h> #include <stdio.h> #include <stdlib.h> #include#include #include void *hello() { printf("Hello World!\n"); pthread_exit(NULL); } int main() { pthread_t thread; if (pthread_create(&thread, NULL, hello, (void *) NULL) == -1) { perror("hellothread"); exit(1); } exit(0); }
pthread_join
for(i=0; i<21; i++) printf("The square of %d is ", i);The other thread should execute the following for-loop:
for(i=0; i<21; i++) printf("%d\n", i*i);The main thread should wait for the two threads to finish (
pthread_join
) and finally print:
printf("That's all!\n");