Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

Semaphore in UNIX – An Overview

By Exforsys | on October 16, 2006 |
Concepts

Semaphore in UNIX – An Overview

Insight about Semaphore: 

First let us begin our topic by giving an insight to what is actually a semaphore. A semaphore is nothing but a term used in UNIX for a variable which acts as a counter. So the next question that comes in mind is what for we need this variable. It’s so simple. The reason is explained below. For instance there may be times when two processes try to access the same file simultaneously. In this event we must control the access of the file when the other process is accessing. This is done by assigning value to semaphore.

The value of the semaphore is initialized by the first process when the file is in access by it. When the second process try to access the file it checks the value of the semaphore and if it finds the value as initialized it does not access the file. After the first process is completed it reinitializes the semaphore value and now the second process uses it. The above example is for two processes but a semaphore can be used even when numbers of processes try to access the same file. Thus semaphores are used to coordinate access to a resource by different processes.

Where a Semaphore gets stored

We have seen that semaphore can be used when numbers of processes try to access the same file. In this case we must make the semaphore available accessible to all processes so that they can read and check the value and also initialize and reinitialize the value of semaphore appropriately. For this reason only the semaphore is stored in kernel so that it can be accessed by all processes.

Details of Semaphore

The command

$ ipcs –s

will give the list of existing semaphores.

Function to create a simple semaphore

The function that can be used to create semaphores is semget( ). This function takes 3 arguments as input namely:

Name of the semaphore with which we are going to create the semaphore. This is technically called as key of the semaphore.

The number of sub semaphores to be created. The minimum required number is 1.

The semaphore mode is specified. There are 2 modes in semaphore namely read and rewrite.

The return integer value from this function indicates the semaphore id with which the semaphore is associated and if the return integer is -1 it indicates that an error has occurred in the creation of semaphore.

How Kernel maintains internally the semaphores:

For every semaphore created a structure is created inside by the kernel byname semid_ds and this structure is found in header file sys/ipc.h.

How to set user defined values for semaphore:

In the create semaphore function we saw that the semaphore created returns a semaphore id which is system generated. By we could realize in real usage scenarios it would prove helpful only when the user would be able to define the value of the semaphore. This is because if the user would be able to define the value of the semaphore then they could design easily stating the numbers they have given for semaphores .For instance number one may be used to mark that semaphore is free and say number two to mark the semaphore is in use by another process and so on. This can be done by using the following functions and values namely:

semctl( )
GETPID
SETVAL
along with semget( ) which we used before for creating semaphores.

Let us see in brief how to do this:

  • The first step is to create semaphore as we discussed before by using semget( ) function which returns the semaphore id.
  • Before seeing how to do this first let us see what the semctl( ) function takes as arguments. It takes 4 parameters as arguments namely:
  • The semaphore id created by semget( ) function
  • Sub semaphore id. For the first time it would be zero since it gets created that time.
  • GETPID. This returns the Process id of the process.
  • value to be placed for the above process id. This value only will be returned by the function semctl( ).
  • After this by using SETVAL in semctl( ) function with all the arguments the same except instead of GETPID we place SETVAL and give the value to be placed in this which is returned by the semctl( ) function.

This is how we assign user defined values to semaphores which give ease of maintenance and use of semaphores by users.

Aspects of Semaphore:

  • Semaphores are identified by semaphore id which is unique for each semaphore.
  • The semaphores can be deleted by suing the function semdelete(semaphore)
  • The semaphore value can be incremented or decremented by using functions wait (sem) and signal (sem) respectively. wait (sem) decrements the semaphore value and if in the process of decrementing the value of semaphore reaches negative value then the process is suspended and placed in queue for waiting. signal (sem) increments the value of the semaphore and it is opposite in action to wait (sem).In other words it causes the first process in queue to get executed.

The value of semaphore represents thus the number of threads which are nothing but processes. In other words we found that if the value is positive then we have threads to decrement and proceed for execution without suspending. If the value of semaphore is negative then it represents that the number of threads or process is blocked and kept in suspended state. If the value of semaphore is zero then it means that there are no threads or processes in waiting state.

So from the above the important fact in semaphores is when we create semaphores we found that semaphores can be assigned with any value. Also we found from above that after creation of semaphores we can modify that is either increment or decrement the value of semaphores. But at any situation we cannot read the current value of semaphore.

« « The Role and Responsibilities of an Actuary
Important Terminologies used in Database » »

Author Description

Avatar

Editorial Team at Exforsys is a team of IT Consulting and Training team led by Chandra Vennapoosa.

Free Training

RSSSubscribe 401 Followers
  • Popular
  • Recent
  • Features of OOP

    October 22, 2006 - 0 Comment
  • Multiple Virtual Storage (MVS)

    October 22, 2006 - 0 Comment
  • Structured Query Language (SQL) – Basic Concepts

    November 8, 2006 - 0 Comment
  • Quality Aspects to Check While Writing COBOL Program

    October 23, 2006 - 0 Comment
  • Debugging – An Important Aspect in Programming

    August 31, 2006 - 0 Comment
  • Pros and cons of client/server computing

    October 25, 2006 - 0 Comment
  • Design Documents in Programming Methodology

    September 17, 2006 - 0 Comment
  • Feasibility Study – Why needed before programming

    September 17, 2006 - 0 Comment
  • Types and Levels of Testing in Programming

    October 2, 2006 - 0 Comment
  • Table space – Important concept of Database

    October 12, 2006 - 0 Comment
  • Structured Query Language (SQL) – Basic Concepts

    November 8, 2006 - 0 Comment
  • Pros and cons of client/server computing

    October 25, 2006 - 0 Comment
  • Quality Aspects to Check While Writing COBOL Program

    October 23, 2006 - 0 Comment
  • Multiple Virtual Storage (MVS)

    October 22, 2006 - 0 Comment
  • Features of OOP

    October 22, 2006 - 0 Comment
  • ISPF – Know about it

    October 17, 2006 - 0 Comment
  • Important Terminologies used in Database

    October 16, 2006 - 0 Comment
  • Software Development Life Cycle

    October 12, 2006 - 0 Comment
  • Table space – Important concept of Database

    October 12, 2006 - 0 Comment
  • Types and Levels of Testing in Programming

    October 2, 2006 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • Structured Query Language (SQL) – Basic Concepts
  • Pros and cons of client/server computing
  • Quality Aspects to Check While Writing COBOL Program
  • Multiple Virtual Storage (MVS)
  • Features of OOP

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2022. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.Accept Reject Read More
Privacy & Cookies Policy
Necessary Always Enabled