​
Showing posts with label process. Show all posts
Showing posts with label process. Show all posts

Monday, 15 May 2017

create child process by fork in linux using C

create child process by fork in linux using C


The tutorial is about to create child process by fork() in linux using C.
fork() which is the library funtion, is used to create child process.I will explain fork() using C.





Now the question is what happens when the child process is created?
When we use fork(), this function splits the running process into two processes, in which one is the parent and other is the child process.

Lets understand this with an example
    this is the C code
  • #include<stdio.h>
    #include<unistd.h>  // for fork()
    void main()
    {  printf("Before calling fork()");
      fork();
      printf("After calling fork()");
    }
  • Output:
    Before calling fork()
    After calling fork()
    After calling fork()

Here, we can see that as the fork() is called the program splitted into
two parallel processes which work simultaneously.


Lets apply our newly learned concept on next level
  • #include<stdio.h>
    #include<unistd.h>
    void main()
    {
      int PID;
      PID=fork();
      if(PID==0)
      {
        //child process
        printf("The pdf files are copying on the Desktop");
      }
      else if(PID>0)
      {
        //parent process
        execl("cp" ,"*.txt" ,"/Desktop" ,NULL);
      }
      else
      printf("Child process isnt created");
      }

NOTE
fork() returns 0 if child process is created
fork() returns PID or process ID of the child process to the parent process.
fork() returns Negative value if child process creation fails.


In above program, child process is displaying the phrase "The pdf files are copying on the Desktop"
and parent process is copying the pdf files.
You can choose the directory according to your system configuration.

Hence guys, this is all about to create child process by fork in linux using C

{ Read More }


Create timer for non windows application in C in which long process is run

Create timer for non windows application in C in which long process is run


There are times when we need to create a timer for our codes, this can be done by following some simple structure like the following:

using System.Threading;

public class WebCrawler
{
private Timer mTimer;

public void Start()
{
mTimer = new Timer(OnTimerTicked, null, 0, 100);
}

public void Stop()
{
mTimer.Change(Timeout.Infinite, Timeout.Infinite);
}

private void OnTimerTicked(object state)
{
DoSomethingLong();
}
}


The problem is the operation implemented in OnTimerTicked may take more than the timer interval (which is 100 milliseconds in the above example) to run. This is not desired as it may lead to memory corruption. The simple way to work around this is to fire the timer only once at the start, which will then invoke OnTimerTicked callback function, at the end of the OnTimerTicked callback, the timer can be reinvoked by calling its change() method, until flag change which cancel the timers ticked operation. This is shown in the following code.

using System.Threading;

public class WebCrawler
{
private Timer mTimer;
private bool mIsWorking;

public void Start()
{
mIsWorking=true;
mTimer = new Timer(OnTimerTicked, null, 0, 100);
}

public void Stop()
{
mIsWorking = false;
mTimer.Change(Timeout.Infinite, Timeout.Infinite);
}

private void OnTimerTicked(object state)
{
if(mIsWorking)
{
DoSomethingLong();
mTimer.Change(100, Timeout.Infinite);
}
}
}

{ Read More }