HOW TO CREATE LOCK FILE IN C

 

File locking provides a very simple yet incredibly useful mechanism for coordinating file accesses. Before I begin to lay out the details, let me fill you in on some file locking secrets:-

There are two types of locking mechanisms: mandatory and advisory.

– Mandatory systems will actually prevent read() and write() to file.

– There are two types of (advisory!) locks: read locks and write locks (also referred to as shared locks and exclusive locks, respectively.)

 

Here I am demonstrating READ-LOCK …

In  my  case , I want  to reduce CPU usage which  takes very  high CPU usage % in case of accessing Sqlite3  by  two  application simultaneously.

To solve  this problem  I  applied  lock  file  concept  to synchronizes  applications ,

I applied it for three different applications.

 

Steps followed while creating lock file:-

 

– Finalize  File Name (xyz.lock)

– Finalize  lock file  location

– Before running  application , check lock file is  present or not

if(getlock(lockfile)== 1) :: Lock file is not present

– If lock file is not present  then create  lock file

int lockstatus = setlock(lockfile);

– After  completion  of job  delete lock file

 

Removelock (lockfile)

– If  lock file is  present  then sleep  for  sometime then again check for lock file

– While creating lock file, handle unexpected application closing mechanism to avoid trouble when next time application needs to be run.

 

Note: Extension of lock file is (abc.lock)

Note: Remove lock file when application is forcefully terminating (ctrl + C)

 

(Example of lock file in c using semaphores : –  )

Reference: http://www.c-program-example.com/2012/04/c-program-to-lock-file-using-semaphores.html

JSON – Brief Introduction

JSON stands for JavaScript Object Notation.it is  lightweight .It gives us text-based open standard designed for human-readable  collection of data that we can access in a really logical manner.

It is used primarily to transmit data between a server and web application, as an alternative to XML.

Although originally derived from the JavaScript scripting language, JSON is a language-independent data format, and code for parsing and generating JSON data is readily available in a large variety of programming languages.

 

JSON is built on two structures:

A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.

An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

example:

{“children”:[{“name”:”1″,”size”:0},{“name”:”2″,”size”:0},{“name”:”3″,”size”:0.4},{“name”:”4″,”size”:0.5882},{“name”:”5″,”size”:19.4}]}

Steps to create json in java.

Jar files required for json

1.commons-beanutils-1.8.1

2.commons-collections-3.2.1

3.commons-lang-2.5

4.commons-logging-1.1.1

5.ezmorph-1.0.6

6.java-json

7.json-rpc-1.0

Put all the above jar files in WEB-INF/lib folder

create a class to create convert data into json

package com.helical.efw.json;

import java.util.List;

import java.util.Map;

import net.sf.json.JSONObject;

public class ConvertJson {

 

public void CommonJson(Map<String, List<Buble>> commonList)

{

String jsonString;

//Suppose in commonList data comming in this format

//{children=[{name=1, size=0.0000}, {name=2, size=0.0000},

//{name=3, size=0.4000}, {name=4, size=0.5882}, {name=5,

//size=19.4000}]}

JSONObject json = new JSONObject();

json.accumulateAll(commonList);

jsonString=json.toString();

System.out.println(json.toString());

}

}

 

 

Public class JsonProducer{

public static void main(String[] args) throws IOException {

ConvertJson cjson=new ConvertJson();

cjson.CommonJson();

}

JSONObject is a class in which there are so many methods available.

like:

JSONObject.accumulateAll(Map) :-It Accumulate values under a key

JSONObject.getJSONObject(java.lang.String key ):-we can get  get the JSONObject value associated with a key.