/** * * * * gcc thrd-sched.c -o thrd-sched -lpthread * * @author Gagne, Galvin, Silberschatz * Operating System Concepts - Eighth Edition * Copyright John Wiley & Sons - 2009. */ #include #include #define NUM_THREADS 5 /* the thread runs in this function */ void *runner(void *param); int thread_cnt = 0; main(int argc, char *argv[]) { int i, scope; pthread_t tid[NUM_THREADS]; /* the thread identifier */ pthread_attr_t attr; /* set of attributes for the thread */ /* get the default attributes */ pthread_attr_init(&attr); /* get the current scheduling policy */ if (pthread_attr_getscope(&attr,&scope) != 0) fprintf(stderr, "Unable to get schedule.\n"); else { if (scope == PTHREAD_SCOPE_PROCESS) printf("PTHREAD_SCOPE_PROCESS\n"); else if (scope == PTHREAD_SCOPE_SYSTEM) printf("PTHREAD_SCOPE_SYSTEM\n"); else printf("Illegal scope value. \n"); } /* set the scheduling */ pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); /* create the threads */ for (i = 0; i < NUM_THREADS; i++) { pthread_create(&tid[i],&attr,runner,NULL); printf("thread: %p\n", &tid[i]); } /** * Now join on each thread */ for (i = 0; i < NUM_THREADS; i++) pthread_join(tid[i], NULL); } /** * The thread will begin control in this function. */ void *runner(void *param) { /* do some work ... */ fprintf(stderr,"thread %d\n",thread_cnt++); pthread_exit(0); }