0
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>

int main()
{
  key_t key, k, shmid;
  int ret, semid, i = 0;
  struct sembuf op[1];
  void *shmptr = (void *) 0;

  union semun {
    int val;
    struct senid_ds *buf;
    unsigned short *array;
  };

  union semun arg;

  /* Creating a shared memory */
  k = ftok(".", 1);

  if (-1 == k) {
    perror("\n ftok \n");
    return 1;
  }

  shmid = shmget(k, 1000, IPC_CREAT | 0744);

  if (-1 == shmid) {
    perror("\n Shared memory creation failed \n");
    return 2;
  }

  shmptr = shmat(shmid, (void *) 0, 0);

  if (NULL == shmptr) {
    perror("\n Process cant attach to shared memory segment! \n");
    return 3;
  }

  /* Creating semaphore */
  key = ftok(".", 4);

  if (-1 == key) {
    perror("\n ftok \n");
    return 4;
  }

#if 0
  semid = semget(key, 1, IPC_CREAT);
#else
  semid = semget(key, 1, 0);
#endif

  if (-1 == semid) {
    perror("\n Cannot create semaphore \n");
    return 5;
  }

#if 0
  arg.val = 1;
  ret = semctl(semid, 0, SETVAL, arg);
  printf("\n ret : %d \n", ret);
#endif

  op[0].sem_num = 0;
  op[0].sem_op = -1;
  op[0].sem_flg = SEM_UNDO;

  ret = semop(semid, op, 1);

  // printf("\n ret : %d semop : %d\n",ret,op[0].sem);
  printf("\n Process 1 locked sem1 \n");

  /* Do Operation on shared resource */
  while (i < 4) {
    strcpy((char*) shmptr, "BDC");
    printf("\n %s \n", (char *) shmptr);
    sleep(2);
    printf("\n After sleep \n");
    i++;
    shmptr++;
  }

  op[0].sem_op = 1;
  ret = semop(semid, op, 1);
  printf("\n Process A unlocked sem1 \n");

  return 0;
}

I tried an example program to lock the shared memory segment . I tried 2 instances of the same program. to see whether the semaphore is locking properly or not. But the above program always allows the other process to use the shared memory when the process1 has already locked. Couldnt identify where the mistake is.

3
  • What do you see that makes you conclude it isn't working? What have you done to try to find the bug? Commented Sep 23, 2013 at 18:25
  • It must be the Yoda Conditions in your code Commented Sep 23, 2013 at 18:56
  • @wallyk : the 2 processes are not synchronised to access the shared segment Commented Sep 23, 2013 at 19:19

1 Answer 1

1

You should enable the IPC_CREAT statement and bitwise-or into the 3rd argument the necessary but missing permissions (S_IRWXU, at least).

You should enable the SETVAL statement to initialize the semaphore to 1 but only execute that statement based on external input (i.e., the program is invoked with an argument that causes it to create the semaphore and initialize it); otherwise, the program should assume that the semaphore already exists.

You need to check the return values from the semop() calls.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Steve. I changed the key value also after changing the permissions while creating. It worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.