Semaphore Question

This is a discussion on Semaphore Question within the C Programming forums, part of the General Programming Boards category; Hi, I am making a semaphore application. I make a child process which waits for the semaphore object to be ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    13

    Semaphore Question

    Hi,

    I am making a semaphore application. I make a child process which waits for the semaphore object to be free from the main process. And when its free than it accesses it.

    For some reason its not printing the correct result:

    What I want is that child process should wait for the semaphore to be free from the main process and when its free it accesses it. But for some reason it goes to the child process and prints out the values without waiting for the semaphore to be free

    Code:
    semid = CreateSemaphore(key,1,vals);
    
    	// Begin the process and wait for the semaphore 
    	pid = fork(); 
    
    	if(pid == 0) // CHILD PROCESS
    	{
    	  NEWLINE;
    	  printf("Process begins and wait for the semaphore"); 
    		
    	   sops[0].sem_num = 0; /* We only use one track */
           sops[0].sem_op = -1; /* wait for semaphore flag to become zero */
           sops[0].sem_flg = 0; /* take off semaphore asynchronous  */ 
    
    	   sleep(6);
    	   semop(semid,&sb,1);
    	   NEWLINE;
    	   printf("Process enters the semaphore");	
    	       
    	}
    	else // Main Process
    	{
    		sleep(5); 
    		NEWLINE;
    		printf("Parent Releases the Semaphore"); 
    		sops[0].sem_num = 0; 
    		sops[0].sem_op = 1; // releases the semaphore 
    		sops[0].sem_flg = 0;
    	}

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    Is a semaphore a kernel object? You might want to use a mutex, I know that's a kernel object.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    13
    Hi,

    Thanks for the reply. Well, but should'nt it automatically assign the semaphore object to the process that is waiting for it. I code this thing in C# (C Sharp) using Semaphore Class and it worked smoothly. I am not sure seems like I am missing something. I need to release the semaphore object from the main method and than other processes will be able to access it.

  4. #4
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    well, it may be different. I don't know a whole lot about using fork, but if it creates another process and not just another thread then you may need a mutex. In your C# program, did you create another process or did you just create another thread?

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    13
    In my c# application I created a new THREAD. I guess I do have to use Mutex.

    Thanks for the advice!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. semaphore
    By menzeli in forum Linux Programming
    Replies: 12
    Last Post: 04-15-2007, 10:26 AM
  2. semaphore
    By menzeli in forum C Programming
    Replies: 1
    Last Post: 03-24-2007, 12:34 PM
  3. clueless about the semaphore object
    By y_cant_i_C in forum Windows Programming
    Replies: 1
    Last Post: 10-25-2006, 02:03 PM
  4. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 03:51 PM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21