Advanced Unix Programming Module 06 Raju Alluri spurthi.com

Similar documents
ADVANCED I/O. ISA 563: Fundamentals of Systems Programming

ISA 563: Fundamentals of Systems Programming

MMAP AND PIPE. UNIX Programming 2015 Fall by Euiseong Seo

FILE SYSTEMS. Jo, Heeseung

Programmation Systèmes Cours 8 Synchronization & File Locking

File Systems Overview. Jin-Soo Kim ( Computer Systems Laboratory Sungkyunkwan University

CMPS 105 Systems Programming. Prof. Darrell Long E2.371

CS631 - Advanced Programming in the UNIX Environment. Dæmon processes, System Logging, Advanced I/O

which maintain a name to inode mapping which is convenient for people to use. All le objects are

File I/0. Advanced Programming in the UNIX Environment

Computer Programming Practice (2008 Winter) Practice 10 C Scoping & Pointer

CS 33. Files Part 2. CS33 Intro to Computer Systems XXI 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

o Code, executable, and process o Main memory vs. virtual memory

14. Memory API. Operating System: Three Easy Pieces

Dynamic memory allocation

read(2) There can be several cases where read returns less than the number of bytes requested:

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Information Sciences and Engineering

Memory (Stack and Heap)

Reminder of midterm 1. Next Thursday, Feb. 14, in class Look at Piazza announcement for rules and preparations

VIRTUAL FILE SYSTEM AND FILE SYSTEM CONCEPTS Operating Systems Design Euiseong Seo

Lecture 8 Dynamic Memory Allocation

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

LSN 3 C Concepts for OS Programming

Memory Management. CSC215 Lecture

Princeton University Computer Science 217: Introduction to Programming Systems. Dynamic Memory Management

Structures and Pointers

The Environment of a Unix Process

Dynamic Memory Management. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Pointers. Héctor Menéndez 1. November 28, AIDA Research Group Computer Science Department Universidad Autónoma de Madrid.

Fall 2017 :: CSE 306. File Systems Basics. Nima Honarmand

Dynamically Allocated Memory in C

I/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo

How do we define pointers? Memory allocation. Syntax. Notes. Pointers to variables. Pointers to structures. Pointers to functions. Notes.

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

I/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo

Princeton University. Computer Science 217: Introduction to Programming Systems. Dynamic Memory Management

Reminder: compiling & linking

Chapter 14. Dynamic Data Structures. Instructor: Öğr. Gör. Okan Vardarlı. Copyright 2004 Pearson Addison-Wesley. All rights reserved.

C Structures & Dynamic Memory Management

2/9/18. Secure Coding. CYSE 411/AIT681 Secure Software Engineering. Agenda. Dynamic Memory Interface. Dynamic Memory Interface

Processes. Johan Montelius KTH

A process. the stack

CS 33. Intro to Storage Allocation. CS33 Intro to Computer Systems XXVI 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Operating systems. Lecture 9

Input and Output System Calls

Memory and C/C++ modules

Programming. Pointers, Multi-dimensional Arrays and Memory Management

Operating System Labs. Yuanbin Wu

UNIX System Programming

UNIX SYSTEM PROGRAMMING

Memory Allocation in C C Programming and Software Tools. N.C. State Department of Computer Science

CSE 333 SECTION 3. POSIX I/O Functions

Dynamic Memory Allocation

1. Lab exercise C programming language

ANITA S SUPER AWESOME RECITATION SLIDES

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

Programming in C. 4. Misc. Library Features, Gotchas, Hints and Tips. Dr. Neel Krishnaswami University of Cambridge

Introduction to Computer Systems /18 243, fall th Lecture, Oct. 22 th

Memory Management I. two kinds of memory: stack and heap

CMPS 105 Systems Programming. Prof. Darrell Long E2.371

Content. In this chapter, you will learn:

CSE 410: Systems Programming

Secure Software Programming and Vulnerability Analysis

The bigger picture. File systems. User space operations. What s a file. A file system is the user space implementation of persistent storage.

19-Nov CSCI 2132 Software Development Lecture 29: Linked Lists. Faculty of Computer Science, Dalhousie University Heap (Free Store)

mmalloc The GNU memory-mapped malloc package Fred Fish Cygnus Support Mike Haertel Free Software Foundation

Dynamic Allocation of Memory

Dynamic Memory Allocation I Nov 5, 2002

Outline. Relationship between file descriptors and open files

In Java we have the keyword null, which is the value of an uninitialized reference type

Section 2: Processes

Memory management. Johan Montelius KTH

Compilation/linking revisited. Memory and C/C++ modules. Layout of C/C++ programs. A sample C program demo.c. A possible structure of demo.

Memory and C/C++ modules

Files. Eric McCreath

Dynamic Memory Allocation I

File Systems. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

Project 3a: Malloc and Free

Parents and Children

RPC / Network FSes 1

CS2028 -UNIX INTERNALS

CSC 1600 Memory Layout for Unix Processes"

COSC Software Engineering. Lectures 14 and 15: The Heap and Dynamic Memory Allocation

Section 3: File I/O, JSON, Generics. Meghan Cowan

Lecture files in /home/hwang/cs375/lecture05 on csserver.

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

Memory Management. CS449 Fall 2017

CSci 4061 Introduction to Operating Systems. File Systems: Basics

Preview. System Call. System Call. System Call. System Call. Library Functions 9/20/2018. System Call

CS240: Programming in C

Dynamic memory. EECS 211 Winter 2019

CSE 333 SECTION 3. POSIX I/O Functions

Heap Arrays. Steven R. Bagley

libxcpc(3) Exception and resource handling in C libxcpc(3)

CSC 271 Software I: Utilities and Internals

COSC345 Software Engineering. The Heap And Dynamic Memory Allocation

Transcription:

Advanced Unix Programming Module 06 Raju Alluri askraju @ spurthi.com

Advanced Unix Programming: Module 6 Data Management Basic Memory Management File Locking

Basic Memory Management Memory allocation using malloc() Allocate a specific number of bytes void *malloc(size_t size); Allocate memory for size number of bytes The contents of the memory allocated are indeterminate So the calling program should take care of initializing the memory

Basic Memory Management Memory allocation using calloc() Allocate memory for a specific number of objects, each of a specific size void *calloc(size_t nobj, size_t size); Allocate memory for nobj number of objects, each of which is of size size number of bytes The allocated space is reinitialized to bits of value 0

Basic Memory Management Memory allocation using realloc() Reallocate given memory by either increasing or decreasing the size void *realloc(void *ptr, size_t newsize); Increase/Decrease the memory pointed to by ptr to newsize. If the size is increased, it may involve moving all the current contents pointed to by ptr to a new location If the size is increased, the contents of the augmented memory is indeterminate

Basic Memory Management Freeing the memory void free(void *ptr); Free the memory pointed to by ptr. The memory might be reused for subsequent allocation operations

File and Record Locking Locking Used to determine the control of a process or thread over a block of memory, a file or part of a file Owner of the lock can perform the operation and release a lock Other processes and threads can wait until a lock is released File Locking Process of locking an entire file Record Locking Process of locking part of a file, from a start location to end location

File and Record Locking Case Study Assigning sequence numbers The current number is stored in a file Process interested in a sequence number Read the number from the file and keep it for using Increment the number and write it back to the file Q: What if two processes read the number from the file simultaneously

File and Record Locking Advisory Locking OS remembers the lock on a file Doesn't enforce writing on to locked files Useful for cooperating processes Mandatory Locking Operating system enforces the locks Enabling a file for mandatory locking Set Group exectue bit to be off Set set group ID bit to on

Record Locking using fcntl() Lock specific part of the file int fcntl(int fd, int cmd,... /*struct flock *arg */); The flock structure is defined by struct flock { short l_type; /* F_RDLCK, F_WRLCK, F_UNLCK*/ short l_whence; off_t l_start; off_t l_end; pid_t l_pid; }

Record Locking using fcntl() Lock specific part of the file int fcntl(int fd, int cmd,... /*struct flock *arg */); Cmd is one of F_SETLK: Set a read write lock (non waiting) F_SETLKW: Set a read write lock (waiting) F_GETLK: Get attributes of the existing lock

Other Locking Techniques Other Unix Locking techniques Use a specific file as a lock to contents of another file Use the link() system call to create a lock file We can use a temp filename (which contains the pid) to create a temp file and then create a link to it Fails if the link/file already exists Create a lock file using creat() with no write access This will prevent other processes (even with super user permissions) to create the same file Create the lock file using open() and give the flags O_CREAT and O_EXCL.