Unix programming Term:2008-2009 III B.Tech II semester Unit-V II PPT Slides Text Books: (1)unix the ultimate guide by Sumitabha Das (2)Advanced programming.

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

Recitation By yzhuang, sseshadr. Agenda Debugging practices – GDB – Valgrind – Strace Errors and Wrappers – System call return values and wrappers – Uninitialization.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
CP104 Introduction to Programming Structure II Lecture 32 __ 1 Data Type planet_t and Basic Operations Abstract Data Type (ADT) is a data type combined.
Memory Allocation. Memory A memory or store is required in a computer to store programs (or information or data). Data used by the variables in a program.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Winter2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Advanced Topics.
The Environment of a UNIX Process. Introduction How is main() called? How are arguments passed? Memory layout? Memory allocation? Environment variables.
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.
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.
CS 241 Section Week #2 2/4/10. 2 Topics This Section MP1 overview Part1: Pointer manipulation Part2: Basic dictionary structure implementation Review.
Process Control Hua LiSystems ProgrammingCS2690Process Control Page 1 of 41.
TDC368 UNIX and Network Programming Camelia Zlatea, PhD Week 6:  Inter-Process Synchronization  Signals.
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
Witawas Srisa-an Chapter 6
Unix Process Environment. main Function A C program starts execution with a function called main. The prototype for the main function is: int main (int.
Pointers Applications
BINA RAMAMURTHY UNIVERSITY AT BUFFALO System Structure and Process Model 5/30/2013 Amrita-UB-MSES
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.
Cli/Serv.: procs/51 Client/Server Distributed Systems v Objectives –look at how to program UNIX processes , Semester 1, Processes.
Chapter 4. INTERNAL REPRESENTATION OF FILES
System V IPC Provides three mechanisms for InterProcess Communication (IPC) : Messages : exchange messages with any process or server. Semaphores : allow.
Shell (Part 2). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
4061 Session 23 (4/10). Today Reader/Writer Locks and Semaphores Lock Files.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
UNIX Files File organization and a few primitives.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
1 Shared Memory. 2  Introduction  Creating a Shared Memory Segment  Shared Memory Control  Shared Memory Operations  Using a File as Shared Memory.
System calls for Process management
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
Operating Systems Process Creation
What is a Process? u A process is an executable “cradle” in which a program may run u This “cradle” provides an environment in which the program can run,
Interprocess Communication
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.
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 interface: file and I/O system calls File operations in C/C++? –fopen(), fread(), fwrite(), fclose(), fseek() in C f.open(…), f.close(…) in C++ I/O.
4.1 Operating Systems Lecture 9 Fork and Exec Read Ch
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.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Linux/UNIX Programming APUE (Process Control) 문양세 강원대학교 IT 대학 컴퓨터과학전공.
Stack and Heap Memory Stack resident variables include:
CS 3305A Process – Part II Lecture 4 Sept 20, 2017.
Protection of System Resources
Using Processes.
FILE LOCK #include <stdio.h> #include <stdlib.h>
Tarek Abdelzaher Vikram Adve Marco Caccamo
System Structure and Process Model
System Structure and Process Model
System Structure B. Ramamurthy.
System Structure and Process Model
CGS 3763 Operating Systems Concepts Spring 2013
CSC215 Lecture Memory Management.
Memory Allocation CS 217.
Unix programming Term: Unit-V I PPT Slides
Virtual Memory CSCI 380: Operating Systems Lecture #7 -- Review and Lab Suggestions William Killian.
EECE.2160 ECE Application Programming
CSCI 380: Operating Systems William Killian
Outline Chapter 2 (cont) Chapter 3: Processes Virtual machines
Dynamic Memory – A Review
Pipes One-way channel joining two processes
EECE.2160 ECE Application Programming
Presentation transcript:

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 in Unix environment by Stevens 1

INDEX UNIT-V II PPT SLIDES Srl. No. Module as per Session planner Lecture No. PPT Slide No. 1.data management: management of memory L malloc,free,realloc,calloc L file locking L create locks files,locking regions L use of read/write locking L competing locks, other commands L Deadlocks L8 9. Revision 2

Memory management ANSI C specifies three functions for memory allocation: 1.malloc: which allocates a specified number of bytes of memory. The initial value of the memory is indeterminate. 2.calloc:which allocates space for a specified number of objects of a specified size. The space is initialized to all 0 bits. 3. realloc:which increases or decreases the size of a previously allocated area. 3

#include void *malloc (size_t size); void *calloc (size_t nobj, size_t size); void *realloc (void *ptr, size_t newsize); All three return: non-null pointer if OK, NULL on error. void free(void *ptr); The function free causes the space pointed to by ptr to be deallocated. 4

Record Locking: Record locking is used to describe the ability of a process to prevent other processes from modifying a region of a file while the first process is reading or modifying that portion of the file. It is byte-range locking, since it is a range of a file (possibly the entire file) that is locked. 5

fcntl Record Locking: #include int fcntl(int filedes, int cmd,... /* struct flock *flockptr */ ); Returns: depends on cmd if OK, -1 on error. Here cmd is F_GETLK, F_SETLK, or F_SETLKW. Flockptr is a pointer to an flock structure. 6

struct flock { short l_type; off_t l_start; short l_whence; off_t l_len; pid_t l_pid; }; The type of lock desired: F_RDLCK (a shared read lock), F_WRLCK (an exclusive write lock), or F_UNLCK (unlocking a region). The starting byte offset of the region being locked or unlocked (l_start and l_whence). 7

The size of the region in bytes (l_len). The ID (l_pid) of the process holding the lock that can block the current process (returned by F_GETLK only). Rules about the specification of the region to be locked or unlocked: Locks can start and extend beyond the current end of file, but cannot start or extend before the beginning of the file. If l_len is 0, it means that the lock extends to the largest possible offset of the file. This allows us to lock a region starting anywhere in the file, up through and including any data that is appended to the file 8

To lock the entire file, we set l_start and l_whence to point to the beginning of the file and specify a length (l_len) of 0. Types of locks: 1.Shared lock: read locks are shard locks. Any no.of process can have read lock on the same object at a time. 2. Exclusive lock : Write locks are exclusive locks. When one process puts write lock on the object, no other read lock or write lock is allowed on the same object. 9

Commands for the fcntl function: F_GETLK: Determine whether the lock described by flockptr is blocked by some other lock. F_SETLK: Set the lock described by flockptr. F_SETLKW: This command is a blocking version of F_SETLK. If the requested read lock or write lock cannot be granted because another process currently has some part of the requested region locked, the calling process is put to sleep. 10

Function to lock or unlock a region of a file: #include int lock_reg (int fd, int cmd, int type, off_t offset, int whence, off_t len) { struct flock lock; lock.l_type = type; lock.l_start = offset; lock.l_whence = whence; lock.l_len = len; return(fcntl( fd, cmd, &lock)); } 11

Implied Inheritance and Release of Locks: Locks are associated with a process and a file. when a process terminates, all its locks are released. whenever a descriptor is closed, any locks on the file referenced by that descriptor for that process are released. Locks are never inherited by the child across a fork. 12

Place a write lock on an entire file: #include int lockfile (int fd) { struct flock fl; fl.l_type = F_WRLCK; fl.l_start = 0; fl.l_whence = SEEK_SET; fl.l_len = 0; return(fcntl(fd, F_SETLK, &fl)); } 13

Advisory locking: In Unix, advisory locking is done with flock() and lockf() commands. Both fcntl and flock offer advisory locking. Advisory locking is locking that requires the cooperation of participating processes. advisory locking is sometimes called unenforced locking. 14

Mandatory locking: Is the kernel enforced file locking, as opposed to the more usual cooperative file locking used to guarantee sequential access to files among processes. A file is marked as a candidate for mandatory locking by setting the group-id bit in its file mode but removing the group-execute bit. Mandatory locks can only be applied via the fcntl()/lockf() locking interface. 15

If a process has locked a region of a file with a mandatory read lock, then other processes are permitted to read from that region. If any of these processes attempts to write to the region it will block until the lock is released. If a process has locked a region of a file with a mandatory write lock, all attempts to read or write to that region block until the lock is released. 16

Advisory versus Mandatory Locking Mandatory locking only protects those portions of a file that are locked. Other portions of the file that are not locked may be accessed according to normal UNIX system file permissions. Advisory locking is more efficient because a record lock check does not have to be performed for every I/O request. 17

Deadlock: Deadlock occurs when two processes are each waiting for a resource that the other has locked. When a deadlock is detected, the kernel has to choose one process to receive the error return. 18L8.1

Example of deadlock detection: #include static void lockabyte (const char *name, int fd, off_t offset) { if (writew_lock (fd, offset, SEEK_SET, 1) < 0) err_sys("%s: writew_lock error", name); printf("%s: got the lock, byte %ld\n", name, offset); } int main(void) { int fd; pid_t pid; if ((fd = creat("templock", FILE_MODE)) < 0) err_sys("creat error"); 19L8.2

if (write(fd, "ab", 2) != 2) err_sys("write error"); TELL_WAIT(); if ((pid = fork()) < 0) { err_sys("fork error"); } else if (pid == 0) lockabyte("child", fd, 0); TELL_PARENT( getppid()); WAIT_PARENT(); lockabyte("child", fd, 1); } 20L8.3

else { lockabyte("parent", fd, 1); TELL_CHILD(pid); WAIT_CHILD(); lockabyte("parent", fd, 0); } exit(0); } 21L8.4

The child locks byte 0 and the parent locks byte 1. Then each tries to lock the other's already locked byte. We use the parentchild synchronization routines TELL_xxx and WAIT_xxx so that each process can wait for the other to obtain its lock. $./a.out parent: got the lock, byte 1 child: got the lock, byte 0 child: writew_lock error: Resource deadlock avoided parent: got the lock, byte 0 22