For a semaphore to work across processes, it needs to reside in shared memory and to be initialized with pshared==1 You're not putting the semaphore in shared memory. Look up e. G shm_open or mmap.
For a semaphore to work across processes, it needs to reside in shared memory and to be initialized with pshared==1 - You're not putting the semaphore in shared memory. Look up e.g. Shm_open or mmap. You should also initialize the semaphore before you fork() - initializing a sempahore twice doesn't work.
Also use sem_wait rather than sem_trywait as you seem to want to block on the semaphore. If you want sem_trywait at least check if the try part succeeded. EDIT: Corrected source.
#include #include #include #include sem_t * sem; /* MODIFIED: We want a semaphore in shared memory, using a pointer instead */ int shared=0; int pid; void func() { sem_wait(sem); /* MODIFIED &sem to sem */ if(pid==0)printf("Child entered\n"); else if(pid>0)printf("Parent entered\n"); sleep(2); shared++; sem_post(sem); /* MODIFIED &sem to sem */ if(pid==0)printf("Child exited\n"); else if(pid>0)printf("Parent exited\n"); } int main() { /* MODIFIED: Put semaphore in shared memory */ sem = mmap(0, sizeof(sem_t), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); /* MODIFIED: Initial count of 1, so that a sem_wait will succeed */ sem_init(sem,1,1); /* MODIFIED: fork() after sem_init() */ pid=fork(); if(pid==0){ printf("In child\n"); func(); } else { func(); } }.
Erik:If I use, sem_wait( ) the program infinitely waits, once the child enters critical section. – kingsmasher1 Mar 13 at 17:33 @kingsmasher1: Yes, you need to give the semaphore an initial count. See updated answer.
– Erik Mar 13 at 17:41 @Erik: Shared memory is needed in case both processes are independent and needs to co-operate (IPC), but in case of parent-child, the child shares, parent's code. So do you still say, shared memory is required in this case? – kingsmasher1 Mar 13 at 17:41 @Erik: I have given the semaphore count, sem_init(&sem,1,0); the third parameter initializes the semaphore value to "0", is this what you are saying?
– kingsmasher1 Mar 13 at 17:43 @kingsmasher1: Yes. When you fork, initially everything's shared, but marked copy-on-write. As soon as you modify something in one process, the child and parent will have different copies of that something.
– Erik Mar 13 at 17:44.
And check the return values of the sem_* functions! From the man page: The sem_trywait() function shall lock the semaphore referenced by sem only if the semaphore is currently not locked; that is, if the semaphore value is currently positive. Otherwise, it shall not lock the semaphore.So if you don't check what it returns, you don't know if you've locked anything at all.
You are using sem_trywait function,then you should check the value returned by this call so as to ensure sysnchronization... Can refer this for more help.... Hope this helps...
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.