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;

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

No screenshot needed, just explain the program and answer the red question

 

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 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 *) 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);

}

 

3. The following example demonstrates a multi-threaded program:
7* Multi-threaded example /
Ainclude <stdio.h>
include <atdlib.h>
tinclude <pthread. h>
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?
Transcribed Image Text:3. The following example demonstrates a multi-threaded program: 7* Multi-threaded example / Ainclude <stdio.h> include <atdlib.h> tinclude <pthread. h> 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?
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Avoiding deadlock
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education