creenshot needed, just explain the program and answer the red question to copy program /* Multi-threaded example */ #include #include #include #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;
No screenshot needed, just explain the
to copy program
/* Multi-threaded example */
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#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
/* 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 *) i);
/* now join on each thread */
for (i = 0; i < NUM_THREADS; i++) pthread_join(tid[i], NULL);
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);
}
Step by step
Solved in 2 steps