LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 02-06-2018, 12:37 AM   #1
dvirginz
LQ Newbie
 
Registered: Feb 2018
Posts: 3

Rep: Reputation: Disabled
Does every call to write sends switches to kernel mode?


I know that a call to the glibc "write" function calls in it's turn to the sys_call write function which is a kernel function.
because sys_call is a kernel function the CPU has to change the ring to zero store the processes registers and so on.
But does it always switches to kernel mode? for example, if i do

write(-1,buffer,LENGTH)

does it still tries to find it in the file descriptors array?
I see in the glibc source code that it does check for fd>0 but i don't see any jump to the sys_call there (it seems like the baracks for main() ends before any call to the alias_write.


/* Write NBYTES of BUF to FD. Return the number written, or -1. */
ssize_t
__libc_write (int fd, const void *buf, size_t nbytes)
{
if (nbytes == 0)
return 0;
if (fd < 0)
{
__set_errno (EBADF);
return -1;
}
if (buf == NULL)
{
__set_errno (EINVAL);
return -1;
}

__set_errno (ENOSYS);
return -1;
}
libc_hidden_def (__libc_write)
stub_warning (write)

weak_alias (__libc_write, __write)
libc_hidden_weak (__write)
weak_alias (__libc_write, write)
#include <stub-tag.h>

So the question is both:

Where does the glibc actually calls the sys_write
Is it true that glibc doesn't call the sys_write if fd<0?
 
Old 02-06-2018, 07:44 AM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
This post on StackOverflow answers your question exactly:
Quote:
Originally Posted by That Other Site:
What you've found is a stub function for systems it's not implemented on. You need to look under the sysdeps tree for the actual implementation.

Note that the actual system calls aren't defined anywhere in the source tree - they're generated at build time from syscalls.list (linked is the one in sysdeps/unix, there are additional ones further down), a series of macros in sysdep.h (linked linux/i386), and a script that actually generates the source files.


To answer your original question, yes, a system-call ordinarily does occur. Or, depending on the architecture, something equivalent to a system call. Disk I/O in particular is a complicated process which makes heavy use of "buffering." Your process is typically allowed to continue while the actual write takes place asynchronously. (Although it can wait. sqlite often does this.)

Last edited by sundialsvcs; 02-06-2018 at 08:00 AM.
 
Old 02-06-2018, 07:58 AM   #3
dvirginz
LQ Newbie
 
Registered: Feb 2018
Posts: 3

Original Poster
Rep: Reputation: Disabled
So just for the clarification,
even if i sent write(-1,0,buffer) linux will not identify the problem in user space, and will switch to kernel space?
although the glibc checks came false?

Last edited by dvirginz; 02-06-2018 at 08:05 AM.
 
Old 02-06-2018, 08:37 AM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by dvirginz View Post
So just for the clarification,
even if i sent write(-1,0,buffer) linux will not identify the problem in user space, and will switch to kernel space?
although the glibc checks came false?
I believe you could run your program under strace and answer that yourself in just a few minutes.
 
Old 02-06-2018, 08:58 AM   #5
dvirginz
LQ Newbie
 
Registered: Feb 2018
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rknichols View Post
I believe you could run your program under strace and answer that yourself in just a few minutes.
I'm trying to see if the libc function executed the int 0x80 (or any other form to change to kernel mode) call and changed to kernel mode.
sorry for the ignorance, but how can i do it with strace?
 
Old 02-06-2018, 12:20 PM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by dvirginz View Post
I'm trying to see if the libc function executed the int 0x80 (or any other form to change to kernel mode) call and changed to kernel mode.
sorry for the ignorance, but how can i do it with strace?
That's exactly what strace traces -- system calls. If you wanted to trace calls to the write(2) library function, you would use ltrace.
 
Old 02-06-2018, 08:02 PM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
And why do you care ?.
Context switching takes place all the time. Literally. vmstat tells you that, but for the absolute number, try the following - repeat a couple of seconds later.
Code:
grep ctxt /proc/stat
 
Old 02-07-2018, 12:07 PM   #8
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
The system often spends more time in kernel mode than in user mode. (Still executing on behalf of the current process/thread.) All sorts of things require a system-call.

The x86 architecture implements call gates to provide more-direct transfers. They've since sort-of fallen out in favor of other mechanisms, but the essential notion is the same. User-land programs have frequent need to call specific subroutines (as determined solely by the OS), such that there is a perceived need to make the transition as rapid as possible.

A "system call" really is "a subroutine call," although it includes a change in the processor's privilege-level and is a one-way street: the requesting user-land program cannot make arbitrary calls into kernel space. Also, when the kernel switches back to user-land mode, it might have switched from one process/thread to another.

"This being Linux," you have all of the source-code that implements this magic on various architectures, and I suggest that it can be very, very informative and educational to "peek behind the curtain" and see what actually happens next.

Last edited by sundialsvcs; 02-07-2018 at 12:10 PM.
 
  


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 to write a system call which returns current system time using call by reference mukul2kul4 Debian 2 09-25-2011 11:17 PM
how can I control the HDD using read(), write() in the kernel mode...? chxooi Linux - Embedded & Single-board computer 1 05-10-2010 05:10 PM
LXer: Sun Cofounder Switches Into Startup Mode LXer Syndicated Linux News 0 10-25-2008 04:20 AM
configure script sends bad switches to daughter configure scripts Wells Linux - Software 7 10-10-2008 03:38 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

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