System V semphore


 
Thread Tools Search this Thread
Operating Systems HP-UX System V semphore
# 1  
Old 07-23-2008
System V semphore

Hello,there
here is my code about semphore, when calling it in the program,sometimes result in the deadlock of semphore, but I can not find it, anyone can point some bugs and give some suggestion?
Code:
#define  BIGCOUNT  	888      // initial value of process counter 
#define  SEM_POST_LIMIT 3	


int sem_create_V(char *semfilepath, int seminterval , char semprojectid) 
{
  key_t 	key ;
  int 		semid ;
  int 		fileId;
  FILE          *fp;


  if ((fp = fopen (semfilepath,"wb")) == NULL )
    {
       perror("fopen");
       return -1;
      }
   fclose (fp);
 
  if ( (key = ftok(semfilepath , semprojectid)) < 0 )
       {
         perror("ftok");
         return -1;
       }

  if ( (semid = Sem_Create(key,seminterval)) < 0 )
        return -1;

  return semid ;
}




int Sem_Create(key_t key,int intval)
{

 int id,semval;
 union semnum{
      int val;
      struct semid_ds *buf;
      ushort *array;
    } semctl_arg;

  op_lock[0].sem_num = 2;
  op_lock[0].sem_op = 0;
  op_lock[0].sem_flg = 0;
  op_lock[1].sem_num = 2;
  op_lock[1].sem_op = 1;
  op_lock[1].sem_flg = 0;

  op_endcreate[0].sem_num = 1;
  op_endcreate[0].sem_op = -1;
  op_endcreate[0].sem_flg = 0;

  op_endcreate[1].sem_num = 2;
  op_endcreate[1].sem_op = -1;
  op_endcreate[1].sem_flg = 0;


  if (key== IPC_PRIVATE)
        return -1;
  
   else
     if (key == (key_t)-1)
        return -1;

     if ((id=semget(key,3,0666|IPC_CREAT))<0)
    // if ((id=semget(key,3,IPC_CREAT))<0)
       {
         perror("semget create");
         return -1;
       }


 again:;
     if (semop(id,&op_lock[0],2)<0) { 
         if (errno== EINVAL)
           goto again;
        perror("semop create");
        return -1; 
     }



   // get the value of the process counter,if it equal 0,then no one has initialize the semaphore
 
   if ((semval =semctl(id,1,GETVAL,0)) <0)
     {
        perror("semctl create GETVAL");
        return -1; 
     }

   if (semval == 0) {
     semctl_arg.val = intval;
     if ( semctl(id,0,SETVAL,semctl_arg) < 0) 
      {
        perror("semctl create SETVAL");
        return -1; 
      }
     semctl_arg.val = BIGCOUNT;
     
     if (semctl(id,1,SETVAL,semctl_arg) < 0)
      {
        perror("semctl create SETVAL");
        return -1;
      }
    }
  
    if (semop(id,&op_endcreate[0],2) < 0)
      {
        perror("semop create");
        return -1; 
      }
    return id;
}


int sem_wait_V(int id, struct sembuf  *sem_pv)
{


  sem_pv = new struct sembuf [1] ;

  if (sem_op(id,-1, sem_pv)<0)
    {
     // perror("sem_op waiting");
//     printf("sem_wait_V: %d,%d !\n",id,errno) ;
     delete sem_pv ;
     return -1;
    }

  delete sem_pv ;
 


  return 0;
}






int sem_post_V(int id, int sem_post_value_V, struct sembuf  *sem_pv)
{

 if ((sem_post_value_V = semctl(id,0,GETVAL,0)) <0)
	{
       	return -1; 
     	}
 if( sem_post_value_V > SEM_POST_LIMIT )
	{
	if( sem_post_value_V == SEM_POST_LIMIT + 1 )	return -1 ;
	return -1 ;
	}

 sem_pv = new struct sembuf [1] ; 
 
 if (sem_op(id,1,sem_pv)<0)
    {
     delete sem_pv ;
     return -1;
    }

 delete sem_pv ;

 return 0;
}

int sem_op(int id,int value, struct sembuf sem_pv1[1] )
{
  int sem_value ,sem_value1; 


   struct sembuf sem_pv[1];
  sem_pv[0].sem_num = 0 ;
     sem_pv[0].sem_flg = 0 ;

  if ((sem_pv[0].sem_op =value) == 0)
	{
	printf("sem_op error: operator is zero !!!!!!!(%d)\n",id) ;
        return -1;
	}

if ((sem_value = semctl(id,0,GETVAL,0)) <0)
     		{
        	perror("semctl create GETVAL");
                return -1;
     		}

 again:;
  if (semop(id,&sem_pv[0],1)<0)
        {
         sched_yield() ;
         if(errno==EINTR) 
          {
                	if ((sem_value1 = semctl(id,0,GETVAL,0)) <0)
     	                	{
        	                   perror("semctl create GETVAL");
                                   return -1;
     		                }
                           else
                             {
                                 if (sem_value != sem_value1 )
                                     return -1;
                                  else
                                     goto again;
                            }
           }
         return -1;
        }    

  return  1;

}

Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Migrating jobs from COBOL Mainframe system to UNIX system

In a nutshell requirement is to migrate the system from mainframe environment to UNIX environment (MF cobol would be used I guess). I have not much of idea in this field. I need to do some investigation on following points - - Ease of conversion - Known Data compatibility issue - Issue in... (9 Replies)
Discussion started by: Tjsureboy4me
9 Replies

2. AIX

Cloning a system via mksysb backup from one system and restore to new system

Hello All, I am trying to clone an entire AIX virtual machine to a new virtual machine including all partitions and OS.Can anyone help me on the procedure to follow? I am not really sure on how it can be done.Thanks in advance. Please use CODE tags for sample input, sample output, and for code... (4 Replies)
Discussion started by: gull05
4 Replies

3. HP-UX

how to mount a file system from a solaris server into an hp-ux system

Hi all I wonder if its possible to mount on a hp-ux server a file system that was previously mounted on a solaris 10 server. The LUN is on NetApp stoarge. The problem on hp-ux I cannot do pvcreate on the lun (disk) because contains data. Any help will be appreciated FR (2 Replies)
Discussion started by: fretagi
2 Replies

4. Solaris

System hangs (freezes) on system bell/beep

I am running OpenIndiana development version oi_148 32-bit on a seven-year-old Dell Inspiron 8600. Seems to be running fine except for one particular annoyance: It freezes whenever a system bell/beep plays. I have mitigated this by turning the system bell off in gnome-terminal, which I use... (3 Replies)
Discussion started by: DeadBadger
3 Replies

5. UNIX for Advanced & Expert Users

how to make a full system backup excluding data and restoring it to a new system

Hi, In order to have a sand box machine that I could use to test some system changes before going to production state, I'd like to duplicate a working system to a virtual one. Ideally, I'd like to manage to do it this way : - Make a full system backup excluding the user file system (this... (7 Replies)
Discussion started by: pagaille
7 Replies

6. SCO

file system not getting mounted in read write mode after system power failure

After System power get failed File system is not getting mounted in read- write mode (1 Reply)
Discussion started by: gtkpmbpl
1 Replies

7. HP-UX

avoid semphore lock

we developed a set of system V semphore interface for our application, in general, all of them work normal, seldom cause the deadlock. Here are some important sem_wait and sem_post interface, pls point some suggestion to fixed the deadlock problem: int sem_wait_V(int id, struct sembuf *sem_pv)... (1 Reply)
Discussion started by: Frank2004
1 Replies

8. Solaris

rsh commands not getting executed from Solaris 10 System to AIX System

Hi Friends, I am trying to execute rsh commands from Solaris 10 system to AIX system. When I give; Solaris10# rsh <hostname> ls -l , it gives me an error rshd : 0826-826 The host name for your address is not known At the same time, Solaris10# rsh <hostname> ---- gives me remote shell of... (25 Replies)
Discussion started by: jumadhiya
25 Replies
Login or Register to Ask a Question