How to port Linux programs with flock to Solaris

Source: Internet
Author: User
Tags flock

How to port programs containing flock to Solaris

Author: Badcoffee
Email: blog.oliver@gmail.com
Blog:Http://blog.csdn.net/yayong
2005Year5Month

A friend's Linux source program contains the following calls:

Flock (FD, lock_un)

There is no problem in compiling with GCC on Linux. But when compiling on Solaris, an error occurs: Error: 'lock _ ex' undeclared (first use in this function)
Obviously, this is because the correct header file is not included, and the lock_ex macro definition cannot be found.

However, my friend checked the Solaris man and confirmed that the header file has been correctly included. The following is a description of the Solaris man Manual: My friend has grep/usr/include/sys/file. h does not find the definition of the lock_ex macro. Is there a problem with the Solaris header file?

From flock (3ucb), we can see that this call is a 3ucb word class in Section 3 of man's manual,
Let's take a look at what 3ucb is for: the problem is solved. Originally, 3ucb functions are reserved for compatibility with BSD functions, in/usr/ucbinclude/sys/file. h.

Through this check, we found that there are a lot of sub-classes in Section 3, so we must pay attention to them when checking man later.

Man flock

SunOS/BSD compatibility library functions flock (3ucb)

Name
Flock-apply or remove an advisory lock on an open file

Synopsis
/Usr/UCB/CC [flag...] file...
# Include sys/file. h

Int flock (FD, operation );
Int FD, operation;

 

The following describes common file lock functions:

Flock (); lockf (); fcntl ();

Flock () is derived from BSD, but it can be found in most Unix systems at present. Flock () is simple and effective on a single host, but it cannot work on NFS.

Perl also has a somewhat confusing flock () function, but it is implemented within Perl. Fcntl () is the only POSIX-compliant file lock implementation, so it is also the only portable. It is also the most powerful file lock-and the most difficult to use.

On the NFS file system, fcntl () requests are submitted to RPC. the lockd daemon, which then communicates with the host lockd. Unlike flock (), fcntl () can implement blocking on the record layer. Lockf () is just a simplified fcntl () file lock interface. No matter which file lock you use, remember to use sync to update all your file inputs/outputs before the lock takes effect.

Lock (FD );
Write_to (some_function_of (FD ));
Flush_output_to (FD);/* Be sure to fl the output before removing the lock */
Unlock (FD );
Do_something_else;/* Another process may update it */
Lock (FD );
Seek (FD, somewhere);/* because the original file pointer is no longer secure */
Do_something_with (FD );
...

Some useful fcntl () blocking methods (for simplicity, skip error handling ):

# Include
# Include
Read_lock (int fd)/* a shared file lock on the entire file */
{
Fcntl (FD, f_setlkw, file_lock (f_rdlck, seek_set ));
}

Write_lock (int fd)/* Exclusive file lock on the entire file */
{
Fcntl (FD, f_setlkw, file_lock (f_wrlck, seek_set ));
}

Append_lock (int fd)/* a lock at the end of a blocked file. Other processes can access the existing content */
{
Fcntl (FD, f_setlkw, file_lock (f_wrlck, seek_end ));
}

The previously used file_lock function is as follows:

Struct flock * file_lock (short type, short whence)
{
Static struct flock ret;
Ret. l_type = type;
Ret. l_start = 0;
Ret. l_whence = whence;
Ret. l_len = 0;
Ret. l_pid = getpid ();
Return & ret;
}

According to Solaris flock (3ucb), this function is not thread-safe. Therefore, we strongly recommend that you replace flock with fcntl. After all, it is the POSIX standard.

In addition, for many applications with large-capacity email systems, mailboxes are shared through NFS, and flock cannot be used. On Solaris, you can use the following code to implement a new flock:

# Ifndef lock_sh
# Define lock_sh 1/* shared lock */
# Define lock_ex 2/* exclusive lock */
# Define lock_nb 4/* don't block when locking */
# Define lock_un 8/* unlock */
# Endif
# Ifdef POSIX
# Include
/*
* This function emulates a subset of Flock ()
*/
Int emul_flock (FD, CMD)
Int FD, CMD;
{Struct flock F;
Memset (& F, 0, sizeof (f ));
If (CMD & lock_un)
F. l_type = f_unlck;
If (CMD & lock_sh)
F. l_type = f_rdlck;
If (CMD & lock_ex)
F. l_type = f_wrlck;
Return fcntl (FD, (CMD & lock_nb )? F_setlk: f_setlkw, & F );
}
# Define flock (F, c) emul_flock (F, c)
# Endif

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.