Section 4.11. System Calls


4.11. System Calls

System calls are interfaces used by user programs to have the kernel perform a specific function on behalf of the calling thread or process. Some Solaris-proprietary system calls may not be available on Linux. When this happens, a replacement version may have to be implemented on the Linux side.

A list of system calls in Linux can be located at /usr/include/asm/unistd.h.

Table A-2 in appendix A provides a system call comparison between Solaris and Linux.

In the following subsections, we highlight important system calls that have minor differences, primarily with their flags.

4.11.1. open()

int open(const char *pathname, int flags, mode_t mode); 


The possible values for the argument flags are constructed by a bitwise-inclusive-OR between one of the following and any combination of the flags provided in:

O_RDONLY or O_WRONLY or O_RDWR 


Some of these flags are available only on Solaris and some only on Linux (see Table 4-18).

Table 4-18. Argument Flags for open()

Solaris

Linux

Description[32]

O_APPEND

O_APPEND

If set, the file offset is set to the end of the file prior to each write.

O_CREATE

O_CREATE

Create the file if it does not exist.

O_DSYNC

O_DSYNC

Write I/O operations on the file descriptor complete as defined by synchronized I/O data-integrity completion.

O_EXCL

O_EXCL

If O_CREATE and O_EXCL are set, open() fails if the file exists. If O_EXCL and O_CREATE are set, and path names a symbolic link, open() fails and sets errno to EEXIST, regardless of the contents of the symbolic link. If O_EXCL is set and O_CREATE is not set, the result is undefined.

O_LARGEFILE

O_LARGEFILE

If set, the offset maximum in the open file description is the largest value that can be represented correctly in an object of type off64_t.

O_NOCTTY

O_NOCTTY

If set and path identifies a terminal device, open() does not cause the terminal device to become the controlling terminal for the process.

O_NOFOLLOW

O_NOFOLLOW

If the path names a symbolic link, open() fails and sets errno to ELOOP.

O_NOLINKS

N/A

If the link count of the named file is greater than 1, open() fails and sets errno to EMLINK.

O_NONBLOCK or O_NDELAY

O_NONBLOCK or
O_NDELAY

The file is open in nonblocking mode.

O_RSYNC

O_RSYNC

Read I/O operations on the file descriptor complete at the same level of integrity as specified by the O_DSYNC and O_SYNC flags.

O_SYNC

O_SYNC

Write I/O operations on the file descriptor complete as defined by synchronized I/O file-integrity completion.

O_TRUNC

O_TRUNC

If the file already exists, it will be truncated.

O_XATTR

N/A

If set in open(), the implied file descriptor is that for the current working directory. Extended attributes must be referenced with a relative path; providing an absolute path results in a normal file reference.

N/A

O_DIRECTORY

If pathname is not a directory, cause the open to fail.

N/A

O_DIRECT

File I/O is done directly to/from user space buffers.

N/A

O_ASYNC

Generate a signal (SIGIO, by default) when input or output becomes possible on this file descriptor.


[32] Solaris and Linux man pages.

4.11.2. fcntl()

int fcntl( int fildes, int cmd, /* arg */ ...) ; 


This system call is used to perform various control functions as specified by the cmd argument on an open file associated with the fildes file descriptor. The call may require the third argument depending on the value of cmd. The available values of cmd are provided in Table 4-19.

Table 4-19. The cmd Flags for fcntl()

Solaris

Linux

Description

F_DUPFD

F_DUPFD

Find the lowest-numbered file descriptor available that is greater than or equal to arg and make it a copy of fildes.

F_DUP2FD

N/A

Similar to F_DUPFD, but always returns arg. F_DUP2FD closes arg if it is open and not equal to fildes. F_DUP2FD is equivalent to dup2( fildes, arg), which is available on Linux.

F_FREESP

N/A

Free storage space associated with a section of the ordinary file fildes. The section is specified by a variable of data type struct flock pointed to by arg.

F_FREESP64

N/A

Same as F_FREESP, but takes a struct flock64 argument rather than a struct flock argument.

F_GETFD

F_GETFD

Get the file descriptor flags that are associated with the file descriptor fildes.

F_GETFL

F_GETFL

Get the file status flags and file access modes for the file descriptor specified by fildes.

F_GETOWN

F_GETOWN

In Solaris, if the file descriptor refers to a socket, get the process or process group ID specified to receive SIGURG signals when out of band data is available.

  

In Linux, it gets the process ID or process group ID specified to receive the SIGIO or SIGURG signal for events on the file descriptor.

F_GETXFL

N/A

Get the file status flags, file access modes, and file creation and assignment flags for the file descriptor specified by fildes.

F_SETFD

F_SETFD

Set the file descriptor flags that are associated with fildes to the third argument, arg.

F_SETFL

F_SETFL

Set the file status flags part of the descriptor's flag to the value specified by arg.

F_SETOWN

F_SETOWN

In Solaris, if fildes refers to a socket, set the process or process group ID specified to receive SIGURG signals when out-of-band data is available, using the value of the third argument, arg.

  

In Linux, it sets the process ID or process group that will receive SIGIO or SIGURG signals for events on file descriptor fd.

F_GETLK

F_GETLK

Return the flock structure that prevents you from getting the lock, or set the l_type field of the lock to F_UNLCK if there is no conflict.

F_GETLK64

F_GETLK64

Equivalent to F_GETLK, but takes a struct flock64 argument instead.

F_SETLK

F_SETLK

Set or clean a file segment lock according to the lock description pointed to by arg.

F_SETLK64

F_SETLK64

Equivalent to F_SETLK, but takes a struct flock64 argument instead.

F_SETLKW

F_SETLKW

Similar to F_SETLK, except that if a shared or exclusive lock is blocked by other locks, the process waits until the request can be satisfied.

F_SETLKW64

F_SETLKW64

Equivalent to F_SETLKW64, but takes a struct flock64 argument instead.

F_SHARE

N/A

Set a share reservation on a file with the specified access mode and designate the types of access to deny.

F_UNSHARE

N/A

Remove an existing share reservation.

F_RDACC

N/A

Set a file share reservation for read-only access.

F_WRACC

N/A

Set a file share reservation for write-only access.

F_RWACC

N/A

Set a file share reservation for read and write access.

F_COMPAT

N/A

Set a file share reservation to compatibility mode.

F_RDDNY

N/A

Set a file share reservation to deny read access to other processes.

F_WRDNY

N/A

Set a file share reservation to deny write access to other processes.

F_RWDNY

N/A

Set a file share reservation to deny read and write access to other process.

F_NODNY

N/A

Do not deny read or write access to any other process.

N/A

F_GETSIG

Get the signal sent when input or output becomes possible.

N/A

F_SETSIG

Set the signal sent when input or output becomes possible.

N/A

F_SETLEASE

Set or remove a file lease according to the values specified in arg.

N/A

F_GETLEASE

Indicate what type of lease we hold on the file referred to by fd.

N/A

F_RDLCK

Take out a read lease. We will be notified when another process opens the file for writting or truncates it.

N/A

F_WRLCK

Take out a write lease. We will be notified when another process opens the file for reading or writing or truncates it.

N/A

F_UNLCK

Remove our lease from the file.

N/A

F_NOTIFY

Provide notification when the directory referred to by fd or any of the files that it contains is changed.


The data type struct flock is defined differently in Solaris and Linux. On Solaris, the flock structure (defined in /usr/include/sys/fcntl.h) contains at least the following elements:

short  l_type;      /* lock operation type */ short  l_whence;    /* lock base indicator */ off_t  l_start;     /* starting offset from base */ off_t  l_len;       /* lock length; l_len == 0 means  until end of file  */ int  l_sysid;       /* system ID running process holding lock */ pid_t  l_pid;       /* process ID of process holding lock */ 


But Linux requires flock to have the following fields (struct flock is defined in /usr/include/bits/fcntl.h):

short l_type;   /* Type of lock: F_RDLCK,            F_WRLCK, F_UNLCK */ short l_whence; /* How to interpret l_start:            SEEK_SET, SEEK_CUR, SEEK_END */ off_t l_start;  /* Starting offset for lock */ off_t l_len;    /* Number of bytes to lock */ pid_t l_pid;    /* PID of process blocking our lock            (F_GETLK only) */ 


4.11.3. dirent

The dirent structure in Solaris is different from that in Linux.

On Linux, dirent is defined in /usr/include/bits/dirent.h, as follows:

struct dirent       { #ifndef __USE_FILE_OFFSET64         __ino_t d_ino;         __off_t d_off; #else        __ino64_t d_ino;        _off64_t d_off; #endif       unsigned short int d_reclen;       unsigned char d_type;       char d_name[256];       }; 


On Solaris, dirent is defined in /usr/include/sys/dirent.h, as follows:

typedef struct dirent {    ino_t  d_ino;            /* "inode number" of entry */    off_t  d_off;            /* offset of disk directory entry */    unsigned short d_reclen; /* length of this record */    char  d_name[1];         /* name of file */ } dirent_t; 


4.11.4. CPU Affinity

A process in a multiprocessor system by default bounces among several CPUs. Explicitly binding a process to certain CPUs (assigning CPU affinity) may yield performance gain in some cases. System calls for CPU affinity on Solaris are different from those on Linux. The Linux 2.6 kernel provides sched_setaffinity() and sched_getaffinity() for CPU affinity. You can find more information about this topic at www.linuxjournal.com/article/6799. Also, taskset (http://rlove.org/schedutils) is a nice tool that enables you to manipulate task CPU affinity.

4.11.5. fork()

In Solaris 10, calls to fork() or fork1() are identical to a call to fork() in Linux. That is, only the calling thread is replicated in the child process. Note that this is the POSIX-specified behavior for fork(). In the previous release of Solaris, the behavior of fork() depends on whether the application is linked with the POSIX thread library. When linked with the Solaris threads (-lthread) but not linked with POSIX threads (-lpthread), fork() is the same as forkall(). (All the threads are replicated in the child process.) When linked with POSIX threads, fork() is the same as fork1(). Linux does not support forkall().




UNIX to Linux Porting. A Comprehensive Reference
UNIX to Linux Porting: A Comprehensive Reference
ISBN: 0131871099
EAN: 2147483647
Year: 2004
Pages: 175

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net