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

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

Embed Size (px)

Citation preview

Page 1: 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

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

1

Page 2: 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

INDEX

UNIT-V II PPT SLIDES

Srl. No. Module as per Session planner Lecture No. PPT Slide No.

1. data management: management of memory L1 1-2

2. malloc ,free ,realloc ,calloc L2 3-9

3. file locking L3 10-17

4. create locks files ,locking regions L4 18-31

5. use of read/write locking L5 32-45

6. competing locks, other commands L6 46-50

7. Deadlocks L8

9. Revision

2

Page 3: 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

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

Page 4: 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

#include <stdlib.h>

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

Page 5: 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

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

Page 6: 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

fcntl Record Locking:

#include <fcntl.h>

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

Page 7: 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

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

Page 8: 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

• 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

Page 9: 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

• 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

Page 10: 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

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

Page 11: 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

Function to lock or unlock a region of a file:

#include <fcntl.h>

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

Page 12: 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

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

Page 13: 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

Place a write lock on an entire file:

#include <unistd.h>

#include <fcntl.h>

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

Page 14: 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

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

Page 15: 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

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

Page 16: 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

• 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

Page 17: 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

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

Page 18: 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

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

Page 19: 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

Example of deadlock detection:

#include <fcntl.h>

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

Page 20: 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

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

Page 21: 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

else {

lockabyte("parent", fd, 1);

TELL_CHILD(pid); WAIT_CHILD(); lockabyte("parent", fd, 0);

}

exit(0);

}

21L8.4

Page 22: 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

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