SlideShare a Scribd company logo
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).pdf
MuhammadMaazShaik
 
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
mhande899
 
Unit 5
Unit 5Unit 5
Unit 5siddr
 
Computer Networks Lab File
Computer Networks Lab FileComputer Networks Lab File
Computer Networks Lab File
Kandarp 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.pdf
marketing413921
 
Tools.cpp
Tools.cppTools.cpp
Tools.cpp
Vorname Nachname
 
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
 
Sysprog 12
Sysprog 12Sysprog 12
Sysprog 12
Ahmed Mekkawy
 
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
rajkumari873
 
RAILWAY RESERWATION PROJECT PROGRAM
RAILWAY RESERWATION PROJECT PROGRAMRAILWAY RESERWATION PROJECT PROGRAM
RAILWAY RESERWATION PROJECT PROGRAM
Krishna Raj
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab Manual
AkhilaaReddy
 
Program presentation
Program presentationProgram presentation
Program presentation
MdAlauddinRidoy
 
Ejercicios de programacion
Ejercicios de programacionEjercicios de programacion
Ejercicios de programacionJeff Tu Pechito
 
C lab programs
C lab programsC lab programs
C lab programs
Dr. Prashant Vats
 
C lab programs
C lab programsC lab programs
C lab programs
Dr. Prashant Vats
 

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.pdf
abinayamobiles
 
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
abinayamobiles
 
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
abinayamobiles
 
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
abinayamobiles
 
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
abinayamobiles
 
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
abinayamobiles
 
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
abinayamobiles
 
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
abinayamobiles
 
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
abinayamobiles
 
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
abinayamobiles
 
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
abinayamobiles
 
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
abinayamobiles
 
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
abinayamobiles
 
Calculate GDP 4050170024001900.pdf
Calculate GDP 4050170024001900.pdfCalculate GDP 4050170024001900.pdf
Calculate GDP 4050170024001900.pdf
abinayamobiles
 
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
abinayamobiles
 
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
abinayamobiles
 
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
abinayamobiles
 
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
abinayamobiles
 
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
abinayamobiles
 
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
abinayamobiles
 

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

Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
NelTorrente
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
Bisnar Chase Personal Injury Attorneys
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Ashish Kohli
 

Recently uploaded (20)

Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
 

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