- Qt Designer
- Review
-
- InterProcess Communications II
- pipes, fifos
- locks
- message passing
- semaphores
- shared memory
- BSD sockets
- client-server model
- FIFOs
- fifo is a named pipe
- created with system call
mknod(path, mode)
mkfifo(path, mode)
- S_IFIFO (stat.h) added to mode to get fifo
- after creation, stays until unlinked
- after creation, opened just as if file
- created with command
/etc/mknod name p
mkfifo name
- ls -l flags fifo's with a `p'
$ /etc/mknod myfifo p
$ ls -l myfifo
prw-rw-r-- matthews fac ... myfifo
$ wc myfifo &
$ ls -l > myfifo
123 32 3
Also could use mkfifo.
- [Creation]
#include stat.h
if(mkfifo("myfifo",0666) < 0)
perror(...)
- [Opening]
#include fcntl.h
if((fd = open("myfifo", O_RDONLY)) < 0)
perror(...)
- [Opening]
- [no delay]
#include fcntl.h
if((fd=open("my",O_RDONLY | O_NDELAY))<0)
perror(...)
\end{itemize}
- Lock Files
- there is a "lock file" associated with each data file
- if the lockfile exists then cannot access data file
- if the lockfile does not exist then can access data file
- Check if lockfile exists
fd = open("some.lock", O_WRONLY|O_CREAT|O_EXCL, 0644)
if (fd < 0 && eerno == EEXIST) ... /*"file locked"*/
else if (fd < 0 ) ... /*file opening error*/
else{ /* I got the file */
- Record Locking - The Problem
- in Unix multiple processes can have the same file open
- this presents problems if there are multiple processes that can update
- Example: Class Roll for CSCE 590
The file csce 590 has the number of empty slots and the roll for the class
csci object oriented programming.
What happens when there are two editor sessions open with the same file?
- The lockf system call
#include unistd.h
int lockf(int fildes, int function, off_t size);
- IPC Facility Keys
- IPC Keys are used by the System V IPC communication routines to uniquely
identify an IPC object much in the way a pathname identifies a file.
- key_t ``key type'' defined in stdio.h
- ftok - non-standard library function that maps path to a key
- The function Ftok
- [Header Files:] sys/types.h sys/ipc.h
- [Parameters:]
- [] char *pathname;
- [] char id;
- [USAGE:] keyval = ftok(path, id);
- The function Ftok
- IPC / IO Anology
{\bf Notes on FTOK}
- The id is an additional factor so that there can be more than 1 key assoc
iated with a file.
- returns (key\_t) -1 if the path does not exist
- if one deletes and then puts back the file key values may be different
\newpage
{\bf IPC / IO Anology}
IPC keys are used:
- to create IPC objects,
- to gain access to existing IPC objects.
- System V IPC objects:
- msg_queues
- semaphores
- shared memory segments
- maintained in system wide table
- Semaphores-
Semaphores were introduced by Edgar Dijkstra to provide a mechanism for the
synchronization of processes.
p(sem) or wait operation
if (sem != 0)
decrement sem by one
else
wait until sem becomes non-zero
- v(sem) or signal operation
if (queue of waiters non-empty)
restart first process in the queue
else
increment sem by one
- Critical Sections
- Mutual Exclusion
- msgqueue calls
- int msgget(key_t key, int msgflg);
- int msgrcv (msqid, msgp, msgsz, msgtyp, msgflg)
- int msgsnd (msqid, msgp, msgsz, msgflg)
- int msgctl(int msqid, int cmd, struct msqid_ds, buf)
- Removing an ipc object
- ipcs - "ipc ls"
- ipcrm -q fid
- msgctl(shmid, IPC_RMID, 0, 0)
- [Readings:]
- Chapter 12 for file locking
- [Assignment:]
-
- [Reminders:]
- Test Nov 16
- Previous Lecture -
- Index -
- Next Lecture -
URL = http://www.cs.sc.edu/~matthews/Courses/510/Lectures/lec20.html