LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-10-2006, 08:19 PM   #1
Chowroc
Member
 
Registered: Dec 2004
Posts: 145

Rep: Reputation: 15
shell script: Why does exec start a process with new PID?


for example:
Code:
#!/bin/sh

_pid=$$
echo $_pid
exec /sbin/syslogd -m 0

[root@LFS0 ~]# ./_test.sh     
7497
[root@LFS0 ~]# ps aux | grep syslogd
root      7498  0.0  0.4  3468  620 ?        Ss   10:43   0:00 /sbin/syslogd -m 0
root      7500  0.0  0.3  5596  516 pts/0    R+   10:43   0:00 grep syslogd
But the man says "If command is specified, it replaces the shell. No new process is created."

What's wrong?

Thanks.

Last edited by Chowroc; 01-10-2006 at 08:34 PM.
 
Old 01-11-2006, 12:27 AM   #2
cyberjun
Member
 
Registered: Mar 2005
Distribution: Gentoo kernel 2.6.14
Posts: 96

Rep: Reputation: 15
Hi,
Nice question. I think I might be able to explain
The catch is the syslogd process. I am sure it forks to go into the background. To verify do the following:
1. Create a similar script like :
Code:
localuser@localhost ~ $ cat see.sh
#!/bin/sh

_pid=$$
echo $_pid
exec yes > /dev/null
2. The above script when executed will not fork into the background. So you will have to execute the "ps aux | grep yes" from another terminal.

Hopefully, this time the PIDs will match. I tried on my own machine and it does show the same PIDs.

cheers,
--cyberjun
 
Old 01-11-2006, 03:27 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
It works OK!

Code:
billym.primadtpdev>echo $$
20726
billym.primadtpdev>exec
billym.primadtpdev>echo $$
20726
billym.primadtpdev>ksh
billym.primadtpdev>echo $$
20994
billym.primadtpdev>exec
billym.primadtpdev>echo $$
20994
billym.primadtpdev>exit
billym.primadtpdev>echo $$
20726
 
Old 01-11-2006, 04:26 AM   #4
Chowroc
Member
 
Registered: Dec 2004
Posts: 145

Original Poster
Rep: Reputation: 15
I think this is caused by the daemon. Commanly, a daemon (write by C) will fork() a new process before the exec().
 
Old 01-11-2006, 05:55 AM   #5
ajs318
LQ Newbie
 
Registered: Apr 2004
Location: East Midlands
Distribution: Debian SID; Mandrake
Posts: 17

Rep: Reputation: 0
The manpage is right: exec keeps the PID. However, standalone daemons typically fork a new process when they start. This is necessary so that the process can detach itself from STDIN, STDOUT and STDERR, and return control to the shell or script which launched it. After the fork, the parent process just exits cleanly; the child process is the one that does the real work.

You are just catching the state of the process table after the fork, which is correctly showing the PID of the child process.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting PID of running process in C/C++ laikos Programming 16 10-30-2015 06:16 PM
Getting pid of a process!! vishamr2000 Programming 34 03-12-2015 07:12 AM
Start a Process with dedicated PID murder Linux - Newbie 5 08-15-2005 03:49 PM
how to get more process' info according to its PID? iclinux Programming 1 02-05-2005 05:30 AM
How can I create a pid file from a backgournd process that I start? Hackiller Linux - General 3 05-12-2004 02:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration