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 10-26-2010, 11:10 PM   #1
AlbertJJ
LQ Newbie
 
Registered: Oct 2010
Posts: 12

Rep: Reputation: 0
System Calls


What is the real difference between system calls and normal function calls. Ultimately function calls too would be passed to kernel for some or the other work.
 
Old 10-26-2010, 11:43 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by AlbertJJ View Post
... Ultimately function calls too would be passed to kernel for some or the other work.
Why do you think so ?

If I write a function:

Code:
int add_2_ints(int a, int b)
  {
  return a + b;
  }
...
// in 'main':

int sum = add_2_ints(2, 3);
, where exactly is the OS kernel involved ?
 
Old 10-26-2010, 11:54 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Quote:
Q: What is the real difference between system calls and normal function calls?
A: There is a HUGE difference.
The latter operates in the context of the user process. The former jumps to a completely different context: from "user space" into "kernel space".

This link describes it in more detail:
http://en.wikipedia.org/wiki/System_call

Quote:
Q: Ultimately function calls too would be passed to kernel for some or the other work?
Wrong: not every function call ends up as a system call. Not by a long shot Sergei's example is one of many illustrating this fact.

The converse, however, is accurate. Every system call you make (for example, the syscall to "_open") is likely to start out as a function call from your program. See the section on "The library as an intermediary" in the above link.

'Hope that helps
 
Old 10-26-2010, 11:59 PM   #4
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
If I write a code as follows:
Code:
int main ()
{
   return 0;
}
Will this return 0 statement not call any system call in return ?
 
Old 10-27-2010, 12:48 AM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by anishakaul View Post
If I write a code as follows:
Code:
int main ()
{
   return 0;
}
Will this return 0 statement not call any system call in return ?
'main' is a special function, upon its completion the kernel is notified that the task is no more needed, so it should be deleted from the task queue(s), etc.
 
1 members found this post helpful.
Old 10-27-2010, 01:07 AM   #6
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by AlbertJJ View Post
Ultimately function calls too would be passed to kernel for some or the other work.
Then I think after reading the Sergei's post above, the above quote should have been re-framed as follows to make more sense:

Quote:
Ultimately function calls (which do not include the user defined functions but do include the C/C++ etc standard library functions) too would be passed to kernel for some or the other work.
Correct me if I am wrong.
 
Old 10-27-2010, 01:47 AM   #7
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Ultimately function calls (which do not include the user defined functions but do include the C/C++ etc standard library functions) too would be passed to kernel for some or the other work.

Correct me if I am wrong.
You're wrong

No offense - but Sergei's example is a great.

And if you wanted another example (an example in the Standard C library), then look no further than "strlen()" .

I repeat what I said above:
Q: What is the real difference between system calls and normal function calls?
A: There is a HUGE difference. By definition, a "system call" changes context from user space to system space. A function call, on the other hand, is executed from user space.

Q: Ultimately function calls too would be passed to kernel for some or the other work?
A: Not every function call ends up as a system call.
In fact, probably FEW ever do.

PS:
"return", as I'm sure you know, is a C/C++ *operator*. As such, it's outside the scope of this discussion.

If you meant "function main()", you can easily create a function that makes NO system calls (e.g. "void main () { ; }"). The PROGRAM might make system calls (in both the preamble and exit code) ... but the FUNCTION doesn't.

Last edited by paulsm4; 10-27-2010 at 01:51 AM.
 
Old 10-27-2010, 09:45 AM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by paulsm4 View Post
...
PS:
"return", as I'm sure you know, is a C/C++ *operator*. As such, it's outside the scope of this discussion.
...
It's actually moot, and I'm not in the mood to dig the C99 standard at the moment, but my vague memories from the late eighties (and from used then Borland Turbo-C) tell me that the compiler may in reality compile 'main' quite differently from other functions. I.e. seeing 'return' in 'main' the compiler might decide not to translate 'return' the usual (for other functions) way, but rather to directly make system 'exit' call.

Again, it's not a "hard claim", but a possibility to consider.
 
  


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
how does java calls the system calls which are written in c babu198649 Linux - General 3 12-05-2011 03:40 AM
system calls with C khodeir Programming 4 03-12-2009 08:26 PM
system calls goldeneagle1234 Linux - Newbie 1 09-14-2008 04:52 AM
system calls arispipis Programming 2 03-19-2008 06:59 PM
how do you use system calls? slinky2004 General 1 12-22-2005 09:39 PM

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

All times are GMT -5. The time now is 02:30 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