Visit Our UNIX and Linux User Community


avoid semphore lock


 
Thread Tools Search this Thread
Operating Systems HP-UX avoid semphore lock
# 1  
Old 02-04-2008
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:
Code:
int sem_wait_V(int id, struct sembuf  *sem_pv)
{

  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_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_pv[1] )
{
  int sem_value ,sem_value1; 


  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;

}


Last edited by Frank2004; 02-04-2008 at 12:06 AM..
# 2  
Old 03-11-2009
hope response, thanks.

Previous Thread | Next Thread
Test Your Knowledge in Computers #27
Difficulty: Medium
Linux was the first fully supported 64-bit operating system for consumers.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to avoid the spaces?

Hi I have a script which runs the isql command and takes the output in a xls file. Is there a way to trim the spaces(leading and trailing) from all the values in the column of the xls sheet? (6 Replies)
Discussion started by: Sharma331
6 Replies

2. UNIX for Advanced & Expert Users

Testing privileges -lock lockfile /var/lock/subsys/..- Permission denied

Hi all, I have to test some user priviliges. The goal is to be sure that an unauthorized user can't restart some modules (ssh, mysql etc...). I'm trying to automate it with a shell script but in same cases I got the syslog broadcast message. Is there any way to simply get a return code... (3 Replies)
Discussion started by: Dedalus
3 Replies

3. Shell Programming and Scripting

How to avoid new line

Hi, I am having multiple echo statements to print output echo "$VAr" echo "$lenght" echo "$sum" I get the output as 12 24 36 I need the output to be 12 24 36. How do i arrive at this. Thanks in advance (4 Replies)
Discussion started by: krashraj
4 Replies

4. Red Hat

Security Question: Lock after invalid login, Session Lock and Required Minimum Password Length

Hello all, If anyone has time, I have a few questions: How do I do the following in Linux. We are using Red Hat and Oracle Enterprise Linux, which is based on Red Hat too. 1. How to lock the account after a few (like 3) invalid password attempts? 2. How do you lock a screen after 30... (1 Reply)
Discussion started by: nstarz
1 Replies

5. Solaris

Any way to avoid these errors?

Hello, this is solaris 10 on SF v440, I know below errors might go away if I put a new PS or put the original one back in socket but is there any other way to stop these errors from appearing in /var/adm/messages ? I do not want to put the failed PS1 in socket neither do we want to spend on... (5 Replies)
Discussion started by: upengan78
5 Replies

6. Shell Programming and Scripting

How to avoid the space

Hi, I have some problem with following command. $path='pwd'.”/pack”; When I check the value in the variable path , it give the following output /tmp/new /pack The pwd will be /tmp/new Now the problem is the spaces is added between pwd and the appended path. I need the output like... (5 Replies)
Discussion started by: kalpeer
5 Replies

7. UNIX for Advanced & Expert Users

How to avoid polling???

Hi all, I have a directory where some process is keeping incremental/new log files. I need to code a program which will periodically poll this directory and if it founds a new file arrived then copy that new file to some other specific directory. We are OK with this polling approach. I just... (3 Replies)
Discussion started by: zing_foru
3 Replies

8. HP-UX

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? #define BIGCOUNT 888 // initial value of process counter #define SEM_POST_LIMIT 3 ... (0 Replies)
Discussion started by: Frank2004
0 Replies

9. UNIX for Dummies Questions & Answers

how to lock keyboard without using lock command

how can I lock my keyboard while I'm away from the computer without using lock command. What other commands gives me the option to lock keyboard device? thanks (7 Replies)
Discussion started by: dianayun
7 Replies

Featured Tech Videos