Understanding of Unix Programs

Ratings:
(4)
Views: 452
Banner-Img
Share this blog:

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      

1 2470 2439
2 2471 2470
2 2470 2439
3 2473 2470
4 2474 2473
4 2473 2470
3 2470 2439
4 2475 2470
4 2470 2439
3 2472 2471
3 2471 1
4 2477 2471
4 2471 1
4 2476 2472
4 2472 1

    Screenshot_4    

1 2550 2439
2 2550 2550
3 2552 2550
4 2553 2552
4 2552 2551
3 2551 2550
4 2554 2551
4 2551 2550
2 2550 2439
3 2555 2550
4 2556 2555
4 2505 2550
3 2550 2439
4 2557 2650
4 2550 2439

     

  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 : Entered Process id =  3272 Parent id = 2439
Child 1 : Entered Process id =3273 Parent id =  3272
Child 2 : Entered Process id =3274 Parent id = 3273
Child 3 : Entered Process id =3275 Parent id =3274
Child 3 : Exiting Process id =3275 Parent id =3274
Child 2 : Exiting Process id =3274 Parent id =3273
Child 1 : Exiting Process id =3273 Parent id =3274
Parent  : Exiting Process id =3272 Parent id =2439

    Screenshot_8     Screenshot_9       Output :-  

Child 3 : Entered Process id =  2054 Parent id = 2063
Child 2 : Entered Process id =2055 Parent id =  2053
Child 1 : Entered Process id =2056 Parent id = 2053
Parent  : Entered Process id =2053 Parent id =1858
Child 3 : Exiting Process id =2054 Parent id =2053
Child 2 : Exiting Process id =2055 Parent id =2053
Child 1 : Exiting Process id =2056 Parent id =2053
Parent  : Exiting Process id = 2053 Parent 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.

 

You liked the article?

Like: 0

Vote for difficulty

Current difficulty (Avg): Medium

EasyMediumHardDifficultExpert
IMPROVE ARTICLEReport Issue

About Author

Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.

Stay Updated
Get stories of change makers and innovators from the startup ecosystem in your inbox