NCHU System & Network Lab Lab 15 Record Locking. NCHU System & Network Lab Record Locking (1/4) What happens when two process attempt to edit the same.

Slides:



Advertisements
Similar presentations
Lab 9 CIS 370 Umass Dartmouth.  A pipe is typically used as a one-way communications channel which couples one related process to another.  UNIX deals.
Advertisements

Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
Pseudo Terminals Concept Application APIs. Overview A pseudo terminal (PTY) is a user level program that appears to be a terminal device to another program.
Unix programming Term: III B.Tech II semester Unit-V II PPT Slides Text Books: (1)unix the ultimate guide by Sumitabha Das (2)Advanced programming.
I/O Multiplexing Road Map: 1. Motivation 2. Description of I/O multiplexing 3. Scenarios to use I/O multiplexing 4. I/O Models  Blocking I/O  Non-blocking.
1 PHP Storing and Retrieving Data. string fgets(resource handle [, int length]) Reads and returns one line from a file; moves file pointer to next line.
Daemon Processes Long lived utility processes Often started at system boot and ended when system shuts down Run in the background with no controlling terminal.
The ‘system-call’ ID-numbers How can Linux applications written in assembly language create and access files?
Concurrent Processes Lecture 5. Introduction Modern operating systems can handle more than one process at a time System scheduler manages processes and.
TDC368 UNIX and Network Programming Camelia Zlatea, PhD Week 6:  Inter-Process Synchronization  Signals.
NCHU System & Network Lab Lab 12 Page Replacement Algorithm.
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
CS Lecture 16 Outline Inter-process Communication (IPC) – Pipes – Signals Lecture 161CS Operating Systems 1.
The ‘wait4()’ function Looking at how the various ‘wait()’ system-calls rely on ‘wait4()’ in the x86_64 Linux kernel.
NCHU System & Network Lab Lab 10 Message Queue and Shared Memory.
NCHU System & Network Lab Lab 13 File I/O & Standard I/O.
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
The Programming Interface. Main Points Creating and managing processes – fork, exec, wait Performing I/O – open, read, write, close Communicating between.
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 11 Case Study 2: Windows Vista Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
Fundamentals CIS 552. Fundamentals Low-level I/O (read/write using system calls)  Opening/Creating files  Reading & Writing files  Moving around in.
SIMULATED UNIX FILE SYSTEM Implementation in C Tarek Youssef Bipanjit Sihra.
CSC Advanced Unix Programming, Fall, 2008 Welcome back to UNIX System Programming! Monday, September 15, class 4.
S -1 Shared Memory. S -2 Motivation Shared memory allows two or more processes to share a given region of memory -- this is the fastest form of IPC because.
Operating Systems Recitation 1, March th, 2002.
Minishell InKwan Yu Topics Unix System calls waitpid() pipe() dup2() C function calls strtok() strcmp() Minishell Software Enginnering.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
NCHU System & Network Lab Lab 11 Memory Mapped File.
1 Shared Memory. 2  Introduction  Creating a Shared Memory Segment  Shared Memory Control  Shared Memory Operations  Using a File as Shared Memory.
Lecture 8 Page 1 CS 111 Online Other Important Synchronization Primitives Semaphores Mutexes Monitors.
Week 12 - Wednesday.  What did we talk about last time?  File I/O  Binary trees  Lab 11.
Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section.
Operating Systems Process Creation
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 11 – File Processing Outline 11.1Introduction.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 14 Threads 2 Read Ch.
CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
NCHU System & Network Lab Lab 14 File and Directory.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
CSCI 330 UNIX and Network Programming
Interprocess Communication. Resource Sharing –Kernel: Data structures, Buffers –Processes: Shared Memory, Files Synchronization Methods –Kernel: Wait.
January 7, 2003Serguei Mokhov, 1 File I/O System Calls Reference COMP 229, 444, 5201 Revision 1.2 Date: July 21, 2004.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Tarek Abdelzaher Vikram Adve CS241 Systems Programming System Calls and I/O.
File I/O open close lseek read and write – unbuffered I/O dup and dup2.
The Process CIS 370, Fall 2009 CIS UMassD. The notion of a process In UNIX a process is an instance of a program in execution A job or a task Each process.
OS Labs 2/25/08 Frans Kaashoek MIT
NCHU System & Network Lab Lab #6 Thread Management Operating System Lab.
Dsh: A Devil Shell COMPSCI210 Recitation 14 Sep 2012 Vamsi Thummala.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Using System Calls (Unix) Have to tell compiler (if C/C++) where to find the headers, etc. – i.e., the “include” files May have to tell compiler where.
Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3.
1 Intro to the Shell with Fork, Exec, Wait Sarah Diesburg Operating Systems CS 3430.
Outline Other synchronization primitives
Unix Process Management
Other Important Synchronization Primitives
CH5 TCP Client - Server Example:
FILE LOCK #include <stdio.h> #include <stdlib.h>
Chapter 11 – File Processing
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
Lab 5 Process Control Operating System Lab.
File Structure Related system calls
2/25/08 Frans Kaashoek MIT OS abstractions 2/25/08 Frans Kaashoek MIT
Virtual Memory CSCI 380: Operating Systems Lecture #7 -- Review and Lab Suggestions William Killian.
File I/O (1) Prof. Ikjun Yeom TA – Mugyo
CSCI 380: Operating Systems William Killian
FILE I/O File Descriptors I/O Efficiency File Sharing
Lab 10 Paging.
Processes Creation and Threads
Process Description and Control in Unix
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
Presentation transcript:

NCHU System & Network Lab Lab 15 Record Locking

NCHU System & Network Lab Record Locking (1/4) What happens when two process attempt to edit the same file at the same time ? Record locking is the ability of a process to prevent other processes from modifying or reading a region of a file while the first process is working on it.

NCHU System & Network Lab Record Locking (2/4) Here are two types of locks : –Read lock (shared lock) Any number of processes can have a shared read lock on a given region of a file. All of them can read from the file but can’t write to it. –Write lock (exclusive lock) Only one process can have an exclusive lock on a given region of a file. The other process are prevented from getting locks or reading and writing.

NCHU System & Network Lab Record locking (3/4) Advisory V.S. Mandatory locking –Advisory lock (cooperative lock) All access functions in a library handle record locking in a consistent way. But advisory locking doesn’t prevent some other process that has write permission for file from writing. database Advisory lock access library A rude process with write permission of the file

NCHU System & Network Lab Record locking (4/4) –Mandatory locking cause the kernel to check every read(), write(), open() to prevent the calling process from violating the lock rule. file kernel fget() write() readn() fput() Locking check

NCHU System & Network Lab fcntl() (1/3) fcntl() function can change the properties of a file that is already open. –The fcntl() is used for five different purposes : cmddescription F_DUPFD Duplicate an existing descriptor F_GETFD,F_SETFDGet/set file descriptor flags F_GETFL,F_SETFLGet/set file status flags (O_RDWR … etc) F_GETOWN,F_SETOWNGet/set asynchronous I/O ownership F_GETLK,F_SETLK, F_SETLKW Get/set/lock file lock, it discussed later. #include int fcntl (int filedes, int cmd, struct flock *lockptr…);

NCHU System & Network Lab fcntl() (2/3) For record locking,the third argument of fcntl() is a pointer to a flock structure : struct flock { short l_type;/* F_RDLCK, F_WRLCK, F_UNLCK */ off_t l_start;/* offset in bytes, relative to l_whence */ short l_whence;/* SEEK_SET, SEEK_CUR, SEEK_END */ off_t l_len;/* length, in bytes ; 0 means lock to EOF */ pid_t l_pid;/* returned with F_GETLK */ }

NCHU System & Network Lab fcntl() (3/3) Three commands of fcntl( record lock ) : –F_GETLK To check the lock described by flockptr : –The information pointed by flockptr is overwritten by an existing lock. –If no lock exists, flockptr.l_type = F_UNLCK –F_SETLK Set the lock described by flockptr. If we try to obtain an illegal record lock, fcntl() will return errono = EAGAIN. –F_SETLKW A blocking version of F_SETLK. It will wait until the desired region released.

NCHU System & Network Lab Deadlock Deadlock –Deadlock occurs when two processes are each waiting for a resource that the other has locked. –When a deadlock is detected, the kernel must choose one to receive the error return. It depends on different implementations of systems. Process A Process B Lock request

NCHU System & Network Lab Inheritance and Release of Locks Inheritance and release of locks : –Release of locks : When a process terminates, all its locks are released. When a descriptor is closed, any locks on it are automatically released. –Inheritance Locks are never inherited by the child process across a fork(). Locks are inherited by a new program across an exec().

NCHU System & Network Lab Ex : FreeBSD file lock structure Figure : FreeBSD data structures for record locking

NCHU System & Network Lab Lab 1 Write a program that : –User can define a lock with type, start, and length. –This program will try to get a lock or it will be blocked by an existing lock. If it is blocked, it should show the lock information. –By running this program in two or more windows, you can see how programs interact while waiting locks.

NCHU System & Network Lab Lab 1 (cont.) Input : R/W, start, length Is it locked? Show the lock and wait.. yes Lock the region unlock Wait for user No

NCHU System & Network Lab

#include #include #include #include #include int main() { struct flock fl = { F_WRLCK, SEEK_SET, 0, 0, 0 }; int fd; fd = open(“test", O_RDWR) fcntl (fd, F_SETLKW, &fl) ; // set the lock ‘fl’ getchar(); printf(“file is locked…”); … exit(1); }

NCHU System & Network Lab Lab 2 Use the program you have done before : –You try to make a deadlock situation to see that how does Linux solve this problem.

NCHU System & Network Lab Reference Advanced Programming in the UNIX Environment 2nd Author : Richard Stevens, Stephen A.Rago, Publisher : Addison-Wesley Beginning Linux Programming Author : Richard Stones, Neil MatthewPublisher : Wrox jollen’s Bloghttp://