词条 | 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\"); exit(1); } length = filelength(handle); status = lock(handle,0L,length/2); if (status == 0) printf("lock succeeded\"); else printf("lock failed\"); status = unlock(handle,0L,length/2); if (status == 0) printf("unlock succeeded\"); else printf("unlock failed\"); 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= t; } for (int i = 0; i < 10; i++) { threads<i id="bks_bltfoecb">.Start(); } } } lock 命令详解用途保留(reserve)终端。 语法lock [ -Timeout ] 描述lock 命令请求用户密码、读取并第二次请求密码以进行验证。在此期间,该命令会锁定终端,直到第二次接收到密码或以下的一条发生,才会释放它: * 超出了超时间隔。 * 有着相应许可的用户杀死了该命令。 超时的缺省值为 15 分钟,但是可以使用 -Timeout 标志进行修改。 标志-Timeout Timeout 参数指定,以分钟的形式表示了超时间隔。缺省值为 15 分钟。 示例 1. 为了将终端保留在密码控制下,请输入: lock 系统会提示两次要求密码,以便其能够验证。如果密码没有在 15 分钟内重复,命令将超时。 2. 为了将终端保留在密码控制下,并且超时间隔为 10 分钟,请输入: lock -10 文件 /usr/bin/lock 包含了 lock 命令。<i id="bks_bltfoecb"> VB----------------------------------------- 限制其他程序对文件的存取格式 |
随便看 |
百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。