Sale!

CECS-326: Operating Systems Lab 6 Lock files

$30.00

Category:
5/5 - (2 votes)

CECS-326: Operating Systems
Lab 6
Lock files
A lock file can be used by processes as a way to communicate with one another. Do not confuse a lock
file with file/record locking which is an I/O technique for standard file protection in UNIX. A lock file can
be used in the following. A process examines a pre-arranged location for the presence or absence of a lock
file. If the file is present, it takes one set of actions. If the file is absent, it takes another set of actions.
There are two system calls that are useful to create and to remove a lock file:
int creat (const char *path, mode_t mode); Æ to create a file having mode
include files: <sys/types.h>, <sys/stat.h>, <fcntl.h>
success: return lowest available integer file descriptor; failure: return -1, sets errno
int unlink (const char *path); Æ to remove a file.
Success: return 0; failure: return -1, sets errno
1. Study the following programs. Use the following include files: <stdio.h>, <stdlib.h>, <fcntl.h>,
<errno.h>, <sys/types.h>. Check for errors, then compile to have same name and run.
release.c
void main(int argc, char *argv[])
{
char *fname; int fd, sleeptime, n_try, count=0; pid_t pid;
pid = getpid(); srand((unsigned) pid); fname = argv[1]; sleeptime = atoi(argv[2]); n_try = atoi(argv[3]);
while (unlink(fname)!=0)
if (++count < n_try) sleep(sleeptime);
else { printf (“\n Cannot release file\n”); exit(-1);
}
printf (“\n File is released\n”);
}
acquire.c
void main(int argc, char *argv[])
{
char *fname; int fd, sleeptime, n_try, count=0; pid_t pid;
pid = getpid(); srand((unsigned) pid); fname = argv[1]; sleeptime = atoi(argv[2]); n_try = atoi(argv[3]);
while ((fd = creat(fname, 0)) == -1 && errno == EACCES)
if (++count < n_try) sleep(rand()%sleeptime);
else { printf (“\n Unable to generate.\n”); exit(-1);
}
close (fd); printf (“\n File %s has been created\n”, fname);
}
2. Run the two programs separately and concurrently. To allow the two programs to execute
concurrently, one program is placed in the background by using an &, and the other program is
placed in the foreground. Try different invocations of the programs with various values for the
sleeptime and the number of tries. Examples of typical runs are:
acquire lock1 10 5
release lock1 2 2
release lock1 10 5 & acquire lock1 5 1
acquire lock1 5 4 & release lock1 10 2, etc.
Observe the results. Explain the results.
END OF LAB 6

PlaceholderCECS-326: Operating Systems Lab 6 Lock files
$30.00
Open chat
Need help?
Hello
Can we help?