hw1 - #include < stdlib . h >...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Homework 1 From the textbook questions: 7,8,10,11,12,13,14,17 on page 170. On question 11, the author is talking about User-level threads. Consider the following two unix C programs //PROCESS PROGRAM //To compile : g++ < filename . cc > //To Run: ./ a . out #include < sys /types . h > #include < unistd . h > #include < sys /wait . h > #include < stdio . h > void function ( int value ) ; int sum = 0; int main( void ) { int status ; int pid = waitpid ( fork () , &status , 0); if ( pid > 0) //PARENT { sum++; } else //CHILD { function ( fork ( ) ) ; } printf (”Sum: %d \ n” , sum ) ; return 0; } CIS 486: Design of Operating Systems (Spring 2010) Page 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Homework 1 void function ( int pArg) { int status ; if (pArg != 0) //CHILD { waitpid (pArg , &status , 0); } sum++; } Question 9: (1) Give the output for the above code. (2) How many processes were created? CIS 486: Design of Operating Systems (Spring 2010) Page 2
Background image of page 2
Homework 1 //THREADED PROGRAM //To compile : g++ - lpthread < filename . cc > //To Run: ./ a . out #include < stdio . h >
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: #include &lt; stdlib . h &gt; #include &lt; math . h &gt; #include &lt; pthread . h &gt; #include &lt; sys /types . h &gt; #include &lt; unistd . h &gt; #include &lt; sys /time . h &gt; int sum = 0; void * grandChild ( void * ptr ) { sum++; } void * child ( void * ptr ) { pthread t thread ; pthread create(&amp;thread , NULL, grandChild , ( void * )NULL) ; pthread join ( thread , NULL) ; sum++; } int main( void ) { sum++; pthread t thread ; pthread create(&amp;thread , NULL, child , ( void * )NULL) ; pthread join ( thread , NULL) ; printf (SUM : %d \ n , sum ) ; return 0; } CIS 486: Design of Operating Systems (Spring 2010) Page 3 Homework 1 Question 10: (1) Give the output from the above program. (2) How may threads are created? Question 11: What is the reason for the dierence in the sum variable between the two programs?? CIS 486: Design of Operating Systems (Spring 2010) Page 4...
View Full Document

Page1 / 4

hw1 - #include &amp;amp;lt; stdlib . h &amp;amp;gt;...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online