lab6 (1)

Embed Size (px)

Citation preview

  • 8/18/2019 lab6 (1)

    1/2

    OS INLAB 6

    Q1: Write a C/C++ program which creates n-1 processes synchronizing

    themselves with the wait function. Each one of the n-1 processes prints on

    screen the value of an element of an array of size n. The remaining element

    (n-1 chil processes an n elements! is printe "y the original parentprocess. The program must#

    1. $ea a comman line parameter n%2. &ynamically allocate an array of size n' rea n values from stin an storethe values in the array%3. rint the values of the array starting with the last element all the wayown to the )rst%

     These operations must "e one "y cloning n-1 times the original parentprocess. Each process prints the value of one element of the arraysynchronizing itself with the other processes "y means of the wait function. The values must "e printe so that the one with ine* n-1 is the )rst' thenthe one with ine* n- an so on. ,int

    1 creates a chil process an calls wait.

    creates a chil process an calls wait.

    (n-1! creates a chil process (n! an calls wait.

     (n! prints element n of the array en e*its.

    (n-1! prints element n-1 of the array an e*its.

     1 prints element 1 of the array an e*its.

    (,ere array ine*es go from 1 to n!.

    Q2: Write a C program which receives two comman line parameters' n and

    t. The parent process has to initialize a counter' create two chil processes

    an e*it. Each one of the two chil processes has to increment the counter'

    print it an create again two chil processes an terminate immeiatelyafter. The process of creating chils stops after creating 2^n  leaves

    processes. o for e*ample' if n0' the parent creates two chil processes'

    an each chil creates two processes for a total num"er of leaves

    processes. 2t this point process creation stops. The last set of chil processes

    (leaf chil processes! have to increment their counter an sleep for t

  • 8/18/2019 lab6 (1)

    2/2

    secons "efore printing it. 3e careful' each process creates two chil

    processes "ut only leaf processes are allowe to wait.

    What4s the orer in which processes terminate5 6s the orer always the

    same5