processes synchronization

Hi,

I’ve got a client’s process and three server’s processes. The client uses spawnl() to make servers. I’m going to use message passing among processes therefore each server creates its own channel and writes channel ID to a file. The client process reads channels’ ID from the file
and makes connections. I need some kind of synchronization to prevent reading by client before servers have done writing because fscanf() returns the ‘No such file or directory’ error. I thouhgt about reader/writer locks. But how to use them among processes?

Here is a sample:

the client:

int main(int argc, char *argv[]) {

ad420_pid = spawnl(P_NOWAIT, “path”, “ad420_g”, NULL );
if(ad420_pid == -1){
PrintErr(DEFAULT_ERR);
}
///I need the client block here
///it’s fine when I put sleep() here but it’s not a good solution
temp_file = fopen( “ad420_ids.txt”, “r” );
if (temp_file == NULL ){
PrintErr(DEFAULT_ERR);
exit( EXIT_FAILURE );
}
fscanf( temp_file, “%d”, &ad420_chid );
if(fclose( temp_file ) == EOF){
PrintErr(DEFAULT_ERR);
}

///do connection to the channel and other stuff

return EXIT_SUCCESS;
}

the server:

int main(int argc, char *argv[]) {

chid = ChannelCreate(0);
if(chid == -1){
PrintErr(DEFAULT_ERR);
}

temp_file = fopen( “ad420_ids.txt”, “w” );
if(temp_file == NULL){
PrintErr(DEFAULT_ERR);
}
fprintf( temp_file, “%d\n”, chid );
if(fclose( temp_file ) == EOF){
PrintErr(DEFAULT_ERR);
}

return EXIT_SUCCESS;
}

Isn’t better to use name_attach() and name_open() instead of ChannelCreate() and ConnectAttach()?

then don’t I need synchronize processes?

Not exactly; however, if your server registers a name (via name_attach), the client, can simply try to “name_open” that name in a loop, until it succeeds, which provides synchronization.