LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-12-2010, 04:58 PM   #1
mashhype
LQ Newbie
 
Registered: Sep 2010
Distribution: Ubuntu Linux 10.04 Lucid Lynx
Posts: 11

Rep: Reputation: 0
creating N semaphores


Hi-
I have a project in my operating systems class that requires me to write a program that creates N semaphores. The arguments entered into the command line are: filename ropt NS value1 value2 ... valueNS.
Argv0 is the filename
Argv1 is the option to remove 'r' the semaphore set (this can also be a 'n' for not remove).
Argv2 is the number of semaphores to be created
Argv3 and beyond are the values that the semaphores are initialized to

I am trying to figure out how I can take a variable number of values (value1 value2 valueNS) without knowing how many values will be entered into the command line and creating the set of semaphores with an unknown number of values...


Should I take everything entered onto the command line and write it to a text file first? And then take the values from there and put hem in an array?

Please help I am new to C...thank you.
 
Old 11-14-2010, 09:01 PM   #2
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by mashhype View Post
I am trying to figure out how I can take a variable number of values (value1 value2 valueNS) without knowing how many values will be entered into the command line
Check the following out:
 
Old 11-15-2010, 04:03 AM   #3
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 475Reputation: 475Reputation: 475Reputation: 475Reputation: 475
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 11-15-2010, 05:26 AM   #4
MrCode
Member
 
Registered: Aug 2009
Location: Oregon, USA
Distribution: Arch
Posts: 864
Blog Entries: 31

Rep: Reputation: 148Reputation: 148
I'm a bit of a novice to C as well, but I hope this helps:

Are you just wondering how to check if there are too many/too few arguments? The way I would do that is to get the argument provided as the number of semaphores (atoi(argv[3]) in your case; argv[0] is the program name) and compare that with the number of arguments - 3 (leaving just the semaphore values), and if it's too high/low, bail out.

Then it should just be a matter of allocating the proper amount of memory for your values (assuming the user didn't provide too many/few args), and assigning that to an array.

If this is completely not what you had in mind, then I apologize...if it helped, then that's great!

Should I provide code? I have a working example, but I'm not sure if I want to post it, lest it be considered "doing your homework for you" by the mods...

Last edited by MrCode; 11-15-2010 at 05:46 AM.
 
Old 11-16-2010, 11:04 AM   #5
mashhype
LQ Newbie
 
Registered: Sep 2010
Distribution: Ubuntu Linux 10.04 Lucid Lynx
Posts: 11

Original Poster
Rep: Reputation: 0
Hi everyone, I got the project done. It was not as hard as I thought. Thanks for all the suggestions.
 
Old 11-16-2010, 06:02 PM   #6
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
It would be helpful for others if you could post here the solution code too and also Mark the thread SOLVED.
 
Old 11-17-2010, 12:27 AM   #7
mashhype
LQ Newbie
 
Registered: Sep 2010
Distribution: Ubuntu Linux 10.04 Lucid Lynx
Posts: 11

Original Poster
Rep: Reputation: 0
Solution to "Creating N Semaphores

Hi everyone,

I hope this helps someone else who was in the same boat I was. I am also new to programming but gaining more confidence with each assignment and project .

Code:
# include <stdio.h>
# include <sys/types.h>
# include <sys/ipc.h>
# include <sys/sem.h>
# include <unistd.h>
# include <stdlib.h>
# include <string.h>

union semun 
{
	int val; 
	struct semid_ds *buf; 
	ushort *array;
};

int main(int argc, char *argv[])
{
	int sem_id, sem_value, i; 
	int ns = atoi(argv[2]);
	int j;
	key_t ipc_key; 
	struct semid_ds sem_buf;
	ushort sem_array[ns]; 
	union semun arg;
	ipc_key = ftok(".", 'S');

if ((argc - 3) != ns) {
	printf("Arguments not matched.  Number of semaphores is %d\n", ns);
	exit(1);
}

for (j = 0; j < ns + 1; j++){
	sem_array[j] = atoi(argv[j+3]);
		if(atoi(argv[j+3]) == atoi(argv[argc-1]))
			break;
}
/* Create semaphore */
if (argv[1][0] == 'n'){
	if ((sem_id = semget(ipc_key, ns, IPC_CREAT| IPC_EXCL | 0666)) == -1) 
	{
	perror ("semget: IPC | 0666");
	exit(1);
	}
}
else if	((sem_id = semget(ipc_key, ns, IPC_CREAT | 0666)) == -1){
	{
	perror ("semget: IPC | 0666");
	exit(1);
	}
}
printf ("Semaphore identifier %d\n", sem_id);

/* Set arg (the union) to the address of the storage location for */
/* returned semid_ds value */

arg.buf = &sem_buf;
if (semctl(sem_id, 0, IPC_STAT, arg) == -1) {
	perror ("semctl: IPC_STAT");
	exit(2);
}
printf ("Create %s", ctime(&sem_buf.sem_ctime));

/* Set arg (the union) to the address of the initializing vector */
arg.array = sem_array;

if (semctl(sem_id, 0, SETALL, arg) == -1) 
{
	perror("semctl: SETALL");
	exit(3);
}

for (i=0; i<ns; ++i) 
{
	if ((sem_value = semctl(sem_id, i, GETVAL, 0)) == -1) 
{
		perror("semctl : GETVAL");
		exit(4);
}
	printf ("Semaphore %d has value of %d\n",i, sem_value);
}
/* remove semaphore */


if (argv[1][0] == 'n')	
	exit(5);



else 
	if (semctl(sem_id, 0, IPC_RMID, 0) == -1)
	{
		perror ("semctl: IPC_RMID");
		exit(6);
	}

}
 
Old 11-17-2010, 12:43 AM   #8
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
It is nice to see that you posted the solution, now PLEASE "Mark the thread SOLVED". See the top of this page for the link.
 
Old 11-17-2010, 08:22 AM   #9
MrCode
Member
 
Registered: Aug 2009
Location: Oregon, USA
Distribution: Arch
Posts: 864
Blog Entries: 31

Rep: Reputation: 148Reputation: 148
Well, since the OP solved their problem, I suppose it wouldn't necessarily be bad of me to flaunt my own success, would it?

This is the code I was going to post to help the OP:

Code:
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>

int main(int argc,char** argv)
{
	int numOfSemaphores = atoi(argv[1]); //number of "semaphores" is integer value of first argument

	if((argc - 2) < numOfSemaphores || (argc - 2) > numOfSemaphores) //too many/few arguments?
	{
		printf("nums provided: %i\n",(argc - 2));
		printf("nums required: %i\n",numOfSemaphores);
		printf("Too many/too few arguments.\n");
		return 1; //fail
	}
	else
	{
		int* semaphores = malloc(numOfSemaphores * sizeof(int)); //allocating ints for holding "semaphore" values

		for(int i = 2; i < argc; i++)
		{
			semaphores[i - 2] = atoi(argv[i]); //copy values provided on cmd line
			printf("%i\n",semaphores[i - 2]); //print what we've got
		}
		
		free(semaphores);
	}

	return 0;
}
As you can see, it doesn't really *do* anything useful, but it does serve as an okay example for what the OP was trying to accomplish. (right?)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] C - Semaphores, initiate semaphores seems to be failing golmschenk Programming 3 06-28-2010 09:32 PM
semaphores mmcds Linux - Newbie 4 01-03-2008 08:16 AM
permission denied error creating POSIX semaphores Mish Linux - Newbie 2 09-28-2006 08:16 PM
creating semaphores for multiple processes djgerbavore Programming 6 04-09-2006 09:07 PM
Semaphores help mojozoox Programming 1 01-07-2004 08:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration