#define NUM_THREADS 10
/*This data is shared by the thread(s) */ pthread_t tid[NUM_THREADS];
/*This is the thread function */ void *runner(void *param);
int main(int argc, char *argv[]) {
int i;
pthread_attr_t attr;
printf("I am the parent thread\n");
/* get the default attributes */ pthread_attr_init(&attr);
/* set the scheduling algorithm to PROCESS(PCS) or SYSTEM(SCS) */ pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
/* set the scheduling policy - FIFO, RR, or OTHER */
pthread_attr_setschedpolicy(&attr, SCHED_OTHER);
/* create the threads */
for (i = 0; i < num_threads;="" i++)="" pthread_create(&tid[i],="" &attr,="" runner,="" (void="" *)="">
/* now join on each thread */
for (i = 0; i < num_threads;="" i++)="" pthread_join(tid[i],="">
printf("I am the parent thread again\n");
return 0;
}
/* Each thread will begin control in this function */
void *runner(void *param) {
int id; id = (int) param;
printf("I am thread #%d, My ID #%lu\n", id, tid[id]);
pthread_exit(0);
}
Extracted text: 3. The following example demonstrates a multi-threaded program: 7* Multi-threaded example / Ainclude include tinclude define NUM_THREADS 10 Thls data is shared by the threadis) / pthread_t tid NUM THREADS): "This is the thread function void *runner (void *param) : int maintint arge, char "argv[l) Int i pthread attr_t attr: printf ("I an the parent thread\n"): * get the default attribates pthread attr_init(satte): * set the scheduling algoritha to PROCESS (PCS) or SYSTEM (SCS) pthread_attr_setscope (tattr, PTHREAD_SCOPE_SYSTEM) : * set the scheduling poliey - FIro, RR, or OTHER / pthread_attr_setschedpolicy (6attr, SCHED OTHER) : create the threada for (i- 07 i < num_threads:="" 1++)="" pthread_create="" (stid(i1,="" sattr,="" runner,="" (void="" )="" 1):="" *="" now="" jain="" on="" each="" thread="" for="" (i-="" 0:="" 1="">< num_threads: 1++) pthread join(tid(a), null) : printf("i an the parent thread again\n"): return 0: 1* kach thread wiil begin control in thin funetion / void "eunner (void "param)1 int idi id (intl param printf("i an thread fid, my id fluin". id, tid[id]): pthread exit (0): run the above program several times and observe the outputs: i am the parent thread i am thread #4, my id #3043994480 i am thread #5, my id #3035601776 i am thread #6, my id #3027209072 i am thread #3, my id #3052387184 i am thread #2, my id #3060779888 i am thread #7, my id #3018816368 i am thread #8, my id #3010423664 i am thread #9, my id #3002030960 i am thread #1, my id #3069172592 i am thread #0, my id #3077565296 i am the parent thread again do the output lines come in the same order every time? why? num_threads:="" 1++)="" pthread="" join(tid(a),="" null)="" :="" printf("i="" an="" the="" parent="" thread="" again\n"):="" return="" 0:="" 1*="" kach="" thread="" wiil="" begin="" control="" in="" thin="" funetion="" void="" "eunner="" (void="" "param)1="" int="" idi="" id="" (intl="" param="" printf("i="" an="" thread="" fid,="" my="" id="" fluin".="" id,="" tid[id]):="" pthread="" exit="" (0):="" run="" the="" above="" program="" several="" times="" and="" observe="" the="" outputs:="" i="" am="" the="" parent="" thread="" i="" am="" thread="" #4,="" my="" id="" #3043994480="" i="" am="" thread="" #5,="" my="" id="" #3035601776="" i="" am="" thread="" #6,="" my="" id="" #3027209072="" i="" am="" thread="" #3,="" my="" id="" #3052387184="" i="" am="" thread="" #2,="" my="" id="" #3060779888="" i="" am="" thread="" #7,="" my="" id="" #3018816368="" i="" am="" thread="" #8,="" my="" id="" #3010423664="" i="" am="" thread="" #9,="" my="" id="" #3002030960="" i="" am="" thread="" #1,="" my="" id="" #3069172592="" i="" am="" thread="" #0,="" my="" id="" #3077565296="" i="" am="" the="" parent="" thread="" again="" do="" the="" output="" lines="" come="" in="" the="" same="" order="" every="" time?=""> num_threads: 1++) pthread join(tid(a), null) : printf("i an the parent thread again\n"): return 0: 1* kach thread wiil begin control in thin funetion / void "eunner (void "param)1 int idi id (intl param printf("i an thread fid, my id fluin". id, tid[id]): pthread exit (0): run the above program several times and observe the outputs: i am the parent thread i am thread #4, my id #3043994480 i am thread #5, my id #3035601776 i am thread #6, my id #3027209072 i am thread #3, my id #3052387184 i am thread #2, my id #3060779888 i am thread #7, my id #3018816368 i am thread #8, my id #3010423664 i am thread #9, my id #3002030960 i am thread #1, my id #3069172592 i am thread #0, my id #3077565296 i am the parent thread again do the output lines come in the same order every time? why?>