My prof gave me some code to learn from, unfortunately it is full of errors. Some of the errors involve creating semaphores, so I found some code online that only makes the semaphores. This way, I can discover the correct way to use them and fix the code my prof gave me.

So I've changed this code to make a set of 10 semaphores, which is what that part of the original code is supposed to do. However, I get an error. semget returns a negative value, and the error is "Invalid argument." Please help me fix this so I can make a set of 10 semaphores. This is on Linux, by the way. Thank you!

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

/* The semaphore key is an arbitrary long integer which serves as an
   external identifier by which the semaphore is known to any program
   that wishes to use it. */

#define KEY (1492)

void main()
{
   int id; /* Number by which the semaphore is known within a program */
   int nsems = 10; /*works for 1 but not for 10*/
   
   /* The next thing is an argument to the semctl() function. Semctl() 
      does various things to the semaphore depending on which arguments
      are passed. We will use it to make sure that the value of the 
      semaphore is initially 0. */

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

   argument.val = 0;

   /* Create the semaphore with external key KEY if it doesn't already 
      exists. Give permissions to the world. */
   /* Always check system returns. */

   if((id = semget(KEY, nsems, 0666 | IPC_CREAT)) < 0) //when nsems==10, we get an error here
   {
      perror("\nUnable to obtain semaphore"); //the error is "Invalid argument"
      exit(0);
   }

   /* What we actually get is an array of semaphores. The second 
      argument to semget() was the array dimension - in our case
      1. */

   /* Set the value of the number 0 semaphore in semaphore array
      # id to the value 0. */

   if( semctl(id, 0, SETVAL, argument) < 0)
   {
      fprintf( stderr, "Cannot set semaphore value.\n");
   }
   else
   {
      fprintf(stderr, "Semaphore %d initialized.\n", KEY);
   }
}
(In case you are wondering why I didn't want to post the original code, it's because it's a huge program with multiple files. )