Search

Sponsored Links

Meta

Categories

Archives

Recent Posts

RSS Feeds

18
Apr

What is the main difference between C under DOS and C under UNIX?

Related Blog Items

There is no difference but UNIX is a multitasking, multi-user OS unlike DOS. In DOS, serial multi-tasking can be achieved but it makes the computer work less efficiently. If, for example, there was some calculation being done and we called in Sidekick, then all work on the calculations would stop as the computer responded to Sidekick. Wouldn’t it then be far better to give Sidekick only a part of the computer’s time? So that even while we were in Sidekick the calculations would carry on being performed in the background.

Under UNIX, all we have to do is to compile and link the background while we carry on doing whatever else we want in the foreground.

Background Process:

main()
{
  long l;
  for(i=0;i<=4000000;i++);
    printf("l is %d \n",i);
}

1)Compile it at UNIX prompt by saying CC<program name>.

2)The file generated will be a.out.

3)Execute it through the command a.out &. The & will make it a background process which is terminated only if process ends, or is interrupted with a DEL.

The UNIX prompt will come back on screen immediately. Load the editor again and wait. After some time a l is 4000000 will be displayed at the top left corner of the screen.

Isn’t this more convenient than DOS’s single tasking environment?

UNIX is able to switch between processes because each process has an identification number. This identification number identifies the variables and REGISTER values that are associated with this process. These variables are in a state of limbo when the process is suspended. And get activated again once the microprocessor resumes execution of the process. 

Process Identification:

/*There is a UNIX function getpid()- that enables us to get the identification number of a process*/

main()
{
  int pid;
  pid=getpid();
  print("Process ID is %d\n",pid);
}

The pid value will depend on the number of processes already running. It will always be unique.

Parent And Child:

A process in UNIX is not a stand-alone. This results in a parent-child relationship existing between processes.

main()
{
  int ppid;
  ppid=getppid();
  print("Parent Process ID is %d\n",ppid);
}

This program will give the ID number of its parent. So who is this parent?

When we boot the system, a special process called the SWAPPER or SCHEDULER is created with a PID of 0. The SWAPPER manages memory allocation for processes and influences CPU allocation. The SWAPPER in turn creates three children:the process dispatcher, vhand and bdflush with ID numbers 1,2,3 respectively.

This is done by executing the file init which exists in the etc sub-directory. The dispatcher now gives birth to the shell. From now on all processes initiated by us are children of the shell.

The fork():

Processes initiated by us can also create children in the same manner as the swapper and the process dispatcher did. These children processes are created using the fork() function. It is by forking processes that we can exploit the multitasking capability of UNIX.

main()
{
  fork();
  printf("Hello World\n");
}

Can you guess what the output of this program will be?

The statement Hello World will be displayed twice on screen. The fork() creates a child that is a duplicate of the parent process. The parent process in this case being the program listed above. Since now there are two identical processes in memory, the Hello World is printed twice.

The moment a call is made to the fork(), a child process is created. The child copy, too, gets a copy of the variable pid, but with a value of 0 by default. The parent process, is returned a value greater than 0 by the fork().

For UNIX is more than just a multi-tasking OS. It also has the facility to enable many users to access the machine it is housed in at one time. And not just access the machine at same time but also interact with each other.

But how UNIX is able to take care of different users simultaneously?

The kernel creates various processes that ultimately allows us to login. The first one is INIT process. INIT decides which of the terminals that can be used to login. The program GETTY, tells UNIX which program to execute. GETTY in turn reads data from the etc/gettydefs file, displays the login: prompt on screen and waits for input. After which it spawns the login program, which takes the login name we have keyed in as a parameter. Then INIT processes creates a child process by forking, through which login processes is executed with the help of exec() fuction.

Popularity: 5%

You need to log on to convert this article into PDF


Related Blog Items

1 Comment

  • dasari.sreenivasarao  said:

    Hi,

    Multi tasking is nicely explained by using fork(). Other Than this, was there any use of this fork(). Can you give more about data and text segment sharing between parent and child.

    Regards,


Leave a comment

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image