Write a C Program to allow cooperating processes to lock a resource for exclusive use,using a) Semaphores, b) Flock or lockf system calls.
AIM:
Write a C Program to allow cooperating processes to lock a resource for exclusive use,using a)Semaphores,b)Flock or lockf system calls
Program:
#include<stdio.h> #include<stdlib.h> #include<fcntl.h> #include<unistd.h> int main(int argc,char* argv[]) { struct flock f1={f_wrlck,seek_set,0,0,0); int fd; f1.l_type=f_rdlck; if((fd=open(‘rr.txt”,o_rdwr))==-1) { perror(“open”); exit(1); } printf(“press<return> to try to get lock:”); getchar(); printf(“trying to get lock”): if(fnctl(fd,f_setlkw,&f1)==-1) { perror(“fcntl”); exit(1); } printf(“got lock \n”); printf(“press <return> to release lock:”); getchar( ); f1.l_type=f_unlck; if(fcntl(fd,f_setlk,&f1)==-1) { perror(“fcntl”); exit(1); } printf(“unlocked\n”); close(fd); }
Output:
press<return> to try to get lock trying to get lock press <return> to release lock unlocked
-
UpdatedOct 23, 2014
-
Views7,615
Write a Shell script to find factorial of a given integer.
Write in C the following Unix commands using system calls A). cat B). ls C). mv
Write a Shell script that displays list of all the files in the current directory to which the user has read, Write and execute permissions.
Write a Shell script that accepts a filename, starting and ending line numbers as arguments and displays all the lines between the given line numbers.
Write a C program to emulate the Unix ls-l command.
Write a Shell script to list all of the directory files in a directory.