|
Home > Archive > Unix Programming > August 2005 > Help! Program error
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Help! Program error
|
|
| crash_zero 2005-08-20, 5:54 pm |
| Hi,
I was playing around with file locks and I keep getting a bad address
error whenever I run the program. Could anyone please tell me why?
Thanks. I've appended my source code below.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
int main() {
FILE *groupsFile;
int f;
if ( (groupsFile = fopen("old", "r")) != NULL){
fcntl(fileno(groupsFile), F_SETLK, F_RDLCK);
}
perror(strerror(errno));
fcntl(fileno(groupsFile), F_SETLKW, F_UNLCK));
f=fclose(groupsFile);
perror(strerror(errno));
printf("%d\n", f );
return 0;
}
| |
| Paul Pluzhnikov 2005-08-20, 5:54 pm |
| "crash_zero" <choo.adrian@gmail.com> writes:
> I was playing around with file locks and I keep getting a bad address
> error whenever I run the program. Could anyone please tell me why?
Because you are supplying incorrect parameter to fcntl.
From "man fcntl":
Advisory locking
F_GETLK, F_SETLK and F_SETLKW are used to acquire, release, and test
for the existence of record locks (also known as file-segment or file-
region locks). The third argument lock is a pointer to a structure
that has at least the following fields (in unspecified order).
struct flock {
...
short l_type; /* Type of lock: F_RDLCK,
F_WRLCK, F_UNLCK */
short l_whence; /* How to interpret l_start:
SEEK_SET, SEEK_CUR, SEEK_END */
off_t l_start; /* Starting offset for lock */
off_t l_len; /* Number of bytes to lock */
pid_t l_pid; /* PID of process blocking our lock
(F_GETLK only) */
...
};
Wrong:
> if ( (groupsFile = fopen("old", "r")) != NULL){
> fcntl(fileno(groupsFile), F_SETLK, F_RDLCK);
Should be:
struct flock lock;
lock.l_type = F_RDLCK;
lock.l_whence = SEEK_SET;
// ... init other lock fields ...
fcntl(fileno(groupsFile), F_SETLK, &lock);
Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
| |
| Pascal Bourguignon 2005-08-20, 5:54 pm |
| "crash_zero" <choo.adrian@gmail.com> writes:
> I was playing around with file locks and I keep getting a bad address
> error whenever I run the program. Could anyone please tell me why?
> [...]
> perror(strerror(errno));
man strerror
This string must not be modified by the application, but
may be modified by a subsequent call to perror() or strerror().
man perror
The routine perror() produces a message on the standard error
output, describing the last error encountered during a call to a
system or library function. First (if s is not NULL and *s is
not NUL) the argument string s is printed, followed by a colon
and a blank. Then the message and a new-line.
So it's either:
fprintf(stderr,"Got this error: %s\n",strerror(errno));
OR:
perror("Got this error: ");
--
__Pascal Bourguignon__ http://www.informatimago.com/
Until real software engineering is developed, the next best practice
is to develop with a dynamic system that has extreme late binding in
all aspects. The first system to really do this in an important way
is Lisp. -- Alan Kay
| |
| crash_zero 2005-08-20, 8:49 pm |
| I see. Thank you! =) Err just one more question if you don't mind.
I'd like to implement a timeout system on this file lock as well..
i.e. Even after the program terminates, if for some reason the lock is
still held, it will release it automatically after a while.
how can I go about doing this?
Thanks again!
| |
| Paul Pluzhnikov 2005-08-20, 8:49 pm |
| "crash_zero" <choo.adrian@gmail.com> writes:
> I'd like to implement a timeout system on this file lock as well..
> i.e. Even after the program terminates, if for some reason the lock is
> still held, it will release it automatically after a while.
There is no need to implement anything. From the same "man fcntl":
As well as being removed by an explicit F_UNLCK, record locks
are automatically released when the process terminates
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^
or if it closes any file descriptor referring to a file on
which locks are held.
Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
| |
| Maxim Yegorushkin 2005-08-22, 7:49 am |
|
Pascal Bourguignon wrote:
[]
> OR:
>
> perror("Got this error: ");
A note: you probably don't need ": " at the end, as these are added by
perror.
|
|
|
|
|