LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-30-2016, 12:29 AM   #1
linux4evr5581
Member
 
Registered: Sep 2016
Location: USA
Posts: 275

Rep: Reputation: Disabled
Understanding System Calls


Is my underatanding of system calls correct? Its that programs interact with the kernel indirectly by having its opcodes interpreted by the cpu and then transferred to the system library. These opcodes (which are stored in the program's executable file) contain the instructions on what resources it needs to be able run, and the kernel provides those resources through the system's libary system calls...

Last edited by linux4evr5581; 10-30-2016 at 04:09 AM.
 
Old 10-30-2016, 12:32 AM   #2
Jjanel
Member
 
Registered: Jun 2016
Distribution: any&all, in VBox; Ol'UnixCLI; NO GUI resources
Posts: 999
Blog Entries: 12

Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
Sounds good. http://wikipedia.org/wiki/System_call
 
Old 10-30-2016, 12:43 AM   #3
linux4evr5581
Member
 
Registered: Sep 2016
Location: USA
Posts: 275

Original Poster
Rep: Reputation: Disabled
You responded quickly and rephrased the question a bit, I apologize
 
Old 10-30-2016, 01:03 AM   #4
Jjanel
Member
 
Registered: Jun 2016
Distribution: any&all, in VBox; Ol'UnixCLI; NO GUI resources
Posts: 999
Blog Entries: 12

Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
No need to apologize! I liked the question! I found another link and added it to my collection
So I (&others someday) thank you!
https://www.cs.fsu.edu/~lacher/cours...ay+answers.pdf See #17-18.

Yes, to put it kinda loosely (I'm sure better experts wll 'chime in')
programs can call like read(3), which calls read(2), which does an INT cpu instruction, which causes the kernel to do the 'physical' I/O.

man man will list the 8 man 'sections': (2) syscall; (3) library
man 2 intro is a good read. And man 3 intro

Feel free to ask more specifics... p.s. I edit while others 'ninja' too

EDIT: oh, I think you mean exec/fork(2): e.g. the shell does to start programs you run.
http://www.tldp.org/LDP/tlk/kernel/processes.html

Last edited by Jjanel; 10-30-2016 at 01:23 AM.
 
Old 10-30-2016, 01:35 AM   #5
linux4evr5581
Member
 
Registered: Sep 2016
Location: USA
Posts: 275

Original Poster
Rep: Reputation: Disabled
I think the forking operation you mentioned is for forking processes, not nessesarily system calls..
 
Old 10-30-2016, 02:31 AM   #6
Jjanel
Member
 
Registered: Jun 2016
Distribution: any&all, in VBox; Ol'UnixCLI; NO GUI resources
Posts: 999
Blog Entries: 12

Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
Hmmmm... I think we're now 'diverging'... Let's try to clarify...
(I'm not good at writing, so I try to find places where others have said it much better)

>programs requests what it needs to be able run
Did you mean 'resources'? A program=process (loosely) just needs memory, then cpu (I think)
These are the other functions=actions (loosely) that the kernel can do, for pgms:
http://wikipedia.org/wiki/System_cal...f_system_calls (I/O, IPC)

>the library reads the program's opcodes?
Not 'opcodes': arguments=parameters=details (loosely) on what specifically the pgm wants.

Let me [us] know...
 
Old 10-30-2016, 03:00 AM   #7
linux4evr5581
Member
 
Registered: Sep 2016
Location: USA
Posts: 275

Original Poster
Rep: Reputation: Disabled
Its my understanding that a program is anything that contains executable code. But reguarding opcodes they are basically machine code stored in a programs executable file that the cpu interprets as instructions on what the program needs done, and the cpu directs those instructions to the appropriate location on the data path in the cpu (I think)... Depending on the program they require resources such as access to certain files or ports etc..

Last edited by linux4evr5581; 10-31-2016 at 11:54 AM.
 
Old 10-30-2016, 03:23 AM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Tribblix, Ubuntu/WSL
Posts: 9,771

Rep: Reputation: 468Reputation: 468Reputation: 468Reputation: 468Reputation: 468
Quote:
Originally Posted by linux4evr5581 View Post
Its my understanding that a program is anything that contains executable code.
That's a reasonable definition. Note that this executable code might be directly executable by the CPU or require an interpreter to be run.
Quote:
And opcodes are basically machine code that the cpu interprets as instructions on what the program does
Yes. Opcodes are low level instructions, i.e. one or more bytes defining what the CPU need to do. They are often followed by operands.
Quote:
and the cpu directs those instructions to the appropriate location on the data path...(I think)
It is unclear what you mean here.

Back to your original question, system calls are basically special library functions that when called do temporarily switch a process thread from userland mode to kernel mode. Each system call perform a specific operation and when done, returns a status and possibly data to the caller.
 
Old 10-30-2016, 03:45 AM   #9
linux4evr5581
Member
 
Registered: Sep 2016
Location: USA
Posts: 275

Original Poster
Rep: Reputation: Disabled
Thanks for your response jlliagre I actually understand what system calls are/do (kind of a misleading title now that I think of it), but you made my understanding a little more clear anyhow.. But im actually trying to understand the exact steps of how all this occurs. Like a detailed 123 steps here's what happens when a program needs something.

Last edited by linux4evr5581; 10-30-2016 at 03:48 AM.
 
Old 10-30-2016, 03:51 AM   #10
Jjanel
Member
 
Registered: Jun 2016
Distribution: any&all, in VBox; Ol'UnixCLI; NO GUI resources
Posts: 999
Blog Entries: 12

Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
opcode "is the portion of a machine language instruction that specifies the operation to be performed. Beside the opcode itself, most instructions also specify the data they will process, in the form of operands."

A pgm/code can do 'user mode' code=instructions, but not I/O etc 'privileged' instructions. A syscall(2) uses the INT 'opcode' to get the kernel to handle the privileged action. Here's something I found that does a good job of explaining "Interrupts and System Calls", on page 4.
http://www.cs.kun.nl/J.Hooman/DES/LinuxBasics.pdf
Much deeper details here: http://users.cms.caltech.edu/~donnie...CS124Lec14.pdf

Look into ltrace/strace. You 'could' write an assembly-language (machine code) pgm that does the same as the user-mode syscall(2), including the INT instruction! But 'library' functions make it easier.
For example, a Java/... 'draw button' calls a Java lib fcn, which may call some graphics lib fcns, which would make lots of calls to another layer of lib fcns, which would make lots of write syscalls to the kernel, for the kernel to pass on to the video driver, to do the actual hwd Output instructions to the video card, to put it loosely... You'd have to write/do many thousands of machine/assembly instructions, without all the 'library functions'.
It's all "user mode", until the syscall does the INT, to get the kernel to do the desired privileged function (I/O cpu instructions). The mode transition is where the write(2)(...pixels...) does an INT, which the kernel catches, and passes the (...pixels...) args to write fcn *INside the kernel code*, which calls the driver code, which does the I/O instructions.

Whew... Now lets hear a 'guru' explain it much better Oh, we did, while I was trying to ramble/mumble all this!

Last edited by Jjanel; 10-30-2016 at 04:03 AM.
 
Old 10-30-2016, 04:19 AM   #11
linux4evr5581
Member
 
Registered: Sep 2016
Location: USA
Posts: 275

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Jjanel View Post
opcode "is the portion of a machine language instruction that specifies the operation to be performed. Beside the opcode itself, most instructions also specify the data they will process, in the form of operands."

A pgm/code can do 'user mode' code=instructions, but not I/O etc 'privileged' instructions. A syscall(2) uses the INT 'opcode' to get the kernel to handle the privileged action. Here's something I found that does a good job of explaining "Interrupts and System Calls", on page 4.
http://www.cs.kun.nl/J.Hooman/DES/LinuxBasics.pdf
Much deeper details here: http://users.cms.caltech.edu/~donnie...CS124Lec14.pdf

Look into ltrace/strace. You 'could' write an assembly-language (machine code) pgm that does the same as the user-mode syscall(2), including the INT instruction! But 'library' functions make it easier.
For example, a Java/... 'draw button' calls a Java lib fcn, which may call some graphics lib fcns, which would make lots of calls to another layer of lib fcns, which would make lots of write syscalls to the kernel, for the kernel to pass on to the video driver, to do the actual hwd Output instructions to the video card, to put it loosely... You'd have to write/do many thousands of machine/assembly instructions, without all the 'library functions'.
It's all "user mode", until the syscall does the INT, to get the kernel to do the desired privileged function (I/O cpu instructions). The mode transition is where the write(2)(...pixels...) does an INT, which the kernel catches, and passes the (...pixels...) args to write fcn *INside the kernel code*, which calls the driver code, which does the I/O instructions.

Whew... Now lets hear a 'guru' explain it much better Oh, we did, while I was trying to ramble/mumble all this!
So INT handles the privileged operations, cool didnt know that.. Yeah tho I saw a video where they show you can use strace, ftrace, and perf to see how the kernel and programs interact, and recently I saw theres now Dtrace avaliable on linux.. But to actually write your own micro kernel i guess would take alot of manual code as I think you stated..

Last edited by linux4evr5581; 10-30-2016 at 04:21 AM.
 
Old 10-30-2016, 04:28 AM   #12
Jjanel
Member
 
Registered: Jun 2016
Distribution: any&all, in VBox; Ol'UnixCLI; NO GUI resources
Posts: 999
Blog Entries: 12

Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
INT/trap causes a 'hardware interrupt', which turns/'hwd-jumps' cpu control to kernel code.
So, the user-mode code stops there (temporarily, until the kernel re-dispatches it), until the priv fcn is done.

"A trap usually results in a switch to kernel mode, wherein the operating system performs some action before returning control to the originating process"

Maybe that 'magic' will make it all make more sense!

Great to see you digging all the way down into understanding all this!

Last edited by Jjanel; 10-30-2016 at 04:40 AM.
 
1 members found this post helpful.
Old 10-30-2016, 04:35 AM   #13
linux4evr5581
Member
 
Registered: Sep 2016
Location: USA
Posts: 275

Original Poster
Rep: Reputation: Disabled
So basically INT/trap switces the cpu into kernel mode which allows the cpu to execute any I/O operation (usually I take it priviledged operations).. And userland operations are done solely in userland mode through the instructions of basic pgm/code of the programs... Sweet learn something new everyday!

Last edited by linux4evr5581; 10-30-2016 at 04:55 AM.
 
Old 10-30-2016, 04:53 AM   #14
Jjanel
Member
 
Registered: Jun 2016
Distribution: any&all, in VBox; Ol'UnixCLI; NO GUI resources
Posts: 999
Blog Entries: 12

Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
Not quite... it switches cpu from running user-mode code to kernel-mode code.
The kernel is 'just' a pgm too! Like you are processing state tax instructions,
and a state instruction 'interrupts' you, to go process the federal tax instructions.
There's only 1 you=cpu, but user=state and kernel=federal instructions to 'do'.
Loose, but ...

Last edited by Jjanel; 10-30-2016 at 04:55 AM.
 
Old 10-30-2016, 04:57 AM   #15
linux4evr5581
Member
 
Registered: Sep 2016
Location: USA
Posts: 275

Original Poster
Rep: Reputation: Disabled
Yeah i edited my post before you replied lol, I figured that out. I noticed my mistake when I realized how could kernel do anything without a processer...And good analogy tho btw makes sense!

Last edited by linux4evr5581; 10-30-2016 at 05:15 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 04:40 AM
System Calls AlbertJJ Programming 7 10-27-2010 10:45 AM
system calls with C khodeir Programming 4 03-12-2009 09:26 PM
system calls goldeneagle1234 Linux - Newbie 1 09-14-2008 05:52 AM
Some system calls Spooky Programming 1 11-24-2004 11:17 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:23 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration