synchronize as in java


 
Thread Tools Search this Thread
Top Forums Programming synchronize as in java
# 1  
Old 03-09-2004
synchronize as in java

Hi,
I am trying to implement the synchronize feature of java using C. I am using a semaphore for the same. I have a wrapper called "synch" to which I pass the function pointer(any_fn). This pointer points to the function (my_fn) which needs to be synchronized.
However to create the semaphore I need to pass a semkey which should be a indicative of the function (my_fn). I cannot use the address of this function (my_fn) because every binary will execute in its own address space and the pointer will always be different, and hence the semkey will also be different.

Can anyone help me generate this semkey.
# 2  
Old 03-09-2004
OK, use 7,564,263.

I'm tempted to end my reply there, but I guess some explanation is in order. Smilie

Suppose for a moment that your my_fn function was going to be used in a single executable so that the semaphore would only need to syncronize among simultaneous instances of that single executable. In that case, you could go ahead and use the address of the function. But what is magic about that address? The loader could, in theory, put another function in another executable at the same address.

There are billions of possible key values, but I have never seen a system that needs more than a few dozen. That makes this a small problem. Just pick something and go with that. It's like picking a name for your function or picking a file name.

Collisions are possible and you should be ready for them. Always test your return codes. And I would put the value in a config file and read it once. This makes it easy to change if that is ever needed. The my_fn could do this internally by using a static value initialized to zero. If zero, read the value and store it in the static variable.

Or you can just store the key as a constant in my_fn and risk the need to recompile should a collision occur. I've done that too.

There is also a function called ftok() that you should look at. It takes a pathname and a small integer and returns a key. I never use it, but if I don't mention it, someone else will. Smilie
# 3  
Old 03-09-2004
Hi Perderabo,
thanks for the reply.
However, initially i started with the idea of ftok. but since i am creating the semaphore in the synch function itself, i m calling the ftok there and so it will always return te same key.
then after this i took up the second approach, about the static variable, however i do not find this graceful and so i have posted this here. having a static variable is ungraceful ( i feel) because someone who wants to use my synch function may set the value of this static variable to a existing value(vaue being used in some other function).
the graceful way i feel is to have some kind of a mechanism to get the key from the function name itself. And offcourse this key is to be generated in the synch function ( or be made availabel to it). So I thought of trying to pass it as the function argument. But again I am passing a function pointer to synch, so dont know how to make this argument available to synch.

Hope, I have made myself clear.

Thanks and waiting patiently for a cool solution.
# 4  
Old 03-09-2004
I may be wrong in understanding the situation but please let me know if I am.

When I went through the post I read statement as stated by linuxpenguin " .... and hence the semkey will also be different " which concludes to me that he is in need of a semkey which should be unique.


But later in his post he says "... i m calling the ftok there and so it will always return the same key".

Is it that he is looking for a dynamic semkey ... please clarify ?
# 5  
Old 03-09-2004
Prasad,

I know I have made it confusing. But very true, I am looking for a dynamic kind of a semkey, which will always have the same value if and only if the synch function pointer points to a particular function. This semkey will have a different value if the function pointer points to another function.
my_fn
fn1
fn2

are three functions
any_fn points to one of these at a time.

from wherever I call synch(any_fn)

if any_fn is my_fn then semkey should be unique say sk
if any_fn is fn1 then semkey should be unique say sk1
if any_fn is fn2 then semkey should be unique say sk2
and so on

if I again synch (any_fn) twice thrice or as many times with any_fn pointing to say fn2, then semkey should always be sk2

if i call synch(any_fn) any number of times with any_fn pointing to fn1 then semkey should always be sk1

I hope this makes it much clear now.
# 6  
Old 03-09-2004
For more clarity here is the code I am using
Code:
/****************** myincludes.h**********************/
#include<stdio.h>     
#include<sys/types.h> 
#include<stdlib.h>    
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>     
#include<strings.h>   
#include<stdarg.h>    
#include <errno.h>    
#include <sys/ipc.h>  
#include <sys/sem.h>  
/*****EOF *****/

/************** synch.c ******************************/
#include"myincludes.h"  
/* This is just a normal header file with standard .h files in it */                                
                                                                            
int proj;          /* This is the one I am currently using and want to do away with */                                                          
                                                                            
struct sembuf sb[2]={0,0,IPC_NOWAIT, 0,1,SEM_UNDO};                         
                                                                            
int synch(any_fn)                                                           
int (*any_fn)();                                                            
{                                                                           
    int semid;                                                              
    key_t semkey;                                                           
    semkey =  ftok("/local/psimulation/hemant",proj);                       
    semid=semget(semkey,1,IPC_CREAT|0666);                                  
    if(semid < 0)   {                                                       
        perror("Error creating semaphore\n"); 
        exit(-1);                             
    }                                         
    if( (semop(semid,&sb,2)) < 0 )  {         
        perror("Semop error");                
        exit(-1);                             
    }                                         
    printf("resource locked\n");              
    (any_fn)();                               
    sb[0].sem_op = -1;                        
    sb[0].sem_flg = SEM_UNDO;                 
    if( (semop(semid,&sb,2)) < 0 )  {         
        perror("Semop error\n");              
        exit(-1);                             
    }                                         
    printf("resource unlocked\n");            
    sb[0].sem_op = 0;                         
    sb[0].sem_flg = IPC_NOWAIT;               
    return;
}
/*****EOF *****/

/************** p1.c ******************************/
/* cc -o p1 p1.c  synch.c */
#include"myincludes.h"      
extern int proj;            
                            
int myfn()                  
{                           
        printf("p1.myfn\n");
        while(1)        {   
                sleep(5);   
                break;      
        }                   
}                           
                            
void main(argc, argv)       
int argc;                   
char *argv[];               
{                           
        proj=31;            /* unique for myfn */
        synch(myfn);        
}                           
/*****EOF *****/

/*******************************************************/

Now execute p1 simultaneously. This is working. I want a better solution for proj and would like to have the semkey created in synch.

Code tags added for readability -- Perderabo

Last edited by Perderabo; 03-10-2004 at 01:14 AM..
# 7  
Old 03-10-2004
Quote:
if any_fn is my_fn then semkey should be unique say sk
if any_fn is fn1 then semkey should be unique say sk1
if any_fn is fn2 then semkey should be unique say sk2
and so on
I tried a sample program with an array of function pointers and when I de-referenced it at any instances only the required code was executed.

Definitely the function pointer array contents were different but constant.

I might have wrongly analyzed the post.... but does this output helps you in anyway.

#include <stdio.h>

void (*fn[3])();

void my_fn()
{
printf ("my_fn\n" ) ;
}

void fn1()
{
printf ("fn1\n" ) ;
}

void fn2()
{
printf ("fn2\n" ) ;
}

main ( )
{
fn[0]=my_fn;
fn[1]=fn1;
fn[2]=fn2;

(*fn[0])();
(*fn[1])();
(*fn[2])();
}

Output:

my_fn
fn1
fn2

Is it possible to then take fn[0],fn[1],fn[2].... as semkey?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

How to synchronize two different files?

Hello, I have the latest stable release of a UNIX-like O.S. in the ISO format, and want to synchronize it with the latest Release Candidate (RC) of it, in order to reducing bandwidth usage. The ISO images for that O.S. is provided via different protocols including rsync, FTP and HTTP Is this... (4 Replies)
Discussion started by: temp-usr
4 Replies

2. AIX

Verify and Synchronize HACMP

Hi Earlier we used to move the resource groups between nodes manually. Recently I have added the HACMP startup and stop scripts. Then I moved the resource group to see if everything works fine as per the startup and stop scripts, but it didn't work out as expected. Do i need to do bring... (4 Replies)
Discussion started by: samsungsamsung
4 Replies

3. Shell Programming and Scripting

How to synchronize using FTP

Hi everyone! I need to write a script that will synchronize two servers using FTP. So basically the script will get only the files that exist on the remote server that do not exist on the local server. Is there an option to do this when using mget? If not, is there a way to copy over only the... (2 Replies)
Discussion started by: Fatbob
2 Replies

4. Shell Programming and Scripting

Synchronize Files-Help

Hi, I have two servers1&2, one is not in the network. Cant communicate from it to other servers. The second one can communicate to above mentioned server. I am trying a script which synchronizes files between server 1 an 2? server1: cant communicate to any other servers server2: can... (4 Replies)
Discussion started by: Tuxidow
4 Replies

5. Programming

synchronize two processes

i am trying to synchronize between father process and son process created by fork() command, to print simultaneously. my program is written in c under bash shell. the compile goes ok but when i try to run nothing happens and the program doesnot end. my code is: #include <stdio.h>... (1 Reply)
Discussion started by: emil2006
1 Replies

6. UNIX for Advanced & Expert Users

synchronize processes

hi. i am writing a c program under bash shell. i would like to use semaphore functions like sem_wait(), sem_post() and i included <semaphore.h> and it compailes fine but when i try to run it gives me an error "undefined reference to sem_wait() , sem_post() , sem_init()" what have i missed... (2 Replies)
Discussion started by: emil2006
2 Replies

7. Programming

need a way to synchronize processes

I am writing a program in C for my networking class, so I am relatively new to this. To begin, I have 7 processes that need do send messages to every other one, and every one of them needs to receive the messages sent by others. I am using fork() to create 6 more processes. The message... (1 Reply)
Discussion started by: inabramova
1 Replies

8. Shell Programming and Scripting

how to synchronize different dirs

I have 4 directory Dir1 file1 file2 file3 file4 Dir2 file3 file5 file6 file8 Dir3 file1 file2 file6 file9 file10 Dir4 file3 file6 file12 file15 and all the 4 dirs are having couple of files. Few of the files are common to other directory/ies and few... (1 Reply)
Discussion started by: reldb
1 Replies

9. Shell Programming and Scripting

How to synchronize all the linux machine?

Hi, I have three Linux machine have three different times. Can I synchronize them using one process? I have root access. Thanks! (1 Reply)
Discussion started by: whatisthis
1 Replies
Login or Register to Ask a Question