C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-25-2001, 01:57 PM   #1
edk
Registered User
 
Join Date: Sep 2001
Posts: 20
Semaphores

OK, after my last thread on this subject failed miserably, I am trying again.

How do I go about creating and unlocking a set of 5 System V semaphores? Once this is done, how do I wait for them, and signal when I'm finished using it?

I'm trying to fork 5 children and execute their functions in a user-specified order. There are 5 children, so there should be a set of 5 semaphores. The user enters the order of execution (integer between 1 and 5, inclusive) using the keyboard. I fork the 5 children all at once, but I need to synchronize their execution with semaphores. How do I do this?

Also, please post code examples, not links to other pages. I have tried many other pages but they confuse me more than they help.

Thanks in advance.
__________________
liberate tutamet ex inferis
edk is offline   Reply With Quote
Old 11-25-2001, 03:55 PM   #2
edk
Registered User
 
Join Date: Sep 2001
Posts: 20
Here's the code segment for what i am doing:

Code:
	for (int i(0); i < theOrder.size(); i++) {
		pid = fork();
		if (pid < 0) {
			cerr<<"ERROR:  fork error\n";
			exit(0);
		}
		else if (pid == 0) {
			// wait for semaphore here (code not written yet
			doChildStuff(i);
			// signal semaphore here (code not written yet

			exit(0);
		}
		childPid[children] = pid;
		children++;
	}
...and here's my creation of the semaphore set:
Code:
	sem_key = 0x2222;
	if ((sem_id = semget(sem_key, theOrder.size(), 0666 | IPC_CREAT)) == -1) {
		cerr<<"ERROR:  semget error\n";
	}
	
	arg.val = 1;
	for (int i(0); i < theOrder.size(); i++) {
		if (semctl(sem_id, i, SETVAL, arg) == -1) {
			cerr<<"semctl error\n";
		}
	}
you can see, all o the semaphores are initialized to 1. now that you know this, how do i make it so that the first semaphore is signalled? also, how do i wait for a semaphore?
__________________
liberate tutamet ex inferis
edk is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Semaphores, need advice on implementation. Swerve C++ Programming 2 01-13-2009 01:54 AM
semaphores Dr Spud C Programming 7 09-22-2007 12:45 PM
Semaphores Problems mhelal Linux Programming 2 05-06-2007 10:36 PM
Semaphores Jules C Programming 4 01-18-2004 01:58 PM
semaphores theLukerBoy C Programming 0 11-05-2002 01:46 AM


All times are GMT -6. The time now is 11:12 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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