複製鏈接
請複製以下鏈接發送給好友

Lock

(函數)

鎖定
lock 確保當一個線程位於代碼的臨界區時,另一個線程不進入臨界區。如果其他線程試圖進入鎖定的代碼,則它將一直等待(即被阻止),直到該對象被釋放。
線程處理(C# 編程指南) 這節討論了線程處理。
lock 調用塊開始位置的 Enter 和塊結束位置的 Exit
通常,應避免鎖定 public 類型,否則實例將超出代碼的控制範圍。
中文名
Lock
含    義
計算機語言中的函數之一
功 能
設置文件共享鎖
用 法
int lock

目錄

  1. 1 簡介
  2. 2 詳解
  3. 用途
  4. 語法
  5. 描述
  6. 標誌

Lock簡介

計算機語言中的函數之一
函數名: lock
功 能: 設置文件共享
用 法: int lock(int handle, long offset, long length);
lock 在機械式(POPPING)中 表示的是鎖架程序例 #include
int main(void)
{
int handle, status;
long length;
/* Must have DOS Share.exe loaded for */
/* file locking to function properly */
handle = sopen("c:\\autoexec.bat",
O_RDONLY,SH_DENYNO,S_IREAD);
if (handle < 0)
{
printf("sopen failed\n");
exit(1);
}
length = filelength(handle);
status = lock(handle,0L,length/2);
if (status == 0)
printf("lock succeeded\n");
else
printf("lock failed\n");
status = unlock(handle,0L,length/2);
if (status == 0)
printf("unlock succeeded\n");
else
printf("unlock failed\n");
close(handle);
return 0;
}
C#程序例
lock 確保當一個線程位於代碼的臨界區時,另一個線程不進入臨界區。如果其他線程試圖進入鎖定的代碼,則它將一直等待(即被阻止),直到該對象被釋放。
線程處理(C# 編程指南) 這節討論了線程處理。
lock 調用塊開始位置的 Enter 和塊結束位置的 Exit。
通常,應避免鎖定 public 類型,否則實例將超出代碼的控制範圍。常見的結構 lock (this)、lock (typeof (MyType)) 和 lock ("myLock") 違反此準則:
如果實例可以被公共訪問,將出現 lock (this) 問題。
如果 MyType 可以被公共訪問,將出現 lock (typeof (MyType)) 問題。
由於進程中使用同一字符串的任何其他代碼將共享同一個鎖,所以出現 lock(“myLock”) 問題。
最佳做法是定義 private 對象來鎖定, 或 private shared 對象變量來保護所有實例所共有的數據。
// statements_lock.cs
using System;
using System.Threading;
class ThreadTest
{
public void RunMe()
{
Console.WriteLine("RunMe called");
}
static void Main()
{
ThreadTest b = new ThreadTest();
Thread t = new Thread(b.RunMe);
t.Start();
}
}
輸出
RunMe called
下例使用線程和 lock。只要 lock 語句存在,語句塊就是臨界區並且 balance 永遠不會是負數。
// statements_lock2.cs
using System;
using System.Threading;
class Account
{
private Object thisLock = new Object();
int balance;
Random r = new Random();
public Account(int initial)
{
balance = initial;
}
int Withdraw(int amount)
{
// This condition will never be true unless the lock statement
// is commented out:
if (balance < 0)
{
throw new Exception("Negative Balance");
}
// Comment out the next line to see the effect of leaving out
// the lock keyword:
lock(thisLock)
{
if (balance >= amount)
{
Console.WriteLine("Balance before Withdrawal : " + balance);
Console.WriteLine("Amount to Withdraw : -" + amount);
balance = balance - amount;
Console.WriteLine("Balance after Withdrawal : " + balance);
return amount;
}
else
{
return 0; // transaction rejected
}
}
}
public void DoTransactions()
{
for (int i = 0; i < 100; i++)
{
Withdraw(r.Next(1, 100));
}
}
}
class Test
{
static void Main()
{
Thread[] threads = new Thread[10];
Account acc = new Account(1000);
for (int i = 0; i < 10; i++)
{
Thread t = new Thread(new ThreadStart(acc.DoTransactions));
threads[i]= t;
}
for (int i = 0; i < 10; i++)
{
threads[i].Start();
}
}
}

Lock詳解

Lock用途

保留(reserve)終端。

Lock語法

lock [ -Timeout ]

Lock描述

lock 命令請求用户密碼、讀取並第二次請求密碼以進行驗證。在此期間,該命令會鎖定終端,直到第二次接收到密碼或以下的一條發生,才會釋放它:
* 超出了超時間隔。
* 有着相應許可的用户殺死了該命令。
超時的缺省值為 15 分鐘,但是可以使用 -Timeout 標誌進行修改。

Lock標誌

-Timeout Timeout 參數指定,以分鐘的形式表示了超時間隔。缺省值為 15 分鐘。
示例
1. 為了將終端保留在密碼控制下,請輸入:
lock
系統會提示兩次要求密碼,以便其能夠驗證。如果密碼沒有在 15 分鐘內重複,命令將超時。
2. 為了將終端保留在密碼控制下,並且超時間隔為 10 分鐘,請輸入:
lock -10
文件
/usr/bin/lock 包含了 lock 命令。<i id="bks_bltfoecb">
VB-----------------------------------------
限制其他程序對文件的存取格式