You are not logged in.

#1 2013-03-26 01:27:15

leetow2006
Member
Registered: 2013-02-13
Posts: 21

Ask a question about semaphore in Linux

Look:

#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?

Offline

#2 2013-03-26 01:30:58

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,774

Re: Ask a question about semaphore in Linux

I am sorry, but I am obliged to ask:  Is this, perchance, a homework problem ?

Last edited by ewaller (2013-03-26 01:31:17)


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#3 2013-03-26 02:34:52

leetow2006
Member
Registered: 2013-02-13
Posts: 21

Re: Ask a question about semaphore in Linux

It is not homework ,I run it in CentOS

Offline

#4 2013-03-26 03:18:49

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,774

Re: Ask a question about semaphore in Linux

leetow2006 wrote:

It is not homework ,I run it in CentOS

Okay, er, I will take that at face value.  Why do you ask here?  I just looked back through your post history and you ask a lot of rather esoteric questions about programming.  Interesting, yes.  Useful to members of this forum, no.  A waste of time to people who respond to you because they want to give back to Arch, yes.

Am I missing something?  As a moderator, I am about to close this thread unless you can dissuade me.


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

Board footer

Powered by FluxBB