Daemon process in UNIX

Daemon process

At   TTY – if we observe ‘?’ , it indicates it a Daemon  process, which does not need any input and automatically comes  into life when the OS is switched ON.   And  the processes can be seen using commands.

 

Gnome – terminal

Is the parent for the bash.

 

Example on System () call

Vi p1.c

#include <stdio.h>

#include <stdlib.h>

Main ()

{

Print f (“Process id = % d………parent id = %d /n”,  get p id (), get p p id ())

System (“./p2 ”);// p2 is executable file of p 2.c

Print f (“After system () call came back to process 1 ”);

}

 

Vi p2.c

 

#include <stdio.h>

#include <stdlib.h>

Main ()

{

Print f (“Process id = % d………parent id = %d /n”,  get p id (), get p p id ());

}

Cc  -0 p2              p2.c

Creates executable for p2.c with name p2.

 

 

Output      cc p1.c

# ./a.out

Process  1 id = 2963 ……….parent id = 2767 à this is PID of gnome – terminal

Process 2 id = 2964……….parent id = 2763 à this is PID process 1 as process1, called process 2

After System () call came back to process 1 .

 

$echo $PATH

/ user/ Kerberos/bin : /ur/local/bin

:/bin : /user/bin : /home /b59p2/bin

These  are the paths available in the terminal if we have two executes like ls, cal etc………

Simply by $ ls, $ cal we can execute, but even through  a. out is an executable , $ a. out does not executes why ?

$ . pwd

/home/b59p2/UNIX/Sys – exec

This is path of you’re a .out, Which is not available in above shown paths. Hence, it searches for a. out there in above paths  & hence not found, Results, bash : a. out : command not found.

Now to execute directly like # a. out , we have two solutions.

  1. Copy a. out in any of directory path

# cs    /home/b59p2/UNIX/Sys- exec/ a. out   /home /b59p2/bin # a. out

  1. Another way is to update the PATH variable

# PATH = $ PATH : /root/b59p2 /UNIX/sys-exec # a. out

  1. $ / bin/sh -c command
  2. $ command

Ex :-  $/bin / sh  -c ls

$ ls                                   Both generates same output

 

Here the major difference is that, in first command  bash creates sh and executes command is returns.

 

In 2nd  statement executes command in present shell Bash is parent.

 

 

 Assignments

  1. create an executable named “cme exec ” , which when used with the i/p command , should be also to execute the command specified..the command is provide from the command line Vi ass1.c

Using System () call

 

#include   <stdio . h>

#include   <stdlib . h>

Main (int  argc, char ** argc )

{

If (argc ! = 2)

{

Print f (“usage ./cmd exec  <command >” );

Return;

}

System ( arg v [1]);

}

creating  executable

cc         -0          cmd exec          ass1.c

 

Output           ./cmd exec ls

  1. a. out ass1.c cmd exec

p1.c p2 p2.c

 

 

Using exec () call

 

#include <stdio. h>

#include <stdlib. h>

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

{

If  (argc ! 2)

{

Print f (“Usage :  ./cmd exec          <command> ”);

Return ;

}

Execlp (  args v [1], argv [1], NULL);

}

 

  1. Q. 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

“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 Daemon process in UNIX"

    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