ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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
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...
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);
}
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.