LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-24-2008, 04:53 AM   #1
piyush masrani
LQ Newbie
 
Registered: Jul 2008
Posts: 6

Rep: Reputation: 0
A command on shell which does not make system calls


Hi,
I was going through some previous assignments and found out a question:

"Find out a command on a shell such that the command does not make a system call. Use strace to locate such a command."

Can please anybody help me to find such a command.
i tried many, but didnt get any. Also, for "strace cd" it gives output as command not found. Can anybody explain why?

Thanx and regards,

Piyush
 
Old 07-24-2008, 05:05 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
strace accepts external commands as argument. cd is not a command, strictly speaking, but a shell built-in. You can verify this using:
Code:
type cd
to confirm this you can try
Code:
which cd
 
Old 07-24-2008, 05:16 AM   #3
piyush masrani
LQ Newbie
 
Registered: Jul 2008
Posts: 6

Original Poster
Rep: Reputation: 0
thanx for the reply....
but what about a command without system call...it should work as
"strace command"
and should not produce any output(system calls)
how can i find such a command?
 
Old 07-24-2008, 06:02 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by piyush masrani View Post
but what about a command without system call...(...) how can i find such a command?
Since this is an assignment, maybe you should go back to the conceptual part and wonder what sys_calls are used for (in user mode)?
 
Old 07-24-2008, 11:26 AM   #5
piyush masrani
LQ Newbie
 
Registered: Jul 2008
Posts: 6

Original Poster
Rep: Reputation: 0
sorry, i didnt get you...
 
Old 07-24-2008, 12:06 PM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Surely every action uses system calls at some point...? Does the question mean, "find a command which does not invoke an external program"?
 
Old 07-25-2008, 12:13 AM   #7
piyush masrani
LQ Newbie
 
Registered: Jul 2008
Posts: 6

Original Poster
Rep: Reputation: 0
ya...it means no external program should be invoked. To be more precise, strace should not display any system calls for the command.
 
Old 07-25-2008, 02:06 AM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Well, every command requires at least one system call - reading the command from the input file handle.
 
Old 07-25-2008, 02:21 AM   #9
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
...and ultimately _exit().
 
Old 07-25-2008, 05:59 AM   #10
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
The "time" function might help you with this assignment.
 
Old 07-25-2008, 09:35 AM   #11
piyush masrani
LQ Newbie
 
Registered: Jul 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by matthewg42 View Post
Well, every command requires at least one system call - reading the command from the input file handle.
Quote:
Originally Posted by Mr. C. View Post
...and ultimately _exit().
i agree with you..

how does time command help in here??
 
Old 07-25-2008, 02:44 PM   #12
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Shell built-ins can't be strace'd because no process is exec'd.

strace false

You will always see the libc runtime initialization/shutdown, which makes several system calls.
 
Old 07-26-2008, 07:54 AM   #13
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
You can't strace internal commands directly because strace works by taking the name of a program and some arguments to it, and executes that program (creating a new process for it). A shell-internal is by definition not a program for which a new process is created - it is just a routine which runs in the existing shell process.

Here's one way to do it indirectly, by using strace on the bash process itself. I used cat instead of letting the shell read directly from the terminal, since a non-interactive shell will be created, and that doesn't produce a huge number of signal handling calls between commands. I also turn off echo to prevent the typed input and the strace output getting mixed up:
Code:
stty -echo
cat | strace bash > /dev/null
After the initial strace output from all the system calls used to create and initialise the new process, you can type in commands and see the system calls generated. For example, the command "echo hello world" produces this output:
Code:
read(0, "e", 1)                         = 1
read(0, "c", 1)                         = 1
read(0, "h", 1)                         = 1
read(0, "o", 1)                         = 1
read(0, " ", 1)                         = 1
read(0, "h", 1)                         = 1
read(0, "e", 1)                         = 1
read(0, "l", 1)                         = 1
read(0, "l", 1)                         = 1
read(0, "o", 1)                         = 1
read(0, " ", 1)                         = 1
read(0, "w", 1)                         = 1
read(0, "o", 1)                         = 1
read(0, "r", 1)                         = 1
read(0, "l", 1)                         = 1
read(0, "d", 1)                         = 1
read(0, "\n", 1)                        = 1
write(1, "hello world\n", 12)           = 12
rt_sigprocmask(SIG_BLOCK, NULL, [], 8)  = 0
As I said before, you will get read calls. This will always be the case, for any command, so strictly speaking the answer to the question is that all commands produce system calls. There are no exceptions. However, I expect the questioner didn't mean it quite like that.

In the case of echo, you get a write system call, and this will be the case with any builtin command which produces any output.

Commands which only change the internal state of the shell like alias or setting a variable will not do this, so that might be the answer which the questioner is looking for. However, even for these commands the shell always calls rt_sigprocmask. It's arguable if this is done as part of the command, or in a wrapper around the command.

In my opinion, the question is too vague to give a precise answer to it, but with th caveats I have mentioned here, I think you can cover all your bases.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how does java calls the system calls which are written in c babu198649 Linux - General 3 12-05-2011 03:40 AM
shell example using system calls djgerbavore Programming 7 03-06-2010 02:39 AM
'sh' shell - Actually calls legacy Bourne shell, or uses system default? Dtsazza Linux - Software 1 10-28-2005 09:20 AM
Is Mount a system call or shell command? comeon Linux - Software 8 02-20-2005 11:55 PM
How to Write Own Shell in Unix using System Calls? indian Programming 2 08-05-2004 04:06 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:27 AM.

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