I’ve implemented a library that simulates pipe() system call , based on shared memory .
Now , the code works OK when I’m not using any fork() , e.g. without invoking any child processes .
My library needs to work with any given int main() program , so the basic problem here is that the modifications with the semaphores should be made with in the library , and not in a main program .
The library :
Here is the library :
static int flag = FALSE;
static int mutex_init = 0;
static pthread_mutex_t lock;
#define BUFFER 4096
int my_new_finish()
{
return 1; // always successful
}
void error_out(const char *msg)
{
perror(msg);
exit(EXIT_FAILURE);
}
Now , this library works OK when I’m not using main program that invoke fork() .
But , when I do use fork() , all hell brakes loose .
For example :
#include <stdio.h>
#include <stdlib.h>
int main()
{
int spd, pid, rb;
char buff[4096];
my_new_init();
if (my_new_fifo("tmp_shm_pipe",0666) < 0)
{
perror("my_new_fifo");
exit(1);
}
if (fork())
{
spd = my_new_open("tmp_shm_pipe", O_RDONLY, 0600);
if (spd < 0)
{
perror("PARENT: my_new_open");
exit(1);
}
rb = my_new_read(spd, buff, sizeof(buff));
if (rb > 0)
write(1, buff, rb);
}
else
{
spd = my_new_open("tmp_shm_pipe", O_WRONLY, 0600);
if (spd < 0)
{
perror("SON: my_new_open");
exit(1);
}
my_new_write(spd, "hello world!\n", sizeof("hello world!\n"));
}
my_new_close(spd);
my_new_un_link("tmp_shm_pipe");
my_new_finish();
return 0;
}
My questions are :
-
How can I use semaphores with the above library , where I don’t “know” the
main()program that I’d be given ? -
I’ve tried to put semaphores in the library (not in the
main()program) but
it didn’t work out good . Can you please explain how can I do that correctly ?
Remarks :
-
Please pay attention that this
mainis just an example , and I could be given countless othermainprogram. -
This is homework
Much appreciated
In your function
my_new_inityou’d need to create a shared semaphone in shared memory – but have a guard around it so that it is only called once; this guard would typically be inside the library module using a module static (or class static) variable.Then in my_new_read at the top:
and in my_new_write release the semaphone after you’ve written something.
The above may need improvement as it is possible that sem_wait will return before data is ready so it may be wise to use a control structure at the beginning of your shared memory segment.