⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 0737-0740.html

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 HTML
字号:
<HTML>



<HEAD>

<TITLE>Linux Complete Command Reference:System Calls:EarthWeb Inc.-</TITLE>

</HEAD>

<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
        var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>

 -->




<!-- ISBN=0672311046 //-->

<!-- TITLE=Linux Complete Command Reference//-->

<!-- AUTHOR=Red Hat//-->

<!-- PUBLISHER=Macmillan Computer Publishing//-->

<!-- IMPRINT=Sams//-->

<!-- CHAPTER=02 //-->

<!-- PAGES=0737-0890 //-->

<!-- UNASSIGNED1 //-->

<!-- UNASSIGNED2 //-->



<P><CENTER>

<a href="../ch01/0661-0736.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0741-0743.html">Next</A></CENTER></P>

















<A NAME="PAGENUM-737"><P>Page 737</P></A>





<H3><A NAME="ch02_ 1">

Part II:

</A></H3>



<H2>



System Calls



</H2>







<A NAME="PAGENUM-738"><P>Page 738</P></A>





<H3><A NAME="ch02_ 2">

intro

</A></H3>



<P>intro&#151;Introduction to system calls

</P>



<P><B>

DESCRIPTION

</B></P>



<P>This chapter describes the Linux system calls.

</P>



<P><B>

CALLING DIRECTLY

</B></P>



<P>In most cases, it is unnecessary to invoke a system call directly, but there are times when the standard C library does

not implement a nice function call for you.

</P>



<P><B>

SYNOPSIS

</B></P>



<!-- CODE SNIP //-->

<PRE>

#include &lt;linux/unistd.h&gt;

</PRE>

<!-- END CODE SNIP //-->



<P>A _syscall macro<BR>

Desired system call

</P>



<P><B>

SETUP

</B></P>



<P>The important thing to know about a system call is its prototype.

You need to know how many arguments, their types,

and the function return type. Six macros make the actual call into the system easier. They have the form

</P>



<!-- CODE SNIP //-->

<PRE>

syscallX(type,name,type1,arg1,type2,arg2,...)

</PRE>

<!-- END CODE SNIP //-->



<P>where

</P>



<TABLE>



<TR><TD>

X

</TD><TD>

0_5, which are the number of arguments taken by the system call

</TD></TR><TR><TD>

type

</TD><TD>

The return type of the system call

</TD></TR><TR><TD>

name

</TD><TD>

The name of the system call

</TD></TR><TR><TD>

typeN

</TD><TD>

The Nth argument's type

</TD></TR><TR><TD>

argN

</TD><TD>

The name of the Nth argument

</TD></TR></TABLE>



<P>These macros create a function called name with the arguments you specify. Once you include

_syscall() in your source file, you call the system call by

name.

</P>



<P><B>

EXAMPLE

</B></P>



<!-- CODE //-->

<PRE>

{

            struct sysinfo s_info;

            int error;



            error = sysinfo(&amp;s_info);

            printf(&quot;code error = %d\n&quot;, error);

            printf(&quot;Uptime = %ds\nLoad: 1 min %d / 5 min %d / 15 min %d\n&quot;

                       &quot;RAM: total %d / free %d / shared %d\n&quot;

                       &quot;Memory in buffers = %d\nSwap: total %d / free  %d\n&quot;

                       &quot;Number of processes = %d\n&quot;,

                 s_info.uptime, s_info.loads[0],

                 s_info.loads[1], s_info.loads[2],

                 s_info.totalram, s_info.freeram,

                 s_info.sharedram, s_info.bufferram,

                 s_info.totalswap, s_info.freeswap,

                 s_info.procs);

            return(0);

       }

</PRE>

<!-- END CODE //-->





<A NAME="PAGENUM-739"><P>Page 739</P></A>







<P><B>

SAMPLE OUTPUT

</B></P>



<!-- CODE //-->

<PRE>

code error = 0

uptime = 502034s

Load: 1 min 13376 / 5 min 5504 / 15 min 1152

RAM: total 15343616 / free 827392 / shared 8237056

Memory in buffers = 5066752

Swap: total 27881472 / free 24698880

Number of processes = 40

</PRE>

<!-- END CODE //-->



<P><B>

NOTES

</B></P>



<P>The _syscall() macros do not produce a prototype. You might have to create one, especially for C++ users.

</P>



<P>System calls are not required to return only positive or negative error codes. You need to read the source to be sure how

it will return errors. Usually, it is the negative of a standard error code, for example,

_EPERM. The _syscall() macros will return the result

r of the system call when r is nonnegative, but will return

_1 and set the variable errno to _r when r is negative.

</P>



<P>Some system calls, such as mmap, require more than five arguments. These are handled by pushing the arguments on the

stack and passing a pointer to the block of arguments.

</P>



<P>When defining a system call, the argument types

must be passed by value or by pointer (for aggregates such as structs).

</P>



<P><B>

FILES

</B></P>



<!-- CODE SNIP //-->

<PRE>

/usr/include/linux/unistd.h

</PRE>

<!-- END CODE SNIP //-->



<P><B>

AUTHORS

</B></P>



<P>Look at the header of the manual page for the author(s) and copyright conditions. Note that these can be different from

page to page.

</P>



<P>Linux 1.2.13, 22 May 1996

</P>



<H3><A NAME="ch02_ 3">

exit

</A></H3>



<P>exit&#151;Terminate the current process

</P>



<P><B>

SYNOPSIS

</B></P>



<!-- CODE SNIP //-->

<PRE>

#include &lt;unistd.h&gt;

void exit(int status);

</PRE>

<!-- END CODE SNIP //-->



<P><B>

DESCRIPTION

</B></P>



<P>exit terminates the calling process immediately. Any open file descriptors belonging to the process are closed; any children

of the process are inherited by process 1, init, and the process's parent is sent a

SIGCHLD signal.

</P>



<P>status is returned to the parent process as the process's exit status and can be collected using one of the

wait family of calls.

</P>



<P><B>

RETURN VALUE

</B></P>



<P>exit never returns.

</P>



<P><B>

CONFORMS TO

</B></P>



<P>SVID, AT&amp;T, POSIX, X/OPEN, BSD 4.3

</P>



<P><B>

NOTES

</B></P>



<P>exit does not call any functions registered with the ANSI C

atexit function and does not flush standard I/O buffers. To

do these things, use exit(3).

</P>



<A NAME="PAGENUM-740"><P>Page 740</P></A>





<P><B>

SEE ALSO

</B></P>



<!-- CODE SNIP //-->

<PRE>

fork(2), execve(2), waitpid(2), wait4(2), kill(2), wait(3), exit(3)

</PRE>

<!-- END CODE SNIP //-->



<P>Linux, 21 July 1993

</P>



<H3><A NAME="ch02_ 4">

accept

</A></H3>



<P>accept&#151;Accept a connection on a socket

</P>



<P><B>

SYNOPSIS

</B></P>



<!-- CODE SNIP //-->

<PRE>

#include &lt;sys/types.h&gt;

#include &lt;sys/socket.h&gt;

int accept(int s, struct sockaddr *addr,int*addrlen);

</PRE>

<!-- END CODE SNIP //-->



<P><B>

DESCRIPTION

</B></P>



<P>The argument s is a socket that has been created with

socket(2), bound to an address with bind(2), and is listening

for connections after a listen(2). The accept function extracts the first connection request on the queue of pending

connections, creates a new socket with the same properties of

s, and allocates a new file descriptor for the socket. If no

pending connections are present on the queue and the socket is not marked as nonblocking,

accept blocks the caller until a connection is present. If the socket is marked nonblocking and no pending connections are present on the queue,

accept returns an error as described below. The accepted socket may not be used to accept more connections. The original socket

s remains open.

</P>



<P>The argument addr is a result parameter that is filled in with the address of the connecting entity, as known to the

communications layer. The exact format of the addr parameter is determined by the domain in which the communication is

occurring. The addrlen is a value-result parameter; it should initially contain the amount of space pointed to by

addr; on return it will contain the actual length (in bytes) of the address returned. This call is used with connection-based socket types,

currently with SOCK_STREAM.

</P>



<P>It is possible to select(2) a socket for the purposes of doing an

accept by selecting it for read.

</P>



<P>For certain protocols that require an explicit confirmation, such as

ISO and DATAKIT, accept can be thought of as

merely dequeuing the next connection request and not implying confirmation. Confirmation can be implied by a normal read

or write on the new file descriptor, and rejection can be implied by closing the new socket.

</P>



<P>One can obtain user connection request data without confirming the connection by issuing a

recvmsg(2) call with a msg iovlen of 0 and a nonzero

msg controllen, or by issuing a getsockopt(2) request. Similarly, one can provide user

connection rejection information by issuing a

sendmsg(2) call providing only the control information, or by calling

setsockopt(2).

</P>



<P><B>

RETURNS VALUES

</B></P>



<P>The call returns _1 on error. If it succeeds, it returns a nonnegative integer that is a descriptor for the accepted socket.

</P>



<P><B>

ERRORS

</B></P>



<TABLE>



<TR><TD>

EBADF

</TD><TD>

The descriptor is invalid.

</TD></TR><TR><TD>

ENOTSOCK

</TD><TD>

The descriptor references a file, not a socket.

</TD></TR><TR><TD>

EOPNOTSUPP

</TD><TD>

The referenced socket is not of type

SOCK_STREAM.

</TD></TR><TR><TD>

EFAULT

</TD><TD>

The addr parameter is not in a writable part of the user address space.

</TD></TR><TR><TD>

EWOULDBLOCK

</TD><TD>

The socket is marked nonblocking and no connections are present to be accepted.

</TD></TR></TABLE>



<P><B>

HISTORY

</B></P>



<P>The accept function appeared in BSD 4.2.

</P>







<P><CENTER>

<a href="../ch01/0661-0736.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0741-0743.html">Next</A></CENTER></P>







</td>
</tr>
</table>

<!-- begin footer information -->







</body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -