LINUX TIPS AND TRICKS --- June 08, 2001

Published by ITworld.com -- changing the way you view IT
http://www.itworld.com/newsletters
________________________________________________________________

System Calls
By Danny Kalev

Every piece of code can run in one of two contexts: user mode 
and kernel mode. A user's program normally runs in user mode. 
By contrast, device drivers and file systems run in kernel 
mode. In user mode, the code executes in a protected 
environment and thus can't damage the system's other processes. 
Code that executes in kernel mode has full access to the 
hardware and low-level system resources.

Consider a device driver. In order to operate, it must have 
unrestricted access to the device it controls. Therefore, it 
must execute in kernel mode. It provides services to processes 
running in user mode, which cannot access the device directly. 
The technical details of calling through user/kernel mode are 
usually hidden from the programmer. A user typically invokes a 
system call (syscall for short) that interacts with the 
hardware and returns a value. Superficially, syscalls look 
like ordinary C functions. However, they differ from ordinary 
functions in two important aspects:

    * Argument passing - A syscall may take only arguments of
      a native word size. Thus, when you pass a single char or
      short to a syscall, the compiler promotes the argument to
      a 32-bit word on a 32-bit system.

    * Return value - A syscall returns a signed word. Typically,
      the return value indicates the status of the operation but
      it can also be a pointer. A syscall cannot return structs
      in aggregates. Likewise, you cannot pass structs by value
      to a syscall. There is a simple workaround to this
      restriction: pass a pointer.

In most cases, the bare syscalls are wrapped in ordinary C 
functions. For example, the standard library's exit() function 
wraps the _exit() system call. The wrapper function often 
performs additional operations before and after the invocation 
of a syscall, say flushing streams, closing files and checking 
the syscalls return value. It's recommended that you use 
syscalls wrapper functions instead of calling a bare syscall 
directly whenever possible. Most syscalls are declared in the 
<unistd.h> header. Most syscalls return 0 to indicate success 
and a negative value to indicate an error. You can use the 
perror() and strerror() interfaces discussed here a few weeks 
ago to obtain a complete description of the error code returned.


About the author(s)
-------------------
Danny Kalev is a system analyst and software engineer with 
more than 10 years of experience, specializing in C++ and 
object-oriented analysis and design on various platforms 
including VMS, DOS, Windows, Unix, and Linux. His technical 
interests involve code optimization, networking, and 
distributed computing. He is also a member of the ANSI C++ 
standardization committee and the author of ANSI/ISO C++ 
Professional Programmer's Handbook (Que, 1999). Contact him 
at linuxnl@excite.com.
________________________________________________________________

ADDITIONAL RESOURCES

System Calls in Linux
http://ww.itworld.com/jump/lintps_nl/starship.python.net/crew/
gandalf/rtlinux/luz.cs.nmt.edu/%257Ebaraban/syscall.html

System Calls and Linux
http://ww.itworld.com/jump/lintps_nl/www.cronos.org/
quanta/docs/scl/
________________________________________________________________

CUSTOMER SERVICE

SUBSCRIBE/UNSUBSCRIBE:
- Go to: http://reg.itworld.com/cgi-bin/subcontent12.cgi
- Enter your email address under "Current subscriber" to log in
- Uncheck the box next to the newsletter you want to
  unsubscribe from
- Or check the box next to the newsletter you want to
  subscribe to
- Submit

If you have questions, please send email to customer service at:
mailto:support@itworld.com
________________________________________________________________

CONTACTS

* For editorial comments, write Andrew Santosusso, Associate
  Editor, Newsletters at: andrew_santosusso@itworld.com

* For advertising information, write Dan Chupka, Account
  Executive at: dan_chupka@itworld.com

* For recruitment advertising information, write Jamie Swartz,
  Eastern Regional Sales Manager at: jamie_swartz@itworld.com
  or Paul Duthie, Western Regional Sales Manager at:
  paul_duthie@itworld.com

* For all other inquiries, write Jodie Naze, Product Manager, 
  Newsletters at: jodie_naze@itworld.com
________________________________________________________________

PRIVACY POLICY
http://www.itworld.com/Privacy/

Copyright 2001 ITworld.com, Inc., All Rights Reserved.
http://www.itworld.com
