Re: Safe way to read/write plist
Re: Safe way to read/write plist
- Subject: Re: Safe way to read/write plist
- From: p3consulting <email@hidden>
- Date: Mon, 12 Apr 2004 11:16:00 +0200
You have to implement some sort of interprocess communication here
(semaphore, shared mem, Mach-port, distributed object, Unix pipe,
distributed lock, sockets, distributed notifications,)
See
NSDistributedLock and NSDistributedNotificationCenter documentation.
if you want to follow the Cocoa way (example below)
man 2 pipe
man semget
man shmget
man socket
...
if you prefer the Unix way
Pascal Pochet
email@hidden
PS
// compile from Terminal.app using
// cc -framework Foundation -Wall -o distlock distlock.m
// open 2 terminal windows and launch ./distlock in each one to see
what happens
// You should see something like:
// first one
/*
2004-04-12 11:01:27.052 distlock[11342] locked at 12/04/2004 11:01
2004-04-12 11:01:37.074 distlock[11342] free at 12/04/2004 11:01
*/
// second one
/*
2004-04-12 11:01:43.963 distlock[11344] acquired by somebody else at
12/04/2004 11:01
2004-04-12 11:01:44.966 distlock[11344] acquired by somebody else at
12/04/2004 11:01
2004-04-12 11:01:45.968 distlock[11344] acquired by somebody else at
12/04/2004 11:01
2004-04-12 11:01:47.042 distlock[11344] acquired by somebody else at
12/04/2004 11:01
2004-04-12 11:01:48.045 distlock[11344] locked at 12/04/2004 11:01
2004-04-12 11:01:58.060 distlock[11344] free at 12/04/2004 11:01
*/
#import <Foundation/Foundation.h>
#import <unistd.h>
int main(int argc, char **argv)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init] ;
// lock could be an instance variable of a window controller...
// in that case don't forget to retain it !
// choose the path of the lock to something you are sure both
processes may have access to
// be careful using ~ in the path: the daemon may not be running under
the same login as the application...
NSDistributedLock *lock = [NSDistributedLock
lockWithPath:@"/tmp/distlock"] ;
BOOL needToReadbackBeforeUpdating = NO ;
while (![lock tryLock]) {
NSLog(@"acquired by somebody else at %@",[[lock lockDate]
descriptionWithCalendarFormat:@"%d/%m/%Y %H:%M" timeZone:nil
locale:nil]) ;
// well if you do that in your main application thread you will block
the UI !
// better organize the polling in a more user friendly way in a real
application...
// like fire a repeating timer when you need to update the file...
needToReadbackBeforeUpdating = YES ;
sleep(1) ;
}
NSLog(@"locked at %@",[[NSCalendarDate calendarDate]
descriptionWithCalendarFormat:@"%d/%m/%Y %H:%M"]) ;
// here you should do your real job: updating the plist...
if (needToReadbackBeforeUpdating) {
// somebody else may have modified the copy...
// up to you to merge the changes !
}
// the sleep is just here for the demo...
sleep(10) ;
[lock unlock] ;
NSLog(@"free at %@",[[NSCalendarDate calendarDate]
descriptionWithCalendarFormat:@"%d/%m/%Y %H:%M"]) ;
[pool release] ;
return 0 ;
}
Le avr. 12, 2004, ` 09:00, Matt Jaffa a icrit :
>
Hi,
>
>
In my cocoa App I have a daemon and also another app that are opening
>
the same file reading and writing to it.
>
>
The daemon is always reading from it and occasionally writing to it,
>
but the other App only reads and writes to it a little bit, but it
>
seems to be crashing because both files are trying to write at the
>
same time,
>
>
Is there a way to synchronize these two apps to not cause this error
>
and stop making them crash?
>
>
Thanks,
>
Matt
>
_______________________________________________
>
cocoa-dev mailing list | email@hidden
>
Help/Unsubscribe/Archives:
>
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.