FILE LOCK #include <stdio.h> #include <stdlib.h>

Slides:



Advertisements
Similar presentations
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Advertisements

CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Unix IPC and Synchronization. Pipes and FIFOs Pipe: a circular buffer of fixed size written by one process and read by another int pipe(int fildes[2])
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.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
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.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
TDC368 UNIX and Network Programming Camelia Zlatea, PhD Week 6:  Inter-Process Synchronization  Signals.
Informationsteknologi Monday, September 10, 2007Computer Systems/Operating Systems - Class 31 Today’s class Review of more C Operating system overview.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Concurrency 1 CS502 Spring 2006 Thought experiment static int y = 0; int main(int argc, char **argv) { extern int y; y = y + 1; return 0; }
Inter Process Communication. Introduction Traditionally describe mechanism for message passing between different processes that are running on some operating.
Unix Processes Slides are based upon IBM technical library, Speaking Unix, Part 8: Unix processes Extended System Programming Laboratory (ESPL) CS Department.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
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.
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.
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Page 111/15/2015 CSE 30341: Operating Systems Principles Chapter 11: File System Implementation  Overview  Allocation methods: Contiguous, Linked, Indexed,
CSNB334 Advanced Operating Systems 4. Concurrency : Mutual Exclusion and Synchronization.
Laface 2007 File system 2.1 Operating System Design Filesystem system calls buffer allocation algorithms getblk brelse bread breada bwrite iget iput bmap.
Interprocess Communication. Resource Sharing –Kernel: Data structures, Buffers –Processes: Shared Memory, Files Synchronization Methods –Kernel: Wait.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Tarek Abdelzaher Vikram Adve CS241 Systems Programming System Calls and I/O.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
The horrors Nezer (T.A) and Evyatar (Bodek). This ex – what we learned planned Stream sockets – TCP Stream sockets – UDS Select, blocking and non blocking.
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
Process Synchronization. Concurrency Definition: Two or more processes execute concurrently when they execute different activities on different devices.
1 Reading compiler errors ls2.c:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘:’ token In file included from /usr/include/stdio.h:75,
The Shell What does a shell do? - execute commands, programs - but how? For built in commands run some code to do the command For other commands find program.
A process is a program in execution A running system consists of multiple processes – OS processes Processes started by the OS to do “system things” –
Andrew Hanushevsky: Basic I/O API’s
CSNB334 Advanced Operating Systems 4
Synchronization: Basics
Process Synchronization
Andrew Hanushevsky: Memory Mapped I/O
Inter Process Communication (IPC)
Inter-Process Communication Pipes Moti Geva
Protection of System Resources
Understand argc and argv
I2C communication* I²C - Inter-Integrated Circuit – or –I squared C)
Project 5: Minix Semaphores
POSIX Threads 1. Background 2. Threads vs. Processes
Lecture 5: Process Creation
Concurrency: Mutual Exclusion and Synchronization
C Stuff CS 2308.
LINUX System Programming with Processes (additional)
Genesis: From Raw Hardware to Processes
Outline Allocation Free space management Memory mapped files
CSE 333 – Section 3 POSIX I/O Functions.
Process Creation Process Termination
Tutorial 3 Tutorial 3.
Programming Assignment # 2 – Supplementary Discussion
Inter-Process Communication
File I/O (1) Prof. Ikjun Yeom TA – Mugyo
IPC Prof. Ikjun Yeom TA – Hoyoun
CSE 333 – Section 3 POSIX I/O Functions.
Concurrency: Mutual Exclusion and Process Synchronization
Synchronization Primitives – Semaphore and Mutex
Virtual Memory: Systems CSCI 380: Operating Systems
dup, dup2 An existing file descriptor (filedes) is duplicated
FILE I/O File Descriptors I/O Efficiency File Sharing
CSE 153 Design of Operating Systems Winter 19
Synchronization.
Lecture 20: Synchronization
File I/O & UNIX System Interface
CSE 451 Section 1/27/2000.
Intro to the Shell with Fork, Exec, Wait
Presentation transcript:

FILE LOCK #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <fcntl.h> #include <unistd.h> int main(int argc, char *argv[]) { /* l_type l_whence l_start l_len l_pid */ struct flock fl = { F_WRLCK, SEEK_SET, 0, 0, 0 }; int fd; fl.l_pid = getpid(); if (argc > 1) fl.l_type = F_RDLCK; if ((fd = open("lockdemo.c", O_RDWR)) == -1) { perror("open"); exit(1); } printf("Press <RETURN> to try to get lock: "); getchar(); printf("Trying to get lock..."); if (fcntl(fd, F_SETLKW, &fl) == -1) { perror("fcntl"); printf("got lock\n"); printf("Press <RETURN> to release lock: "); fl.l_type = F_UNLCK; /* set to unlock same region */ if (fcntl(fd, F_SETLK, &fl) == -1) { printf("Unlocked.\n"); close(fd); FILE LOCK

FIFO : speak.c #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #define FIFO_NAME "american_maid" main() { char s[300]; int num, fd; /* don't forget to error check this stuff!! */ mknod(FIFO_NAME, S_IFIFO | 0666, 0); printf("waiting for readers...\n"); fd = open(FIFO_NAME, O_WRONLY); printf("got a reader--type some stuff\n"); while (gets(s), !feof(stdin)) { if ((num = write(fd, s, strlen(s))) == -1) perror("write"); else printf("speak: wrote %d bytes\n", num); }

FIFO : tick.c #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #define FIFO_NAME "american_maid" main() { char s[300]; int num, fd; /* don't forget to error check this stuff!! */ mknod(FIFO_NAME, S_IFIFO | 0666, 0); printf("waiting for writers...\n"); fd = open(FIFO_NAME, O_RDONLY); printf("got a writer:\n"); do { if ((num = read(fd, s, 300)) == -1) perror("read"); else { s[num] = '\0'; printf("tick: read %d bytes: \"%s\"\n", num, s); } } while (num > 0);

Shared Memory Examples Refer to class webpage shm1.c , shm2.c

Semaphore a special variable (sv) upon which only two operations are allowed P(sv) : wait if sv is greater than zero, decrement sv. If sv is zero, suspend execution of the process V(sv) : signal If some other process has been suspended for waiting sv, make it resume execution If no process is suspended waiting for sv, increment sv. They are used to ensure that a single executing process has exclusive access to a resource. Critical Section Binary Semaphore

Semaphore semaphore sv = 1; loop forever { P(sv); critical section code; V(sv); non-critical code section }

Example Code : sem1.c Refer to class webpage.

Memory mapped File I/O a segment of virtual memory which has been assigned a direct byte-for-byte correlation with some portion of a file or file-like resource. this correlation between the file and the memory space permits applications to treat the mapped portion as if it were primary memory.

Memory Mapped File I/O Benefits : better performance Drawbacks increased I/O performance, especially when used on small files a system call is orders of magnitude slower than a simple change of program's local memory Many benefits of virtual memory (e.g. lazy loading for large file, etc) Drawbacks Drawbacks of virtual memory (e.g. page fault) Difficult in handling very large file (bigger than possible maximum memory) ? Common Use Process loader to bring executable file into memory