Ask a question about semaphore in Linux

Programming, for all ages and all languages.
Post Reply
leetow2003
Member
Member
Posts: 70
Joined: Fri Nov 19, 2010 6:54 pm

Ask a question about semaphore in Linux

Post by leetow2003 »

Look:

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <error.h>

int main()
{ 
   int semid,i;
   struct semid_ds semds;
   key_t semkey;
   if((semkey=ftok(".",1))<0)
   {
     perror("ftok error");
     exit(1);
   }

   semid=semget(semkey,1,IPC_CREAT |IPC_EXCL | 0660);
   if(semid==-1)
   {
      perror("semget error");
      exit(1);
   }
   if(-1==semctl(semid,0,IPC_RMID,0))
   {
      perror("semctl RMID error");
      exit(1); 
   }
   
   return 0;
}
I find when I execute it at first,it run well,but since then it always
display error "File Exists",but I use command:ipcs -s not find any semaphore.
When I correct semkey=ftok(".",1) to semkey=ftok("/root",1),it always
run well.Why?How to correct it?
Post Reply