I am trying to figure out how to get my program to do one of two things:


1. Create and remove a semaphore based on me entering a 'r' in the command line at argv[1]


2. If argv[1] is a 'n', then I want check to see if the semaphore exists first using the IPC_EXCL parameter and then display perror("semget: file exists")

Hope that makes sense...here is my code:

# include <stdio.h>
# include <sys/types.h>
# include <sys/ipc.h>
# include <sys/sem.h>
# include <unistd.h>
# include <stdlib.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]);
	char n = atoi(argv[1]);
	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);

for (j = 0; j < ns + 1; j++){
	sem_array[j] = atoi(argv[j+3]);
		if(atoi(argv[j+3]) == atoi(argv[argc-1]))

/* Create semaphore */

if	((sem_id = semget(ipc_key, ns, IPC_CREAT | IPC_EXCL | 0666)) == -1)
	perror ("semget: File exists");
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");
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");

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

if	(semctl(sem_id, 0, IPC_RMID, 0) == -1)
	perror ("semctl: IPC_RMID");


Thank you I guess I didnt use the (CODE) icon properly, sorry about that...

right now I am having what seems to me like a very simple problem:

using an if statement to check if argv[1] is equal to the character 'n'...but when I run the program, it completely skips that line of code...even though the command line has character 'n' in argv[1]....

You need to post most recent code. If you want to just check for a single character than if( argv[1][0] == 'n')

Thank you SIR :). You are a gentleman and a scholar!! So simple.