Setting the save text bit (indicated by t) on an executable file is supposed to tell the kernel to leave a program in memory after it terminates.
This use is now largely obsolete. The save text permission on a directory means something slightly different. When this permission bit is set on a directory, a user may delete a file only if he or she has write permission (w) for that file,
even if he or she has write permission on the directory. This is a strengthening of the normal UNIX rules discussed in a previous lesson. To set this bit, use the command
chmod u+t directory
To unset this bit, use the command
chmod u–t directory
When used on a file, these bits are extremely important, but are also relevant to running processes. Therefore, we will discuss them in further detail a bit later in this course when we discuss processes. The SGID bit has a special meaning when set on a directory. It forces new files created in that directory to be owned by the same group that owns the directory (instead of the group of the file's creator). This feature can be convenient if you want to force all files created in a certain directory (regardless of who creates them) to have the same group ownership. To set this bit on a directory, you would use
chmod g+s directory
File locking permissions in Unix-like systems (including Linux) provide a mechanism to control and synchronize access to files, especially when multiple processes or users are involved. Here's a breakdown of key concepts:
Purpose of File Locking:
- Prevent Data Corruption: Locking prevents multiple processes or users from modifying the same file simultaneously, leading to potential data loss or inconsistencies.
- Coordination: File locking allows processes to coordinate access to shared resources. For example, one process can lock a file, make changes, and then release the lock, signaling to other processes that the file is now safe to modify.
Types of File Locks:
- Advisory Locks:
- Cooperative in nature – processes are expected to check for locks before accessing a file.
- A process can technically violate an advisory lock, but well-behaved programs generally respect them.
- Mandatory Locks:
- Enforced by the operating system kernel.
- Even if a process tries to access a file with a mandatory lock, the kernel will deny access, ensuring strict protection.
Lock Granularity:
- Whole-file Locks: The entire file is locked. Simple, but less flexible if multiple processes need non-overlapping access to different parts of the file.
- Byte-range Locks: Allows locking of specific sections within a file, enabling finer-grained control for concurrent access.
Tools For File Locking:
- fcntl() : A system call used within programs to acquire and release locks.
- lockf() : A command-line utility simplified interface for file locking actions.
- Specific Commands: Some commands (like editors) might have built-in locking features to prevent conflicts while editing a file.
Example Scenario:
Imagine a log file being written to by multiple processes. With file locking, a process can acquire an exclusive lock on the log file before appending data. This prevents other processes from writing at the same time, ensuring the log's integrity.
Important Notes:
- File locking is most effective when all processes accessing a shared file cooperate.
- Network file systems (NFS) add complexity, as locking mechanisms might need to function across systems.