1752767 Members
5306 Online
108789 Solutions
New Discussion юеВ

Open & locking files

 
SOLVED
Go to solution
Walter K. Maul Jr.
Occasional Advisor

Open & locking files

Hello, need a C program to open a file, read the list of filenmanes from the file, then loop to open each file and lock it. We are testing the auto tune ability of the nflocks on the server. Or is there another/better way to cause the nflocks to increase. Needs to be C, because we have not other compiler. Any help would be great.
4 REPLIES 4
Laurent Menase
Honored Contributor
Solution

Re: Open & locking files

#include
#include
#include
main()
{
static char buf [8192];
while( scanf("%s",&buf)>0)
{
int f;
f=open(buf,O_RDWR,0);
if (f<0) { perror(buf); continue; }
if (lockf(f,F_TLOCK,4096))
printf("%s already locked\n",buf);
else
printf("%s locked\n",buf);
}
sleep (100000);
}


a.out
Dennis Handly
Acclaimed Contributor

Re: Open & locking files

How much do you know about C programming?
There is open(2) and fcntl(2) or lockf(2).

You could use a script to read that file and put the files on the command line of your program:
lock-prog $(< file-list)

Then just loop on argv and open and lock each file.
You can only open up to maxfiles_lim(5) so you will have to increase that kernel parm, or run multiple instances of your lock-prog.

I suppose you could create a 1 Mb file and lock each byte separately and not need to open each file in your list.
Walter K. Maul Jr.
Occasional Advisor

Re: Open & locking files

LM,
where do I modify the code to put my filename that has this list of filenames? I am not a C programmer, I do understand a little of the code.

Thanks again.
James R. Ferguson
Acclaimed Contributor

Re: Open & locking files

Hi Walter:

> where do I modify the code to put my filename that has this list of filenames?

You don't have to modify anything, Compile Laurent's code and redirect the file of your choice to be its input as he showed:

# a.out < filenames

Regards!

...JRF...