Movatterモバイル変換


[0]ホーム

URL:


Skip Navigation Links
Exit Print View
Link to oracle.comman pages section 2: System Calls    Oracle Solaris 11 Information Library
Oracle Technology Network
Library
PDF
Print View
Feedback
search filter icon
search icon
 Search Scope:

Document Information

Preface

Introduction

System Calls

access(2)

acct(2)

acl(2)

adjtime(2)

alarm(2)

brk(2)

chdir(2)

chmod(2)

chown(2)

chroot(2)

close(2)

creat(2)

dup(2)

exec(2)

execl(2)

execle(2)

execlp(2)

execv(2)

execve(2)

execvex(2)

execvp(2)

_Exit(2)

_exit(2)

exit(2)

faccessat(2)

facl(2)

fchdir(2)

fchmod(2)

fchmodat(2)

fchown(2)

fchownat(2)

fchroot(2)

fcntl(2)

fexecve(2)

fgetlabel(2)

fork1(2)

fork(2)

forkall(2)

forkallx(2)

forkx(2)

fpathconf(2)

fstat(2)

fstatat(2)

fstatvfs(2)

futimens(2)

futimesat(2)

getacct(2)

getcontext(2)

getdents(2)

getegid(2)

geteuid(2)

getgid(2)

getgroups(2)

getisax(2)

getitimer(2)

getlabel(2)

getmsg(2)

getpflags(2)

getpgid(2)

getpgrp(2)

getpid(2)

getpmsg(2)

getppid(2)

getppriv(2)

getprojid(2)

getrctl(2)

getrlimit(2)

getsid(2)

gettaskid(2)

getuid(2)

getustack(2)

ioctl(2)

issetugid(2)

kill(2)

lchown(2)

link(2)

linkat(2)

llseek(2)

lseek(2)

lstat(2)

_lwp_cond_broadcast(2)

_lwp_cond_reltimedwait(2)

_lwp_cond_signal(2)

_lwp_cond_timedwait(2)

_lwp_cond_wait(2)

_lwp_continue(2)

_lwp_info(2)

_lwp_kill(2)

_lwp_mutex_lock(2)

_lwp_mutex_trylock(2)

_lwp_mutex_unlock(2)

_lwp_self(2)

_lwp_sema_init(2)

_lwp_sema_post(2)

_lwp_sema_trywait(2)

_lwp_sema_wait(2)

_lwp_suspend(2)

memcntl(2)

meminfo(2)

mincore(2)

mkdir(2)

mkdirat(2)

mknod(2)

mknodat(2)

mmap(2)

mmapobj(2)

mount(2)

mprotect(2)

msgctl(2)

msgget(2)

msgids(2)

msgrcv(2)

msgsnap(2)

msgsnd(2)

munmap(2)

nice(2)

ntp_adjtime(2)

ntp_gettime(2)

open(2)

openat(2)

pathconf(2)

pause(2)

pcsample(2)

pipe(2)

poll(2)

p_online(2)

ppoll(2)

pread(2)

priocntl(2)

priocntlset(2)

processor_bind(2)

processor_info(2)

profil(2)

pset_assign(2)

pset_bind(2)

pset_create(2)

pset_destroy(2)

pset_getattr(2)

pset_info(2)

pset_list(2)

pset_setattr(2)

putacct(2)

putmsg(2)

putpmsg(2)

pwrite(2)

read(2)

readlink(2)

readlinkat(2)

readv(2)

rename(2)

renameat(2)

resolvepath(2)

rmdir(2)

sbrk(2)

semctl(2)

semget(2)

semids(2)

semop(2)

semtimedop(2)

setcontext(2)

setegid(2)

seteuid(2)

setgid(2)

setgroups(2)

setitimer(2)

setpflags(2)

setpgid(2)

setpgrp(2)

setppriv(2)

setrctl(2)

setregid(2)

setreuid(2)

setrlimit(2)

setsid(2)

settaskid(2)

setuid(2)

setustack(2)

shmadv(2)

shmat(2)

shmctl(2)

shmdt(2)

shmget(2)

shmids(2)

shmop(2)

sigaction(2)

sigaltstack(2)

sigpending(2)

sigprocmask(2)

sigsend(2)

sigsendset(2)

sigsuspend(2)

sigwait(2)

__sparc_utrap_install(2)

stat(2)

statvfs(2)

stime(2)

swapctl(2)

symlink(2)

symlinkat(2)

sync(2)

sysfs(2)

sysinfo(2)

time(2)

times(2)

uadmin(2)

ulimit(2)

umask(2)

umount(2)

umount2(2)

uname(2)

unlink(2)

unlinkat(2)

ustat(2)

utime(2)

utimensat(2)

utimes(2)

uucopy(2)

vfork(2)

vforkx(2)

vhangup(2)

waitid(2)

wracct(2)

write(2)

writev(2)

yield(2)

write

, pwrite

, writev

- write on a file

Synopsis

#include <unistd.h>ssize_twrite(intfildes,const void *buf,size_tnbyte);
ssize_tpwrite(intfildes,const void *buf,size_tnbyte,off_toffset);
#include <sys/uio.h>ssize_twritev(intfildes,const struct iovec *iov,intiovcnt);

Description

Thewrite() function attempts to writenbyte bytes from the buffer pointedto bybuf to the file associated with the open file descriptor,fildes.

Ifnbyte is 0,write() will return 0 and have no otherresults if the file is a regular file; otherwise, the results areunspecified.

On a regular file or other file capable of seeking, the actualwriting of data proceeds from the position in the file indicated bythe file offset associated withfildes. Before successful return fromwrite(), thefile offset is incremented by the number of bytes actually written. On aregular file, if this incremented file offset is greater than the lengthof the file, the length of the file will be set tothis file offset.

If theO_SYNC bit has been set, write I/O operations on thefile descriptor complete as defined by synchronized I/O file integrity completion.

Iffildes refers to a socket,write() is equivalent tosend(3SOCKET) withno flags set.

On a file not capable of seeking, writing always takes place startingat the current position. The value of a file offset associatedwith such a device is undefined.

If theO_APPEND flag of the file status flags is set, thefile offset will be set to the end of the file priorto each write and no intervening file modification operation will occur betweenchanging the file offset and the write operation.

For regular files, no data transfer will occur past the offset maximumestablished in the open file description withfildes.

Awrite() to a regular file is blocked if mandatory file/record lockingis set (seechmod(2)), and there is a record lock owned byanother process on the segment of the file to be written:

If awrite() requests that more bytes be written than thereis room for—for example, if the write would exceed the process filesize limit (seegetrlimit(2) andulimit(2)), the system file size limit, orthe free space on the device—only as many bytes as there isroom for will be written. For example, suppose there is space for 20bytes more in a file before reaching a limit. Awrite() of512-bytes returns 20. The nextwrite() of a non-zero number of bytesgives a failure return (except as noted for pipes and FIFO below).

Ifwrite() is interrupted by a signal before it writes any data,it will return -1 witherrno set toEINTR.

Ifwrite() is interrupted by a signal after it successfully writes somedata, it will return the number of bytes written.

Ifwrite() exceeds the process file size limit, the application generates aSIGXFSZ signal, whose default behavior is to dump core.

After awrite() to a regular file has successfully returned:

Write requests to a pipe or FIFO are handled the same asa regular file with the following exceptions:

When attempting to write to a file descriptor (other than a pipe,a FIFO, a socket, or a stream) that supports nonblocking writes andcannot accept the data immediately:

Upon successful completion, wherenbyte is greater than 0,write() will markfor update thest_ctime andst_mtime fields of the file, and ifthe file is a regular file, theS_ISUID andS_ISGID bits ofthe file mode may be cleared.

For streams files (seeIntro(2) andstreamio(7I)), the operation ofwrite() isdetermined by the values of the minimum and maximumnbyte range (“packetsize”) accepted by the stream. These values are contained in the topmoststream module, and can not be set or tested from user level. Ifnbyte falls within the packet size range,nbyte bytes are written. Ifnbyte does not fall within the range and the minimum packet sizevalue is zero,write() breaks the buffer into maximum packet size segmentsprior to sending the data downstream (the last segment may besmaller than the maximum packet size). Ifnbyte does not fallwithin the range and the minimum value is non-zero,write() fails andsetserrno toERANGE. Writing a zero-length buffer (nbyte is zero) toa streams device sends a zero length message with zero returned.However, writing a zero-length buffer to a pipe or FIFO sends nomessage and zero is returned. The user program may issue theI_SWROPTioctl(2) to enable zero-length messages to be sent across thepipe or FIFO (seestreamio(7I)).

When writing to a stream, data messages are created with a priorityband of zero. When writing to a socket or to a streamthat is not a pipe or a FIFO:

Thewrite() andwritev() functions will fail if the stream head hadprocessed an asynchronous error before the call. In this case, thevalue oferrno does not reflect the result ofwrite() orwritev()but reflects the prior error.

pwrite()

Thepwrite() function is equivalent towrite(), except that it writes intoa given position and does not change the file offset (regardless ofwhetherO_APPEND is set). The first three arguments topwrite() are the sameaswrite(), with the addition of a fourth argumentoffset for thedesired position inside the file.

writev()

Thewritev() function performs the same action aswrite(), but gathers theoutput data from theiovcnt buffers specified by the members of theiov array:iov[0],iov[1], …,iov[iovcnt - 1]. Theiovcnt buffer is valid ifgreater than 0 and less than or equal to{IOV_MAX}. SeeIntro(2)for a definition of{IOV_MAX}.

Theiovec structure contains the following members:

void    *iov_base;size_t  iov_len;

Eachiovec entry specifies the base address and length of an areain memory from which data should be written. Thewritev() functionalways writes all data from an area before proceeding to the next.

Iffildes refers to a regular file and all of theiov_lenmembers in the array pointed to byiov are 0,writev() willreturn 0 and have no other effect. For other file types,the behavior is unspecified.

If the sum of theiov_len values is greater thanSSIZE_MAX, theoperation fails and no data is transferred.

Return Values

Upon successful completion,write() returns the number of bytes actually written tothe file associated withfildes. This number is never greater thannbyte.Otherwise,-1 is returned, the file-pointer remains unchanged, anderrno is set toindicate the error.

Upon successful completion,writev() returns the number of bytes actually written. Otherwise, it returns-1, the file-pointer remains unchanged, anderrno isset to indicate an error.

Errors

Thewrite(),pwrite(), andwritev() functions will fail if:

EAGAIN

Mandatory file/record locking is set,O_NDELAY orO_NONBLOCK is set, and there is a blocking record lock; an attempt is made to write to a stream that can not accept data with theO_NDELAY orO_NONBLOCK flag set; or a write to a pipe or FIFO ofPIPE_BUF bytes or less is requested and less thannbytes of free space is available.

EBADF

Thefildes argument is not a valid file descriptor open for writing.

EDEADLK

The write was going to go to sleep and cause a deadlock situation to occur.

EDQUOT

The user's quota of disk blocks on the file system containing the file has been exhausted.

EFBIG

An attempt is made to write a file that exceeds the process's file size limit or the maximum file size (seegetrlimit(2) andulimit(2)).

EFBIG

The file is a regular file,nbyte is greater than 0, and the starting position is greater than or equal to the offset maximum established in the file description associated withfildes.

EINTR

A signal was caught during the write operation and no data was transferred.

EIO

The process is in the background and is attempting to write to its controlling terminal whoseTOSTOP flag is set, or the process is neither ignoring nor blockingSIGTTOU signals and the process group of the process is orphaned.

ENOLCK

Enforced record locking was enabled and{LOCK_MAX} regions are already locked in the system, or the system record lock table was full and the write could not go to sleep until the blocking record lock was removed.

ENOLINK

Thefildes argument is on a remote machine and the link to that machine is no longer active.

ENOSPC

During a write to an ordinary file, there is no free space left on the device.

ENOSR

An attempt is made to write to a streams with insufficient streams memory resources available in the system.

ENXIO

A hangup occurred on the stream being written to.

EPIPE

An attempt is made to write to a pipe or a FIFO that is not open for reading by any process, or that has only one end open (or to a file descriptor created bysocket(3SOCKET), using typeSOCK_STREAM that is no longer connected to a peer endpoint). ASIGPIPE signal will also be sent to the thread. The process dies unless special provisions were taken to catch or ignore the signal.

ERANGE

The transfer request size was outside the range supported by the streams file associated withfildes.

Thewrite() andpwrite() functions will fail if:

EFAULT

Thebuf argument points to an illegal address.

EINVAL

Thenbyte argument overflowed anssize_t.

Thepwrite() function fails and the file pointer remains unchanged if:

ESPIPE

Thefildes argument is associated with a pipe or FIFO.

Thewrite() andwritev() functions may fail if:

EINVAL

The stream or multiplexer referenced byfildes is linked (directly or indirectly) downstream from a multiplexer.

ENXIO

A request was made of a non-existent device, or the request was outside the capabilities of the device.

ENXIO

A hangup occurred on the stream being written to.

A write to a streams file may fail if an error messagehas been received at the stream head. In this case,errnois set to the value included in the error message.

Thewritev() function may fail if:

EINVAL

Theiovcnt argument was less than or equal to 0 or greater than {IOV_MAX}; one of theiov_len values in theiov array was negative; or the sum of theiov_len values in theiov array overflowed anssize_t.

Usage

Thepwrite() function has a transitional interface for 64-bit file offsets. Seelf64(5).

Attributes

Seeattributes(5) for descriptions of the following attributes:

ATTRIBUTE TYPE
ATTRIBUTE VALUE
Interface Stability
Committed
MT-Level
write() isAsync-Signal-Safe
Standard

See Also

Intro(2),chmod(2),creat(2),dup(2),fcntl(2),getrlimit(2),ioctl(2),lseek(2),open(2),pipe(2),ulimit(2),send(3SOCKET),socket(3SOCKET),attributes(5),lf64(5),standards(5),streamio(7I)

Copyright © 2011, Oracle and/or its affiliates. All rights reserved.Legal Notices
PreviousNext

[8]ページ先頭

©2009-2025 Movatter.jp