Understanding of Unix Programs

Q. W.a. p to accept any number of commands from the command line and execute them in the same sequence as they are provided.

 

Using system () call

#include <stdio. h>

#include <stdlib. h>

Main (int arg c , char ** arg v)

{

Int I ;

If  (argc ==  2)

{

Print f (“Usage :  ./a. out         <command 1>   <command 2> ………….”);

Return ;

}

 

For (I =1 ; argv [i] ; I ++ )

System (arg v [i] );

}

 

Output

./a .out ls date cal

  1. out ass1.c ass2. C ass3. C cmd exec  cmd sys p1.c p2 thu Aug 12  00:28:23 1st 2010

August 2010

Mon   tue wed  thu   fri  sat 

 

 

EX:- 1

# include <stdio.h>

#include <unistd.h>

Main ()

{

Print f (“Before : process id  = % d , parent id = % d/n ”, get p id (), get  p pid ());

Print f (“Enter to call fork ”);

Get char ();

Fork ();

Print f (“After  : process id  = % d , parent id = % d/n ”, get p id (), get  p pid ());

}

 

Output

Before :

process id = 2069 , parent id = 1835

Enter to call fork

After :

process id = 2071, parent id = 2069

After :

process id = 2069, parent id = 1835

Main ()

1835 2 Bash

2069 2 process

 

 

Screenshot_1

 

 

Ex :-2

# include <stdio.h>

#include <unistd.h>

Main ()

{

Print f (“Before : process id  = % d , parent id = % d/n ”, get p id (), get  p pid ());

Fork ();

Fork ();

Print f (“After  : process id  = % d , parent id = % d/n ”, get p id (), get  p pid ());

}

Bash : 1835

Main ()

Process : 2156

Screenshot_2

 

Output:

Before:           process id = 2156 ,  parent id = 1835

After  :           process id = 2158 , parent id = 2157

After  :           process id = 2157 , parent id = 2156

After  :           process id = 2159 , parent id = 2156

After  :           process id = 2156 , parent id = 1839

Last print f is using  common code, so it is called duplicate process

That is sharing the common code by multiple process.

 

Observation 1. C

Main ()

{

Print f (“1 : %d %d /n”, get p id (), get p pid () );

Fork ();

Print f (“2: %d %d /n”, get p id (), get p pid () );

Fork ();

Print f (“3 : %d %d /n”, get p id (), get p pid () );

Fork ();

Print f (“14: %d %d /n”, get p id (), get p pid () );

 

Output :-

 

Screenshot_3

 

 

 

124702439
224712470
224702439
324732470
424742473
424732470
324702439
424752470
424702439
324722471
324711
424772471
424711
424762472
424721

 

 

Screenshot_4

 

 

125502439
225502550
325522550
425532552
425522551
325512550
425542551
425512550
225502439
325552550
425562555
425052550
325502439
425572650
425502439

 

 

 

  1. Observation 2.c

# include <stdio . h>

Main ()

{

Int ret;

Ret = fork ();

If (ret = = -1)

{

P error (“fork ”);

Return ;

}

Else if (ret = = 0 )

{

Print f (“Exclusive child code /n ”);

Exit (0);

}

Else

{

Print f (“Exclusive parent code/ n ”);

Exit (0);

}

}

 

Screenshot_5

 

 

Output :-

Exclusive child code

Exclusive parent code

      ↓

Fork on success returns two values “Zero ” & “Non – zero” collected by child process

     ↓

Parent process

 

4.If memory is not sufficient then fork causes error  Race condition  in O.S

If the out come of a multiprocessing environment involved with 2 tasks  “T1” & “T 2” , is different from then condition  is  occurred.

  • Child process always run in background.
  • Parent process  always run in background.

2If the parent exited before child then it is called orphan.

2If the child exited but parent is running  it is “ZOMBIE” state.

2 That is exited value is not collected by parent.

 

Screenshot_6

Screenshot_7

 

 

The exit status must be collected only by wait (); wait (), will be discussed later.

 

2W. a. p to execute an application which will be duplicated with four new jobs. That is total no. of jobs hence must be is

(i) x 2 x +1 2 x +2 2 x +3.

Hence two process should  not have common  parent id.

#include <stdio.h>

#include <unistd.h>

Main ()

{

If (fork ())

{// parent process

Print f ( “parent : entered process id : % d, parent id + % d/n), get p id (), get p pid()”);

Sleep (11);

Print f ( “parent : entered process id : % d, parent id + % d/n), get p id (), get p pid()”);

}

Else

{

//child 1 process

If (fork ())

{

Print f ( “child 1 : entered process id : % d, parent id + % d/n), get p id (), get p pid()”);

Sleep (1);

Print f ( “child 1 : entered process id : % d, parent id + % d/n), get p id (), get p pid()”);

}

Else

{

If (fork ())

{// child 2 process

Print f ( “child 2 : entered process id : % d, parent id + % d/n), get p id (), get p pid()”);

Sleep (3);

Print f ( “child 2: entered process id : % d, parent id + % d/n), get p id (), get p pid()”);

}

Else

{//child 3 process

 

Print f ( “child 3: entered process id : % d, parent id + % d/n), get p id (), get p pid()”);

Sleep (2);

Print f ( “child 3 : entered process id : % d, parent id + % d/n), get p id (), get p pid()”);

}

}

}// end of ‘ else ’  paired with main ‘if’

}// end  of main

 

Output

 

Parent : EnteredProcess id =  3272Parent id = 2439
Child 1 : EnteredProcess id =3273Parent id =  3272
Child 2 : EnteredProcess id =3274Parent id = 3273
Child 3 : EnteredProcess id =3275Parent id =3274
Child 3 : ExitingProcess id =3275Parent id =3274
Child 2 : ExitingProcess id =3274Parent id =3273
Child 1 : ExitingProcess id =3273Parent id =3274
Parent  : ExitingProcess id =3272Parent id =2439

 

 

Screenshot_8

 

 

Screenshot_9

 

 

 

Output :-

 

Child 3 : EnteredProcess id =  2054Parent id = 2063
Child 2 : EnteredProcess id =2055Parent id =  2053
Child 1 : EnteredProcess id =2056Parent id = 2053
Parent  : EnteredProcess id =2053Parent id =1858
Child 3 : ExitingProcess id =2054Parent id =2053
Child 2 : ExitingProcess id =2055Parent id =2053
Child 1 : ExitingProcess id =2056Parent id =2053
Parent  : ExitingProcess id = 2053Parent id =1853

 

 

2W. a. p to execute pwd , cal, date by child 1 , child 2 & child 3 with random delays and observe the sequence  of output

#include <stdio.h>

#include <unistd.h>

Main ()

{

If (fork ())

{

If (fork ())

{

Int t ;

Brand (get p id ());

T = rand () % 10 +1;

Sleep (t);

Execlp (“pwd”, ‘pwd ’, NULL);

}

Else

{

Int  t ;

Brand (get p id ());

T = rand () % 10 +1;

Sleep (t);

Execlp (“cal”, ‘cal ’, NULL);

}

}

Else

{

Int t ;

Brand (get p id ());

T = rand () % 10 +1;

Sleep (t);

Execlp (“date”, ‘date ’, NULL);

}

}

Here we can declare ‘t’ as  common local / global variable. It does not be a problem due to concept of “copy – on – write”.

Output :-

July 2011

SU   MO TU  WE  TH  FR  ST

Sat Jul 284:05:34 1st 201

/home /b59p2/UNIX/fork

 

Name

# include <stdlib.h>

Rand, rand –r, brand – pseudo- random number generator int rand (void) ;

Int rand-r  (unsigned int * seed p);

Void brand  (unsigned int * seed);

 

Return Value

The  rand () and rand – r function return a value between 0 and RAND – MAX.

The brand () function return no  value.

  • The rand () function returns a pseudo- random number generator integer between 0 and RAND – MAX.
  • The Brand () function sets its argument as the seed for a new sequence of pseudo- random number generator integers to be returned  by rand ()
  • These sequence are repeatable by calling brand() with same seed value.
  • If no seed value is provided , the rand () function is automatically seeded with a value of 1.

 

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Understanding of Unix Programs"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    Support


    Please leave a message and we'll get back to you soon.
    Three + 6