SlideShare a Scribd company logo
1 of 4
Download to read offline
Consider the fork_example.c code (under "Example code for processes" on Canvas). How many
processes and threads are created?
(code also shown below)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <inttypes.h> /* for PRIxPTR */
#include <pthread.h>
// On Linux we need these for waitpid()
#include <sys/types.h>
#include <sys/wait.h>
// On Solaris, this cleans up format warnings from GCC
#define PID (int) getpid()
// process identity, for output only
char *me = "parent"; // or, "child"
/* function prototype for thread creation */
void * thread_func(void * arg);
/* data for threads */
struct thread_struct {
pthread_t tid; // system-assigned thread identifier
int num; // our own thread number
int in; // input data
int out; // output data
};
struct thread_struct bogus = { 0, 0, 0, -1 };
pthread_attr_t attr; // thread attributes, not used here
struct thread_struct in[2];
void thread_generator(int low, int high, char *identifier)
{
for (int i = low; i < high; i++)
{
in[i].tid = (pthread_t) (-1); // assigned later
in[i].num = i;
in[i].in = i+1;
in[i].out = -1;
printf("[pid %d] %s %s: pthread_create(%d): tryingn",
PID, me, identifier, in[i].num);
// pthread_create(&in[i].tid, &attr, thread_func, &in[i]);
int ret = pthread_create(&in[i].tid, NULL, thread_func, &in[i]);
// in[i].tid = identifier of new thread
// the new thread executes thread_func(&in[i])
// pthread_create() returns an int, equal to 0 or error number
if (ret == 0)
{
printf("[pid %d] %s %s: pthread_create(%d): okn",
PID, me, identifier, in[i].num);
}
else
{
printf("[pid %d] %s %s: pthread_create(%d): failed: %sn",
PID, me, identifier, in[i].num, strerror(ret));
}
}
}
void thread_collector(int low, int high, char *identifier)
{
for (int i = low; i < high; i++)
{
printf("[pid %d] %s %s: pthread_join(%d): tryingn",
PID, me, identifier, in[i].num);
void *out = &bogus;
int ret = pthread_join(in[i].tid, &out);
// out = exit status from pthread_exit()
// pthread_join() returns an int, equal to 0 or error number
if (ret == 0)
{
printf("[pid %d] %s %s: pthread_join(%d): ok, exit status = %p, out = %dn",
PID, me, identifier, in[i].num, out, in[i].out);
}
else
{
printf("[pid %d] %s %s: pthread_join(%d): failed: %s, exit status = %p, out = %dn",
PID, me, identifier, in[i].num, strerror(ret), out, in[i].out);
}
}
}
int main(int argc, char * argv[])
{
printf("[pid %d] %s: &bogus = %pn", PID, me, &bogus);
for (int i = 0; i < 2; i++)
{ printf("[pid %d] %s: &in[%d] = %pn", PID, me, i, &in[i]); }
for (int i = 0; i < 2; i++)
{
in[i].tid = (pthread_t) (-1); // assigned later
in[i].num = i;
in[i].in = -1;
in[i].out = -1;
}
// create two threads
thread_generator(0, 2, "main pre-fork");
pid_t pid = fork();
if (pid < 0)
{
printf("[pid %d] %s: main, fork(): failed: %sn",
PID, me, strerror(errno));
}
else if (pid == 0)
{
me = "child";
printf("[pid %d] %s: main, parent is %dn",
PID, me, (int) getppid());
}
else
{
printf("[pid %d] %s: main, child is %dn",
PID, me, (int) pid);
}
for (int i = 0; i < 2; i++)
{ printf("[pid %d] %s: in[%d].tid = %" PRIxPTR "n", PID, me, i, (uintptr_t) in[i].tid); }
// join with two threads
thread_collector(0, 2, "main");
if (pid == 0)
{
printf("[pid %d] %s: main, sleeping 1n", PID, me);
sleep(1);
printf("[pid %d] %s: main, exitingn", PID, me);
exit(0);
}
if (pid > 0)
{
printf("[pid %d] %s: main, waitingn", PID, me);
waitpid(pid, NULL, 0);
}
printf("[pid %d] %s: main, exitingn", PID, me);
return 0;
}
/* standard format for Pthreads */
void * thread_func(void * arg)
{
struct thread_struct *p = arg;
printf("[pid %d] %s: hello, world, this is thread %d, ID %" PRIxPTR " %" PRIxPTR "n",
PID, me, p->num, (uintptr_t) p->tid, (uintptr_t) pthread_self());
printf("[pid %d] %s: thread %d, sleeping %dn",
PID, me, p->num, p->in);
sleep(p->in);
// do some work, so we can see if the thread actually ran
p->out = 1;
printf("[pid %d] %s: thread %d, exitingn",
PID, me, p->num);
pthread_exit(arg); // the thread's exit status
// void return
}
Group of answer choices
2 process, 2 threads
3 processes, 2 threads
2 processes, 4 threads
3 processes, 4 threads

More Related Content

Similar to Consider the fork_examplec code under Example code for pr.pdf

sodapdf-converted into ppt presentation(1).pdf
sodapdf-converted into ppt presentation(1).pdfsodapdf-converted into ppt presentation(1).pdf
sodapdf-converted into ppt presentation(1).pdfMuhammadMaazShaik
 
C language concept with code apna college.pdf
C language concept with code apna college.pdfC language concept with code apna college.pdf
C language concept with code apna college.pdfmhande899
 
Unit 5
Unit 5Unit 5
Unit 5siddr
 
Computer Networks Lab File
Computer Networks Lab FileComputer Networks Lab File
Computer Networks Lab FileKandarp Tiwari
 
Unit 6
Unit 6Unit 6
Unit 6siddr
 
The following code is an implementation of the producer consumer pro.pdf
The following code is an implementation of the producer consumer pro.pdfThe following code is an implementation of the producer consumer pro.pdf
The following code is an implementation of the producer consumer pro.pdfmarketing413921
 
C basics
C basicsC basics
C basicsMSc CST
 
Rootkit on Linux X86 v2.6
Rootkit on Linux X86 v2.6Rootkit on Linux X86 v2.6
Rootkit on Linux X86 v2.6fisher.w.y
 
Quest 1 define a class batsman with the following specifications
Quest  1 define a class batsman with the following specificationsQuest  1 define a class batsman with the following specifications
Quest 1 define a class batsman with the following specificationsrajkumari873
 
RAILWAY RESERWATION PROJECT PROGRAM
RAILWAY RESERWATION PROJECT PROGRAMRAILWAY RESERWATION PROJECT PROGRAM
RAILWAY RESERWATION PROJECT PROGRAMKrishna Raj
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab ManualAkhilaaReddy
 
Ejercicios de programacion
Ejercicios de programacionEjercicios de programacion
Ejercicios de programacionJeff Tu Pechito
 

Similar to Consider the fork_examplec code under Example code for pr.pdf (20)

sodapdf-converted into ppt presentation(1).pdf
sodapdf-converted into ppt presentation(1).pdfsodapdf-converted into ppt presentation(1).pdf
sodapdf-converted into ppt presentation(1).pdf
 
C language concept with code apna college.pdf
C language concept with code apna college.pdfC language concept with code apna college.pdf
C language concept with code apna college.pdf
 
Unit 5
Unit 5Unit 5
Unit 5
 
Computer Networks Lab File
Computer Networks Lab FileComputer Networks Lab File
Computer Networks Lab File
 
Unit 6
Unit 6Unit 6
Unit 6
 
The following code is an implementation of the producer consumer pro.pdf
The following code is an implementation of the producer consumer pro.pdfThe following code is an implementation of the producer consumer pro.pdf
The following code is an implementation of the producer consumer pro.pdf
 
Vcs29
Vcs29Vcs29
Vcs29
 
Tools.cpp
Tools.cppTools.cpp
Tools.cpp
 
C basics
C basicsC basics
C basics
 
Rootkit on Linux X86 v2.6
Rootkit on Linux X86 v2.6Rootkit on Linux X86 v2.6
Rootkit on Linux X86 v2.6
 
Sysprog 12
Sysprog 12Sysprog 12
Sysprog 12
 
Sysprog 12
Sysprog 12Sysprog 12
Sysprog 12
 
Quest 1 define a class batsman with the following specifications
Quest  1 define a class batsman with the following specificationsQuest  1 define a class batsman with the following specifications
Quest 1 define a class batsman with the following specifications
 
RAILWAY RESERWATION PROJECT PROGRAM
RAILWAY RESERWATION PROJECT PROGRAMRAILWAY RESERWATION PROJECT PROGRAM
RAILWAY RESERWATION PROJECT PROGRAM
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab Manual
 
Program presentation
Program presentationProgram presentation
Program presentation
 
C
CC
C
 
Ejercicios de programacion
Ejercicios de programacionEjercicios de programacion
Ejercicios de programacion
 
C lab programs
C lab programsC lab programs
C lab programs
 
C lab programs
C lab programsC lab programs
C lab programs
 

More from abinayamobiles

Hace diez aos Samsung Electronics Company vendi un montn.pdf
Hace diez aos Samsung Electronics Company vendi un montn.pdfHace diez aos Samsung Electronics Company vendi un montn.pdf
Hace diez aos Samsung Electronics Company vendi un montn.pdfabinayamobiles
 
Hello I need help within this question thank you Select a .pdf
Hello I need help within this question thank you Select a .pdfHello I need help within this question thank you Select a .pdf
Hello I need help within this question thank you Select a .pdfabinayamobiles
 
De qu manera la tecnologa el marketing y las ventas de A.pdf
De qu manera la tecnologa el marketing y las ventas de A.pdfDe qu manera la tecnologa el marketing y las ventas de A.pdf
De qu manera la tecnologa el marketing y las ventas de A.pdfabinayamobiles
 
En 2009 el dficit presupuestario del gobierno de EE UU a.pdf
En 2009 el dficit presupuestario del gobierno de EE UU a.pdfEn 2009 el dficit presupuestario del gobierno de EE UU a.pdf
En 2009 el dficit presupuestario del gobierno de EE UU a.pdfabinayamobiles
 
Assume that IQ scores are normally distributed with a mean 1.pdf
Assume that IQ scores are normally distributed with a mean 1.pdfAssume that IQ scores are normally distributed with a mean 1.pdf
Assume that IQ scores are normally distributed with a mean 1.pdfabinayamobiles
 
During January its first month of operations Crane Company.pdf
During January its first month of operations Crane Company.pdfDuring January its first month of operations Crane Company.pdf
During January its first month of operations Crane Company.pdfabinayamobiles
 
Based on a case study with the title Zespri by Zespri by Jos.pdf
Based on a case study with the title Zespri by Zespri by Jos.pdfBased on a case study with the title Zespri by Zespri by Jos.pdf
Based on a case study with the title Zespri by Zespri by Jos.pdfabinayamobiles
 
Bir nfus saym yetikinlerin 522sinin kadn 102sinin b.pdf
Bir nfus saym yetikinlerin 522sinin kadn 102sinin b.pdfBir nfus saym yetikinlerin 522sinin kadn 102sinin b.pdf
Bir nfus saym yetikinlerin 522sinin kadn 102sinin b.pdfabinayamobiles
 
A group of evolutionary biologists measure population struct.pdf
A group of evolutionary biologists measure population struct.pdfA group of evolutionary biologists measure population struct.pdf
A group of evolutionary biologists measure population struct.pdfabinayamobiles
 
6 De acuerdo con la investigacin sobre comunicacin de gn.pdf
6 De acuerdo con la investigacin sobre comunicacin de gn.pdf6 De acuerdo con la investigacin sobre comunicacin de gn.pdf
6 De acuerdo con la investigacin sobre comunicacin de gn.pdfabinayamobiles
 
3 Kronik hastalklar iin risk faktrlerinin epidemiyolojisi.pdf
3 Kronik hastalklar iin risk faktrlerinin epidemiyolojisi.pdf3 Kronik hastalklar iin risk faktrlerinin epidemiyolojisi.pdf
3 Kronik hastalklar iin risk faktrlerinin epidemiyolojisi.pdfabinayamobiles
 
1The following are supply chain drivers A facilities and.pdf
1The following are supply chain drivers A facilities and.pdf1The following are supply chain drivers A facilities and.pdf
1The following are supply chain drivers A facilities and.pdfabinayamobiles
 
Caso Uber Esta parte del caso acumulativo Uber se centra en .pdf
Caso Uber Esta parte del caso acumulativo Uber se centra en .pdfCaso Uber Esta parte del caso acumulativo Uber se centra en .pdf
Caso Uber Esta parte del caso acumulativo Uber se centra en .pdfabinayamobiles
 
Calculate GDP 4050170024001900.pdf
Calculate GDP 4050170024001900.pdfCalculate GDP 4050170024001900.pdf
Calculate GDP 4050170024001900.pdfabinayamobiles
 
b Explain the principle of maximum likelihood estimation .pdf
b Explain the principle of maximum likelihood estimation .pdfb Explain the principle of maximum likelihood estimation .pdf
b Explain the principle of maximum likelihood estimation .pdfabinayamobiles
 
7 Axon retim hattnda tek bir rn olan AX484 ile erken aa.pdf
7 Axon retim hattnda tek bir rn olan AX484 ile erken aa.pdf7 Axon retim hattnda tek bir rn olan AX484 ile erken aa.pdf
7 Axon retim hattnda tek bir rn olan AX484 ile erken aa.pdfabinayamobiles
 
Why can covid researchers run an OLS regression of the follo.pdf
Why can covid researchers run an OLS regression of the follo.pdfWhy can covid researchers run an OLS regression of the follo.pdf
Why can covid researchers run an OLS regression of the follo.pdfabinayamobiles
 
What factor determines whether retirement of general longte.pdf
What factor determines whether retirement of general longte.pdfWhat factor determines whether retirement of general longte.pdf
What factor determines whether retirement of general longte.pdfabinayamobiles
 
Which of the following are true statements about the Oaxaca .pdf
Which of the following are true statements about the Oaxaca .pdfWhich of the following are true statements about the Oaxaca .pdf
Which of the following are true statements about the Oaxaca .pdfabinayamobiles
 
9 Given that Z is a standard normal variable what is the v.pdf
9 Given that Z is a standard normal variable what is the v.pdf9 Given that Z is a standard normal variable what is the v.pdf
9 Given that Z is a standard normal variable what is the v.pdfabinayamobiles
 

More from abinayamobiles (20)

Hace diez aos Samsung Electronics Company vendi un montn.pdf
Hace diez aos Samsung Electronics Company vendi un montn.pdfHace diez aos Samsung Electronics Company vendi un montn.pdf
Hace diez aos Samsung Electronics Company vendi un montn.pdf
 
Hello I need help within this question thank you Select a .pdf
Hello I need help within this question thank you Select a .pdfHello I need help within this question thank you Select a .pdf
Hello I need help within this question thank you Select a .pdf
 
De qu manera la tecnologa el marketing y las ventas de A.pdf
De qu manera la tecnologa el marketing y las ventas de A.pdfDe qu manera la tecnologa el marketing y las ventas de A.pdf
De qu manera la tecnologa el marketing y las ventas de A.pdf
 
En 2009 el dficit presupuestario del gobierno de EE UU a.pdf
En 2009 el dficit presupuestario del gobierno de EE UU a.pdfEn 2009 el dficit presupuestario del gobierno de EE UU a.pdf
En 2009 el dficit presupuestario del gobierno de EE UU a.pdf
 
Assume that IQ scores are normally distributed with a mean 1.pdf
Assume that IQ scores are normally distributed with a mean 1.pdfAssume that IQ scores are normally distributed with a mean 1.pdf
Assume that IQ scores are normally distributed with a mean 1.pdf
 
During January its first month of operations Crane Company.pdf
During January its first month of operations Crane Company.pdfDuring January its first month of operations Crane Company.pdf
During January its first month of operations Crane Company.pdf
 
Based on a case study with the title Zespri by Zespri by Jos.pdf
Based on a case study with the title Zespri by Zespri by Jos.pdfBased on a case study with the title Zespri by Zespri by Jos.pdf
Based on a case study with the title Zespri by Zespri by Jos.pdf
 
Bir nfus saym yetikinlerin 522sinin kadn 102sinin b.pdf
Bir nfus saym yetikinlerin 522sinin kadn 102sinin b.pdfBir nfus saym yetikinlerin 522sinin kadn 102sinin b.pdf
Bir nfus saym yetikinlerin 522sinin kadn 102sinin b.pdf
 
A group of evolutionary biologists measure population struct.pdf
A group of evolutionary biologists measure population struct.pdfA group of evolutionary biologists measure population struct.pdf
A group of evolutionary biologists measure population struct.pdf
 
6 De acuerdo con la investigacin sobre comunicacin de gn.pdf
6 De acuerdo con la investigacin sobre comunicacin de gn.pdf6 De acuerdo con la investigacin sobre comunicacin de gn.pdf
6 De acuerdo con la investigacin sobre comunicacin de gn.pdf
 
3 Kronik hastalklar iin risk faktrlerinin epidemiyolojisi.pdf
3 Kronik hastalklar iin risk faktrlerinin epidemiyolojisi.pdf3 Kronik hastalklar iin risk faktrlerinin epidemiyolojisi.pdf
3 Kronik hastalklar iin risk faktrlerinin epidemiyolojisi.pdf
 
1The following are supply chain drivers A facilities and.pdf
1The following are supply chain drivers A facilities and.pdf1The following are supply chain drivers A facilities and.pdf
1The following are supply chain drivers A facilities and.pdf
 
Caso Uber Esta parte del caso acumulativo Uber se centra en .pdf
Caso Uber Esta parte del caso acumulativo Uber se centra en .pdfCaso Uber Esta parte del caso acumulativo Uber se centra en .pdf
Caso Uber Esta parte del caso acumulativo Uber se centra en .pdf
 
Calculate GDP 4050170024001900.pdf
Calculate GDP 4050170024001900.pdfCalculate GDP 4050170024001900.pdf
Calculate GDP 4050170024001900.pdf
 
b Explain the principle of maximum likelihood estimation .pdf
b Explain the principle of maximum likelihood estimation .pdfb Explain the principle of maximum likelihood estimation .pdf
b Explain the principle of maximum likelihood estimation .pdf
 
7 Axon retim hattnda tek bir rn olan AX484 ile erken aa.pdf
7 Axon retim hattnda tek bir rn olan AX484 ile erken aa.pdf7 Axon retim hattnda tek bir rn olan AX484 ile erken aa.pdf
7 Axon retim hattnda tek bir rn olan AX484 ile erken aa.pdf
 
Why can covid researchers run an OLS regression of the follo.pdf
Why can covid researchers run an OLS regression of the follo.pdfWhy can covid researchers run an OLS regression of the follo.pdf
Why can covid researchers run an OLS regression of the follo.pdf
 
What factor determines whether retirement of general longte.pdf
What factor determines whether retirement of general longte.pdfWhat factor determines whether retirement of general longte.pdf
What factor determines whether retirement of general longte.pdf
 
Which of the following are true statements about the Oaxaca .pdf
Which of the following are true statements about the Oaxaca .pdfWhich of the following are true statements about the Oaxaca .pdf
Which of the following are true statements about the Oaxaca .pdf
 
9 Given that Z is a standard normal variable what is the v.pdf
9 Given that Z is a standard normal variable what is the v.pdf9 Given that Z is a standard normal variable what is the v.pdf
9 Given that Z is a standard normal variable what is the v.pdf
 

Recently uploaded

Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 

Recently uploaded (20)

Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 

Consider the fork_examplec code under Example code for pr.pdf

  • 1. Consider the fork_example.c code (under "Example code for processes" on Canvas). How many processes and threads are created? (code also shown below) #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <inttypes.h> /* for PRIxPTR */ #include <pthread.h> // On Linux we need these for waitpid() #include <sys/types.h> #include <sys/wait.h> // On Solaris, this cleans up format warnings from GCC #define PID (int) getpid() // process identity, for output only char *me = "parent"; // or, "child" /* function prototype for thread creation */ void * thread_func(void * arg); /* data for threads */ struct thread_struct { pthread_t tid; // system-assigned thread identifier int num; // our own thread number int in; // input data int out; // output data }; struct thread_struct bogus = { 0, 0, 0, -1 }; pthread_attr_t attr; // thread attributes, not used here struct thread_struct in[2]; void thread_generator(int low, int high, char *identifier) { for (int i = low; i < high; i++) { in[i].tid = (pthread_t) (-1); // assigned later in[i].num = i; in[i].in = i+1; in[i].out = -1; printf("[pid %d] %s %s: pthread_create(%d): tryingn", PID, me, identifier, in[i].num); // pthread_create(&in[i].tid, &attr, thread_func, &in[i]); int ret = pthread_create(&in[i].tid, NULL, thread_func, &in[i]);
  • 2. // in[i].tid = identifier of new thread // the new thread executes thread_func(&in[i]) // pthread_create() returns an int, equal to 0 or error number if (ret == 0) { printf("[pid %d] %s %s: pthread_create(%d): okn", PID, me, identifier, in[i].num); } else { printf("[pid %d] %s %s: pthread_create(%d): failed: %sn", PID, me, identifier, in[i].num, strerror(ret)); } } } void thread_collector(int low, int high, char *identifier) { for (int i = low; i < high; i++) { printf("[pid %d] %s %s: pthread_join(%d): tryingn", PID, me, identifier, in[i].num); void *out = &bogus; int ret = pthread_join(in[i].tid, &out); // out = exit status from pthread_exit() // pthread_join() returns an int, equal to 0 or error number if (ret == 0) { printf("[pid %d] %s %s: pthread_join(%d): ok, exit status = %p, out = %dn", PID, me, identifier, in[i].num, out, in[i].out); } else { printf("[pid %d] %s %s: pthread_join(%d): failed: %s, exit status = %p, out = %dn", PID, me, identifier, in[i].num, strerror(ret), out, in[i].out); } } } int main(int argc, char * argv[]) { printf("[pid %d] %s: &bogus = %pn", PID, me, &bogus); for (int i = 0; i < 2; i++) { printf("[pid %d] %s: &in[%d] = %pn", PID, me, i, &in[i]); }
  • 3. for (int i = 0; i < 2; i++) { in[i].tid = (pthread_t) (-1); // assigned later in[i].num = i; in[i].in = -1; in[i].out = -1; } // create two threads thread_generator(0, 2, "main pre-fork"); pid_t pid = fork(); if (pid < 0) { printf("[pid %d] %s: main, fork(): failed: %sn", PID, me, strerror(errno)); } else if (pid == 0) { me = "child"; printf("[pid %d] %s: main, parent is %dn", PID, me, (int) getppid()); } else { printf("[pid %d] %s: main, child is %dn", PID, me, (int) pid); } for (int i = 0; i < 2; i++) { printf("[pid %d] %s: in[%d].tid = %" PRIxPTR "n", PID, me, i, (uintptr_t) in[i].tid); } // join with two threads thread_collector(0, 2, "main"); if (pid == 0) { printf("[pid %d] %s: main, sleeping 1n", PID, me); sleep(1); printf("[pid %d] %s: main, exitingn", PID, me); exit(0); } if (pid > 0) { printf("[pid %d] %s: main, waitingn", PID, me); waitpid(pid, NULL, 0); }
  • 4. printf("[pid %d] %s: main, exitingn", PID, me); return 0; } /* standard format for Pthreads */ void * thread_func(void * arg) { struct thread_struct *p = arg; printf("[pid %d] %s: hello, world, this is thread %d, ID %" PRIxPTR " %" PRIxPTR "n", PID, me, p->num, (uintptr_t) p->tid, (uintptr_t) pthread_self()); printf("[pid %d] %s: thread %d, sleeping %dn", PID, me, p->num, p->in); sleep(p->in); // do some work, so we can see if the thread actually ran p->out = 1; printf("[pid %d] %s: thread %d, exitingn", PID, me, p->num); pthread_exit(arg); // the thread's exit status // void return } Group of answer choices 2 process, 2 threads 3 processes, 2 threads 2 processes, 4 threads 3 processes, 4 threads