#include <unistd.h> int fsync(int file_descriptor);Service Program Name: QP0LLIB1
The fsync() function transfers all data for the file indicated by the open file descriptor file_descriptor to the storage device associated with file_descriptor. fsync() does not return until the transfer is complete, or until an error is detected.
No authorization is required. Authorization is verified during open() or creat().
If fsync() is not successful, errno usually indicates one of the following errors. Under some conditions, errno could indicate an error other than those listed here.
Error condition | Additional information |
---|---|
[EACCES] |
If you are accessing a remote file through the Network File System, update operations to file permissions at the server are not reflected at the client until updates to data that is stored locally by the Network File System take place. (Several options on the Add Mounted File System (ADDMFS) command determine the time between refresh operations of local data.) Access to a remote file may also fail due to different mappings of user IDs (UID) or group IDs (GID) on the local and remote systems. |
[EAGAIN] | |
[EBADF] | |
[EBADFID] | |
[EINVAL] |
For example, the file type is not valid for this operation. |
[EIO] | |
[EJRNDAMAGE] | |
[EJRNENTTOOLONG] | |
[EJRNINACTIVE] | |
[EJRNRCVSPC] | |
[ENEWJRN] | |
[ENEWJRNRCV] | |
[ENOTAVAIL] | |
[ENOTSAFE] | |
[ESTALE] |
If you are accessing a remote file through the Network File System, the file may have been deleted at the server. |
[EUNKNOWN] |
If interaction with a file server is required to access the object, errno could indicate one of the following errors:
Error condition | Additional information |
---|---|
[EADDRNOTAVAIL] | |
[ECONNABORTED] | |
[ECONNREFUSED] | |
[ECONNRESET] | |
[EHOSTDOWN] | |
[EHOSTUNREACH] | |
[ENETDOWN] | |
[ENETRESET] | |
[ENETUNREACH] | |
[ESTALE] |
If you are accessing a remote file through the Network File System, the file may have been deleted at the server. |
[ETIMEDOUT] | |
[EUNATCH] |
The following messages may be sent from this function:
Message ID | Error Message Text |
---|---|
CPE3418 E | Possible APAR condition or hardware failure. |
CPFA0D4 E | File system error occurred. Error number &1. |
CPF3CF2 E | Error(s) occurred during running of &1 API. |
CPF9872 E | Program or service program &1 in library &2 ended. Reason code &3. |
See Code disclaimer information for information pertaining to code examples.
The following example uses fsync():
#include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #define mega_string_len 250000 main() { char *mega_string; int file_descriptor; int ret; char fn[]="fsync.file"; if ((mega_string = (char*) malloc(mega_string_len)) == NULL) perror("malloc() error"); else if ((file_descriptor = creat(fn, S_IWUSR)) < 0) perror("creat() error"); else { memset(mega_string, 's', mega_string_len); if ((ret = write(file_descriptor, mega_string, mega_string_len)) == -1) perror("write() error"); else { printf("write() wrote %d bytes\n", ret); if (fsync(file_descriptor) != 0) perror("fsync() error"); else if ((ret = write(file_descriptor, mega_string, mega_string_len)) == -1) perror("write() error"); else printf("write() wrote %d bytes\n", ret); } close(file_descriptor); unlink(fn); } free(mega_string); }
Output:
write() wrote 250000 bytes write() wrote 250000 bytes
Top | UNIX-Type APIs | APIs by category |