Shritam Bhowmick Explains Shell Injection v/s Remote Code Execution v/s Code Injection – Yes, they are Different!

Shell Injection v/s Remote Code Execution v/s Code Injection

By Shritam Bhowmick
Web Application Penetration Tester
LinkedIn: https://www.linkedin.com/profile/view?id=281014248&trk=nav_responsive_tab_profile
Academia: https://independent.academia.edu/ShritamBhowmick
Facebook: https://www.facebook.com/coded32

The Bare Beginning

I promised to deliver yet another quality content on my blog and as promised after this particular post, my true intentions were to go ahead with posting something deep on HTTP Parameter Pollution. Looking at the awareness level (read the comments for a deep approach insight on application security!) of incoming proclaimed security researchers or bug hunters, I recently decided to make my point across and let the info-sec community know the real concepts behind Application Injection vulnerabilities. This post is focused to switch off the ego gap and provide a platform for others to realize core concepts and the necessity of such concepts to be applicable to adaptive application penetration testing!

I quickly realized the core necessity of a distinguished vulnerability levels according to their categorization and be real technical about the application security issue, most focused primarily on this post on Injection level web vulnerabilities which are very common if dug enough into. On a recent conversation with many of ‘elite’ bug hunters, I noticed, there is a huge gap on the technical consciousness level they possess and likely end up only getting T-shirts for the sake of name on Hall of fame (No pun intended!). Money would encircle you girls, knowledge will encircle you girls, cars, super-bikes and alike!

Continue reading

Advertisements

Shritam Bhowmick Explains HTTP Parameter Contamination

HTTP Parameter Contamination

By Shritam Bhowmick
Web Application Penetration Tester
LinkedIn: https://www.linkedin.com/profile/view?id=281014248&trk=nav_responsive_tab_profile
Academia: https://independent.academia.edu/ShritamBhowmick
Facebook: https://www.facebook.com/coded32

Earlier on my previous post about Enterprise HTTP Security, I described how HTTP Security is a fine clockwork for an application penetration tester; this post would look into deeper aspect of HTTP Security and how logical manipulation of HTTP could be potentially used by an attacker to manifest the underlying application level vulnerabilities bypassing any security restrictions which were in place originally.

In terms of application development during the standard phases, multi-tier application architecture is prevalent. The multi-tier architecture is a client-server architecture, where the presentation, the application processing and the data management is a complete separate processes. In basic terms, the multi-tier architecture are convenient for developers. The reason they are convenient for developers is the fact that developers have to re-use code and develop applications in which the whole application framework needn’t have to be written all over again. They could only modify parts of the application architecture based on tiers and profit flexibility in the use of such applications. The unfortunate part is the handling of the same data over multiple platform can lead to security breach, or leave the application vulnerable. Logical errors are triggered this way and are completely different from Injection based attacks such as:

  • LDAP/Blind LDAP Injections
  • XML Injection
  • HTML Injection
  • SQL Injection
  • ORM based Injection
  • Spring Injection/nHibernate Injection
  • Xpath Injection
  • Command Injection

All of the above mentioned ‘injection’ variants fall into code level application vulnerabilities and is completely different from ‘logical’ vulnerabilities which still have a greater level of impact on the web applications. During my old research at early phases of dissection behavioral pattern of different platform based application on different web-architectures, I found these led to couple of logical based vulnerabilities which could be used by an attacker for benefits. This lead self-curiosity to further research and I came up concluding something which already existed called ‘HTTP Parameter Contamination or HPC’. During my research at Defencely, I found out this particular attack methodology does not only rely on a specific platform but is widely used across many other different web based platforms, such as PHP on Apache, ASP.NET on IIS, etc.

Continue reading

Enterprise HTTP Security Inspection for Realistic Application Security

The need for HTTP Security Inspection on Application Security

By Shritam Bhowmick
Web Application Penetration Tester
LinkedIn: https://www.linkedin.com/profile/view?id=281014248&trk=nav_responsive_tab_profile
Academia: https://independent.academia.edu/ShritamBhowmick
Facebook: https://www.facebook.com/coded32

Application Layer – HTTP from the Security Perspective

An Application Layer is the first layer which need a security check which just goes beyond any other common checks. Somehow, automated scanners might do this as pre-defined in the programmed logic, but most of them fail to find the bugs which passes through the HTTP Handler and hence create critical vulnerabilities for business enterprise. Yes, I had been talking about Hyper Text Transfer Layer Protocol, which is by now the most common, wildly wide used text based protocol around the internet. Web Applications use the text based protocol since it’s easier to implement and handle parallel requests. The Web Server handles these requests which are made by the clients and penetration testers often ignore a variety of checks against HTTP from the security perspective. Once, an application penetration is fairly grown studying HTTP at a deep level, he/she could already understand why a particular request could be manipulated by client side proxy in multiple of ways and produce a critical security bypass.

Before I jump into concluding the security aspects of the Hyper Text Transfer Protocol, my application security research have shown a comprehensive study of where to start from HTTP from the security standpoint. This question has been asked a lot of times and people have failed to come up with an exact method to detail everything in one book or post. This study is more of a guideline for application penetration testers rather than a reference study; but either way it could be used both the ways. I had prepared a standard draft for HTTP Security Research in topic segments which if wished could be sub-categorized but for the sake of the reference and a guide, I have detailed them into modular topics which could be used by any application security researcher, bug hunter, or application security enthusiast for their own analysis.

badlyart

Continue reading

Fine Tuning Adaptive Network Penetration Test – External, Internal and Wireless

Fine Tuning Automation for Network Penetration Test

By Shritam Bhowmick
Web Application Penetration Tester
LinkedIn: https://www.linkedin.com/profile/view?id=281014248&trk=nav_responsive_tab_profile
Academia: https://independent.academia.edu/ShritamBhowmick
Facebook: https://www.facebook.com/coded32

Network Penetration Testing

A lot has been discussed earlier related to network penetration test in forums, IRC’s and security conferences but everyone looked for some automated approach to keep network penetration test related task going fast. The fast approach is desired for mass IP scans and lot of IP ranges which have to be tested in a short time. Most of these network nodes have services open which could be further investigated if these services were well known to be exploited in the wild.

networktest

There are various Network Penetration testing which could be referenced below:

  1. External Network Penetration Testing
  2. Internal Network Penetration Testing
  3. Wireless Network Penetration Testing

Now as most of you had already assumed, there could be automated approach to all of them; this however seems easy but is harder if taken from a wide security view-point. The art of choosing a set tools at your disposal for Network Security Audit lies beyond the scope defined since lot of these tools send malicious packets which could deliver stress to the web-server or critical production server costing the clients financially off their services. As a penetration tester I have learned this art from my own lesson and experiences and this would be my own personal methodology for a Network penetration test. Some of the questions which should be asked before-hand to the client before beginning with an engagement would be the major feedback on how one should be preparing for the penetration test.

Continue reading

Adaptive Application Framework Driven Vulnerabilities and the Padding Oracle

Securing Web Applications before Deployment.

An analysis focused on various framework used to deploy web applications.

By Shritam Bhowmick
Web Application Penetration Tester
LinkedIn: https://www.linkedin.com/profile/view?id=281014248&trk=nav_responsive_tab_profile
Academia: https://independent.academia.edu/ShritamBhowmick
Facebook: https://www.facebook.com/coded32

Abstract

Dedicated vulnerability and bug researchers go deep into the application security aspects while studying application internals and there is a prominent rise in hidden attack vectors which are never common. There is a default common misconception among the developers that deploying applications which are vendor-enabled with 3rd party proprietary framework libraries will add security to the application. Libraries which the developers rely on are themselves vulnerable if properly dissected and studied. This brings business concerns to the business assets. The business assets could be anything from bank details to storing credit card information for customers to easily access such numbers for the ease of the customers. Although data integrity is maintained when storing and is encrypted, it takes a while for an attacker to get in and get out without being really noticed. Contrary to the statements above, there is yet another belief that Open source libraries will be safer since they go revisions by the mass community but the truth is bitter. Again, deep down in the open-source libraries, there exist multiple critical vulnerabilities which needs to be addressed before deploying them as they are. The information given below will detail the vulnerabilities which are deep inside the libraries which are used to deploy rich internet based applications.

open2 open

What Developers see as a convenient way for deploying a web application?

  • Languages used: PHP, JAVA, Ruby, SCALA, Perl, Python, HASKELL, Cold Fusion and more.
  • Framework Used:NET, Zend, CodeIgniter, Spring, Catalyst, Snap, CakePHP, Yii, Fusebox, and more. Even more popular ones are Django, Sinatra, Mason, Pyjamas, Symfony and Grails.

Continue reading

Web Security Threat Prediction

Web Security Threat Prediction

By Shritam Bhowmick
Web Application Penetration Tester
LinkedIn: https://www.linkedin.com/profile/view?id=281014248&trk=nav_responsive_tab_profile
Academia: https://independent.academia.edu/ShritamBhowmick
Facebook: https://www.facebook.com/coded32

Abstract

The Web Security scene has been much complex than ever known and its time various industry take a deeper look to it to gain an in-depth gravity of the situation which affects them directly or in-directly. This could come at a blow and wouldn’t let you know until it’s too late. This post will take you mind blown from the recent predictions in terms of Web Security and will let you inform on the latest web attacks in rise and how such attacks are bad for business as well as reputation let alone financial losses. When we talk about industry, this doesn’t have to be the retail industry; it aims at stretch from the medical appliances to the car manufacturing industry and too low down to the Electronic Cigarette industry. That been said, we will look how various industrial assets which have had their presence and continue to have a presence in the web world affects them directly or indirectly and why Web Security for them is an absolute important factor too big a risk to ignore and compromise with the same.

Prediction 2015

I have come across and defined a statistical background check on as many application attack vectors and evidently from the statistical approach have come up to a very conclusive set of industries which could go bankruptcy as well as reputation loss if Web Security part is ignored. Here we have thrown out some of the industries which have a direct impact on business ignoring Web Security at their end.

  • Medical Department
  • Web Retail Department and Business Assets
  • Opensource Platforms
  • Mobile Devices

1ta

Continue reading

Knowing the terminologies beyond being an Ignorant.

Terminologies Beyond being an Ignorant.

There is no software that can hack facebook passwords (except key logging and phishing) and this goes same with e-mail account hacking. And these are some of the queries and desires one could start off primitive constructional hacking from, but they often fall off the edge and go destructive blackhat hacking for the curiosity. Curiosity is such an agent which could change and push forward the envelope. But this, if not done the right way will only lead to false reality of what is really not constructional nor is the reality in itself. Every one else seem tired of the questions and yet there really seems to be confusion on what to admire, are the guys who work hard to be on the white side, or the guys who still work hard at the black side of breaking the security. Now when I say ‘security’, strictly it does not have to be application security or network security, think bigger and there is physical security, mobile security, personal security; a person who bypass security or any counter-measures which are in place to prohibit access is known to be generally called as a ‘cracker‘ or a ‘hacker‘.

But don’t get hooked by the terms. They are both very different. It depends on the person on which side he chose to be. That is if a person has chosen the darker side, he would be in a long run be called and termed as a cracker. If not, he belongs to the ‘hacker’ category. And the latter is the category we would be talking about since I have really no interest on the other side of the fence where relatively today or later, things keep getting worse with law enforcement going stricter. Now, a cowboy fro curiosity might just explore both the sides and this is the gray area. Such people would be called as a Gray Hat. Let’s walk straight to the points and see some terminologies which could be mentioned to illuminate some of the people who had been missing a lot of what, why, how and the where’s. Here are some terminologies related to computer science but are inclined on the side of ‘computer hacks’ on a broader scope.

Kernel is the main component of most computer operating systems; it is a bridge between applications and the actual data processing done at the hardware level. The kernel’s responsibilities include managing the system’s resources (the communication between hardware and software components). Usually as a basic component of an operating system, a kernel can provide the lowest-level abstraction layer for the resources (especially processors and I/O devices) that application software must control to perform its function. It typically makes these facilities available to application processes through inter-process communication mechanisms and system calls.

Linux is a computer operating system which is based on free and open source software. Although many different varieties of Linux exist, all are Unix-like and based on the Linux kernel, an operating system kernel. The Linux was originally a ‘kernel’ where lines of code were added by the community later to make it better and better and now, Linux has so many distributions with 1000’s of lines of code and utilities.

An exploit (from the verb to exploit, in the meaning of using something to one’s own advantage) is a piece of software, a chunk of data, or sequence of commands that takes advantage of a bug, glitch or vulnerability in order to cause unintended or unanticipated behavior to occur on computer software, hardware, or something electronic (usually computerized). This frequently includes such things as gaining control of a computer system.

A shell is a piece of software that provides an interface for users of an operating system which provides access to the services of a kernel. However, the term is also applied very loosely to applications and may include any software that is “built around” a particular component, such as web browsers and email clients that are “shells” for HTML rendering engines. The name shell originates from shells being an outer layer of interface between the user and the internals of the operating system (the kernel).

PHP is a general-purpose server-side scripting language originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document. It also has evolved to include a command-line interface capability and can be used in standalone graphical applications.

JQuery is a cross-browser JavaScript library designed to simplify the client-side scripting of HTML. It was released in January 2006 at BarCamp NYC by John Resig. Used by over 49% of the 10,000 most visited websites, jQuery is the most popular JavaScript library in use today.

A network host is a computer connected to a computer network. A network host may offer information resources, services, and applications to users or other nodes on the network. A network host is a network node that is assigned a network layer host address.

Algorithm: In mathematics and computer science an algorithm is an effective method expressed as a finite list of well-defined instructions for calculating a function Algorithms are used for calculation, data processing, and automated reasoning. In simple words an algorithm is a step-by-step procedure for calculations.

There are many websites that can be searched for vulnerabilities and can be hacked but if you are a real hacker then you should select the website and then try to hack it and this is termed as target hacking.

A router is a device that forwards data packets between computer networks, creating an overlay inter-network. A router is connected to two or more data lines from different networks. When data comes in on one of the lines, the router reads the address information in the packet to determine its ultimate destination. Then, using information in its routing table or routing policy, it directs the packet to the next network on its journey. Routers perform the “traffic directing” functions on the Internet.

A data packet is typically forwarded from one router to another through the networks that constitute the inter-network until it gets to its destination node.

In computer networks, a proxy server is a server (a computer system or an application) that acts as an intermediary for requests from clients seeking resources from other servers.

BB5 unlocking in Nokia phones is not possible to install unsigned OS in Nokia (not simlock).

The Metasploit Project is an open-source computer security project which provides information about security vulnerabilities and aids in penetration testing and IDS signature development. Its most well-known sub-project is the Metasploit Framework, a tool for developing and executing exploit code against a remote target machine. Other important sub-projects include the Opcode Database,shell code archive, and security research.

There is not a method to decrypt nokia MCUSW file and change it because if we do it then the check sum is changed than that of phone and its not installed. Symbian can be hacked by using ROM patcher and hello.

Free hosting websites don’t allow to use rapid leech script and other forums.

Unix (officially trademarked as UNIX, sometimes also written as Unix) is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna. The Unix operating system was first developed in assembly language.

A scripting language, script language, or extension language is a programming language that allows control of one or more applications. “Scripts” are distinct from the core code of the application, as they are usually written in a different language and are often created or at least modified by the end-user. Scripts are often interpreted from source code or bytecode

Cross-site scripting (XSS) is a type of computer security vulnerability typically found in Web applications that enables attackers to inject client-side script into Web pages viewed by other users. A cross-site scripting vulnerability may be used by attackers to bypass access controls such as the same origin policy. This is a Application (Web Application) Security vulnerability and could be classified into different types of Cross Site Scripting attacks such as persistent, non-persistent and DOM-based. There are contexts which are bypassed with using certain characters if not already black-listed or the application isn’t using white-list for allowing only certain legacy characters into the application input. Output encoding is one of the many methods to stop this kind of attack.

A denial-of-service attack (DoS attack) or distributed denial-of-service attack (DDoS attack) is an attempt to make a computer resource unavailable to its intended users. Although the means to carry out, motives for, and targets of a DoS attack may vary, it generally consists of the concerted efforts of a person, or multiple people to prevent an Internet site or service from functioning efficiently or at all, temporarily or indefinitely

A Media Access Control address (MAC address) is a unique identifier assigned to network interfaces for communications on the physical network segment. MAC addresses are used for numerous network technologies and most IEEE 802 network technologies including Ethernet. Logically, MAC addresses are used in the Media Access Control protocol sub-layer of the OSI reference model.

Social engineering is the art of manipulating people into performing actions or divulging confidential information. While similar to a confidence trick or simple fraud, the term typically applies to trickery or deception for the purpose of information gathering, fraud, or computer system access; in most cases the attacker never comes face-to-face with the victim.

Rooting is a process that allows users of mobile phones and other devices running the Android operating system to attain privileged control (known as “root access”) within Android’s Linux subsystem with the goal of overcoming limitations that carriers and manufacturers put on some devices. It is analogous to jailbreaking on devices running the Apple iOS operating system.

Tethering means sharing the Internet connection of an Internet-capable mobile phone with other devices. This sharing can be offered over a wireless LAN (Wi-Fi), or over Bluetooth, or by physical connection using a cable. In the case of tethering over wireless LAN, the feature may be branded as a mobile hotspot. The Internet-connected mobile phone acts as a portable router when providing tethering services to others.

Malware, short for malicious software, consists of programming (code, scripts, active content, and other software) designed to disrupt or deny operation, gather information that leads to loss of privacy or exploitation, gain unauthorized access to system resources, and other abusive behavior.The expression is a general term used by computer professionals to mean a variety of forms of hostile, intrusive, or annoying software or program code.

A honeypot is a trap set to detect, deflect, or in some manner counteract attempts at unauthorized use of information systems. Generally it consists of a computer, data, or a network site that appears to be part of a network, but is actually isolated and monitored, and which seems to contain information or a resource of value to attackers.

A cache is a component that transparently stores data so that future requests for that data can be served faster. The data that is stored within a cache might be values that have been computed earlier or duplicates of original values that are stored elsewhere. If requested data is contained in the cache (cache hit), this request can be served by simply reading the cache, which is comparatively faster. Otherwise (cache miss), the data has to be recomputed or fetched from its original storage location, which is comparatively slower. Hence, the more requests can be served from the cache the faster the overall system performance.

A Trojan horse, or Trojan, is software that appears to perform a desirable function for the user prior to run or install, but (perhaps in addition to the expected function) steals information or harms the system. The term is derived from the Trojan Horse story in Greek mythology.

Overclocking is the process of operating a computer component at a higher clock rate (more clock cycles per second) than it was designed for or was specified by the manufacturer.

The MD5 Message-Digest Algorithm is a widely used cryptographic hash function that produces a 128-bit (16-byte) hash value., MD5 has been employed in a wide variety of security applications, and is also commonly used to check data integrity.

An assembly language is a low-level programming language for computers, microprocessors, microcontrollers, and other programmable devices. It implements a symbolic representation of the machine codes and other constants needed to program a given CPU architecture.

A hash function is any algorithm or subroutine that maps large data sets to smaller data sets, called keys. For example, a single integer can serve as an index to an array (associative array). The values returned by a hash function are called hash values, hash codes,hash sums, check-sums or simply hashes.

In computer security and programming, a buffer overflow, or buffer overrun, is an anomaly where a program, while writing data to a buffer, overruns the buffer’s boundary and overwrites adjacent memory. This is a special case of violation of memory safety. Buffer overflows can be triggered by inputs that are designed to execute code, or alter the way the program operates. This may result in erratic program behavior, including memory access errors, incorrect results, a crash, or a breach of system security. They are thus the basis of many software vulnerabilities and can be maliciously exploited.

Remote File Inclusion (RFI) is a type of vulnerability most often found on websites. It allows an attacker to include a remote file, usually through a script on the web server. The vulnerability occurs due to the use of user-supplied input without proper validation. This can lead to something as minimal as outputting the contents of the file, but depending on the severity.

SQL often referred to as Structured Query Language is a programming language designed for managing data in relational database management systems (RDBMS). Originally based upon relational algebra and tuple relational calculus, its scope includes data insert, query, update and delete, schema creation and modification, and data access control. SQL injection or SQLi is a code injection technique that exploits a security vulnerability in some computer software. An injection occurs at the database level of an application (like queries). The vulnerability is present when user input is either incorrectly filtered for string literal escape characters embedded in SQL statements or user input is not strongly typed and unexpectedly executed. Using well designed query language interpreters can prevent SQL injections.

Here are some tips and factsheets you would love to check since they are mostly universal in the hackerdom culture and people know it by default. If you do not know this by default, you are missing something and need to work on it:

  1. Ankit Fadia’s seminars are crap and its courses too.
  2. Getting access to router doesn’t provide you access to network.
  3. One cannot just press the key and hack-away a system or bring down the SCADA power grids.
  4. There is no powerful software or an antivirus program or utility which could detect all the malware which are existent.

Would add many if there are feedbacks on some ideas and what could be possibly be missing. Leave your feedbacks in the comments and I’d appreciate if you could whip out some original sources. Roger Out.

C – Practical System Programming for Graphics in C.

Practical System Programming for Graphics in C

By Shritam Bhowmick
Web Application Penetration Tester
LinkedIn: https://www.linkedin.com/profile/view?id=281014248&trk=nav_responsive_tab_profile
Academia: https://independent.academia.edu/ShritamBhowmick
Facebook: https://www.facebook.com/coded32

This is an introduction post for “Graphics and Internet” for WBUT 2014, which includes C as system programming and the underlying source code concepts for drawing basic graphics using C. Earlier in this post, I discussed data-structures in C for WBUT University in details which covered the practical aspects of year 2014. This post in contrary will mention the graphical aspect of the C programming keeping in mind the practical labs. I am glad I could post as far as the syllabus is concerned (which is really outdated as discussed in the last post), but this will help the current students who could have a look here after 2014 passes.

===========================================================================

1.) Draw a Basic Circle:

#include
#include

main()
{
int gd = DETECT, gm;

initgraph(&gd, &gm, "C:TCBGI");

circle(100, 100, 50);

getch();
closegraph();
return 0;
}

===========================================================================

2.) Draw a Rainbow

#include
#include
#include
#include

void main()
{
int gdriver = DETECT,gmode;
int x,y,i;
initgraph(&gdriver,&gmode,"C:TurboC4BGI");
x=getmaxx()/2;
y=getmaxy()/2;
for(i=30;i<200;i++)
{
delay(100);
setcolor(i/10);
arc(x,y,0,180,i-10);
}
getch();
}

===========================================================================

3.) Draw a Smiley

#include
#include
#include
#include

void main()

{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:BGI");
outtextxy(150,10,"This is a program to draw a Smiley Asshole!");
circle(200,200,80);
circle(160,180,8);
circle(240,180,8);
arc(200,230,180,360,20);
line(190,245,210,260);
line(210,245,210,260)
arc(200,260,180,360,9);
line(200,180,195,210);
arc(202,210,190,10,8);
getch();
closegraph();

}

===========================================================================

4.) Draw a Diagonal Line

#include < stdio.h >
#include < conio.h >
#include < graphics.h >

void main()
{
int gdriver=DETECT,gmode=0;
initgraph(&gdriver, &gmode, "c:tcbgi");
line(0,0,getmaxx(),getmaxy());
getch();
closegraph();

}

===========================================================================

5.) Draw an Eclipse

#include
#include

main()
{
int gd = DETECT, gm;

initgraph(&gd, &gm, "C:TCBGI");

ellipse(100, 100, 0, 360, 50, 25);

getch();
closegraph();
return 0;
}

===========================================================================

5.) Draw a Triangle

#include
#include
#include

void main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "c:tcbgi");

line(300, 100, 200, 200);
line(300, 100, 400, 200);
line(200, 200, 400, 200);

getch();
closegraph();
}

There are others as well which are related to Web Development. The posts belonging to Web-development could already be accessed from here. The section is for everything related to ‘code’ and working with code in different languages. HTML could be found there. I would appreciate if the readers leave behind a feedback. Roger out./-

Data Structures in C Practicals and Why Colleges with their IDE’s are Outdated. A Wiser Proof of Concept.

Data Structures in C and Why Colleges with their IDE’s are Outdated!

By Shritam Bhowmick
Web Application Penetration Tester
LinkedIn: https://www.linkedin.com/profile/view?id=281014248&trk=nav_responsive_tab_profile
Academia: https://independent.academia.edu/ShritamBhowmick
Facebook: https://www.facebook.com/coded32

The Introduction Preface

Hi, this is about all the practical lab C  for WBUT Winters (Semester 3rd of the University prescribed Syllabus) section data-structures questions solved at once place for a ready reference to the students who might seek help and also on an information as to why colleges now should be moving from the stone age. All the programs are complied under Tubro C++ compiler and would not be/should not be expected to be executed/processed to create an object under GNU C compiler or  any Windows variant compilers (such as Visual C compiler). Since the syllabus itself is too outdated and people had been talking about them in various forums; I came up with a compilation of the code as well as the questions which could help them understand the core with the ready service code to be executed only within from Turbo C++ compiler. This is C code and not C++ code. The source code for each of them had been practically tested on Windows 8.1 platform using DOSBOX as an emulator to run Turbo C.

The Recommendations

It’s recommended to move on with the syllabus and pay less attention there, if anyone really is serious on C and C++ programming. Python, Ruby, Delphi, Lua and Perl are the modern spectacular languages to choose from and Java is really not that friendly as you might think if you have to do a certain task. Java isn’t that flexible in terms that Java would really need long source code for a simple problem. See “Python to other languages – A comparison“. Get to know more at Stackoverflow. To refernce from the original source, here’s is the justification why someone would prefer to code in Python rather than any other older languages in a nutshell:

  1. Speed of development. I can crank out working code before the Java folks have stuff that will compile.
  2. Flexibility. I can refactor and rework a module in an afternoon. The Java folks have to redesign things on paper to be sure they can get some of the bigger refactorings to recompile. In Java, the changes aren’t as small and focused.I should probably blog about some of the nastier refactorings that would be a huge pain — even using the refactoring tools in Eclipse or NetBeans. The most recent example was a major change to the top-most superclass of a class hierarchy that would have invalidated a mountain of Java code. But the method was used in exactly two places, allowing a trivial change to the superclass and those two places. 100’s of unit tests passed, so I’m confident in the change without the compiler overhead.
  3. Simplicity. Python doesn’t need a sophisticated IDE and super-complex WS libraries. Python code can be written in Komodo Edit. A WS interface between your Java front-end and PostgresSQL can be knocked together in Werkzeug in a day. You can plug your transactions into this without much overhead.

The post primarily covered Python against Java, but there are instances why some one could just prefer Python over C which could be referenced from here in this post. This does not at all mean to compare C and Python on basis of what they have to deliver but rather compares them on an average on the end-users opinion perspective. There are other considerations as well into modern computing which is resolved below in the list.

Why Would Python be Slow but equivalently faster ever than C Deployments?

Python is a higher level language than C, which means it abstracts the details of the computer from you – memory management, pointers, etc, and allows you to write programs in a way which is closer to how humans think. It is true that C code usually runs 10 to 100 times faster than Python code if you measure only the execution time. However if you also include the development time Python often beats C. For many projects the development time is far more critical than the run time performance. Longer development time converts directly into extra costs, fewer features and slower time to market.

Why use C contrary to the switching to any other programming languages, a hope out of no-where!?

“If there ever were a quote that described programming with C, it would be this. To many programmers, this makes C scary and evil. It is the Devil, Satan, the trickster Loki come to destroy your productivity with his seductive talk of pointers and direct access to the machine. Then, once this computational Lucifer has you hooked, he destroys your world with the evil “segfault” and laughs as he reveals the trickery in your bargain with him.

But, C is not to blame for this state of affairs. No my friends, your computer and the Operating System controlling it are the real tricksters. They conspire to hide their true inner workings from you so that you can never really know what is going on. The C programming language’s only failing is giving you access to what is really there, and telling you the cold hard raw truth. C gives you the red pill. C pulls the curtain back to show you the wizard. C is truth. Why use C then if it’s so dangerous? Because C gives you power over the false reality of abstraction and liberates you from stupidity.”

The Coverage

Now since we are already hopped into C with Turbo C as a compiler. I’d go writing the source code of the practicals here and try to keep it updated as much as I could to my disposal. Instead this is the responsibility at your side to post back with comments on what else its to be added so this could help others in the process.

Notes

The source code has replaced all the instances of \n to n. Take care of that plus the include headers have not been defined to be kept away from copy/paste practices. This in my opinion will lead to a better coding practices and would lead to a great start with self-research.

The Question Set

The questions I have dug so far follows these:

  1. Doubly Linked List with insertion and deletion.
  2. Priority Queue with addition and deletion of elements.
  3. Dequeue or Double Ended Queue using Linked List.
  4. Dequeue or Double Ended Queue using Circular Array.
  5. Priority Queue Implementation using Arrays.
  6. Circular Queue using Linked List with Circular Queue Concept Notes.
  7. Circular Queue Implementation in C Data-Structures using Arrays.
  8. Stack for PUSH and POP operations in C both via recursive and regular methods.
  9. Linear Search Implementation in C using Arrays.
  10. Binary Search Implementation in C using Arrays.
  11. Insertion Sort Implementation in C using Arrays.
  12. Merge Sort Implementation in C using Arrays.
  13. Quick Sort Implementation in C using Arrays.
  14. Heap Sort Implementation in C using Arrays.

Source Code Solutions

1.) Doubly Linked List with Insertion, Deletion and Reverse Display

Here’s the source code (to avoid copy/paste gimmicks, I have stripped off all the includes. Feel free to add them at your own expense) for Doubly Linked List:

#include
#include
#include
struct node
{
struct node *previous;
int data;
struct node *next;
}*head, *last;

void insert_beginning(int value)
{
struct node *var,*temp;
var=(struct node *)malloc(sizeof(struct node));
var->data=value;
if(head==NULL)
{
head=var;
head->previous=NULL;
head->next=NULL;
last=head;
}
else
{
temp=var;
temp->previous=NULL;
temp->next=head;
head->previous=temp;
head=temp;
}
}

void insert_end(int value)
{
struct node *var,*temp;
var=(struct node *)malloc(sizeof(struct node));
var->data=value;
if(head==NULL)
{
head=var;
head->previous=NULL;
head->next=NULL;
last=head;
}
else
{
last=head;
while(last!=NULL)
{
temp=last;
last=last->next;
}
last=var;
temp->next=last;
last->previous=temp;
last->next=NULL;
}
}

int insert_after(int value, int loc)
{
struct node *temp,*var,*temp1;
var=(struct node *)malloc(sizeof(struct node));
var->data=value;
if(head==NULL)
{
head=var;
head->previous=NULL;
head->next=NULL;
}
else
{
temp=head;
while(temp!=NULL && temp->data!=loc)
{
temp=temp->next;
}
if(temp==NULL)
{
printf("n%d is not present in list ",loc);
}
else
{
temp1=temp->next;
temp->next=var;
var->previous=temp;
var->next=temp1;
temp1->previous=var;
}
}
last=head;
while(last->next!=NULL)
{
last=last->next;
}
}
int delete_from_end()
{
struct node *temp;
temp=last;
if(temp->previous==NULL)
{
free(temp);
head=NULL;
last=NULL;
return 0;
}
printf("nData deleted from list is %d n",last->data);
last=temp->previous;
last->next=NULL;
free(temp);
return 0;
}

int delete_from_middle(int value)
{
struct node *temp,*var,*t, *temp1;
temp=head;
while(temp!=NULL)
{
if(temp->data == value)
{
if(temp->previous==NULL)
{
free(temp);
head=NULL;
last=NULL;
return 0;
}
else
{
var->next=temp1;
temp1->previous=var;
free(temp);
return 0;
}
}
else
{
var=temp;
temp=temp->next;
temp1=temp->next;
}
}
printf("data deleted from list is %d",value);
}

void display()
{
struct node *temp;
temp=head;
if(temp==NULL)
{
printf("List is Empty");
}
while(temp!=NULL)
{
printf("-> %d ",temp->data);
temp=temp->next;
}
}

int main()
{
int value, i, loc;
head=NULL;
printf("Select the choice of operation on link list");
printf("n1.) insert at begningn2.) insert at atn3.) insert at middle");
printf("n4.) delete from endn5.) reverse the link listn6.) display listn7.)exit");
while(1)
{
printf("nnenter the choice of operation you want to do ");
scanf("%d",&i);
switch(i)
{
case 1:
{
printf("enter the value you want to insert in node ");
scanf("%d",&value);
insert_beginning(value);
display();
break;
}
case 2:
{
printf("enter the value you want to insert in node at last ");
scanf("%d",&value);
insert_end(value);
display();
break;
}
case 3:
{
printf("after which data you want to insert data ");
scanf("%d",&loc);
printf("enter the data you want to insert in list ");
scanf("%d",&value);
insert_after(value,loc);
display();
break;
}
case 4:
{
delete_from_end();
display();
break;
}
case 5:
{
printf("enter the value you want to delete");
scanf("%d",value);
delete_from_middle(value);
display();
break;
}
case 6 :
{
display();
break;
}
case 7 :
{
exit(0);
break;
}
}
}
printf("nn%d",last->data);
display();
getch();
}

2.) Priority Queue with Addition and Deletion of Elements

#include
#include
#define MAX 5

void insert_by_priority(int);
void delete_by_priority(int);
void create();
void check(int);
void display_pqueue();

int pri_que[MAX];
int front, rear;

void main()

{

int n, ch;
printf("n1 - Insert an element into queue");
printf("n2 - Delete an element from queue");
printf("n3 - Display queue elements");
printf("n4 - Exit");
create();

while (1)

{

printf("nEnter your choice : ");

scanf("%d", &ch);

switch (ch)

{

case 1:

printf("nEnter value to be inserted : ");

scanf("%d",&n);

insert_by_priority(n);

break;

case 2:

printf("nEnter value to delete : ");

scanf("%d",&n);

delete_by_priority(n);

break;

case 3:

display_pqueue();

break;

case 4:

exit(0);

default:

printf("nChoice is incorrect, Enter a correct choice");

}

}

}

/* Function to create an empty priority queue */

void create()

{

front = rear = -1;

}

/* Function to insert value into priority queue */

void insert_by_priority(int data)

{

if (rear >= MAX - 1)

{

printf("nQueue overflow no more elements can be inserted");

return;

}

if ((front == -1) && (rear == -1))

{

front++;

rear++;

pri_que[rear] = data;

return;

}

else

check(data);

rear++;

}

/* Function to check priority and place element */

void check(int data)

{

int i,j;

for (i = 0; i <= rear; i++) { if (data >= pri_que[i])

{

for (j = rear + 1; j > i; j--)

{

pri_que[j] = pri_que[j - 1];

}

pri_que[i] = data;

return;

}

}

pri_que[i] = data;

}

/* Function to delete an element from queue */

void delete_by_priority(int data)

{

int i;

if ((front==-1) && (rear==-1))

{

printf("nQueue is empty no elements to delete");

return;

}

for (i = 0; i <= rear; i++)

{

if (data == pri_que[i])

{

for (; i < rear; i++)

{

pri_que[i] = pri_que[i + 1];

}

pri_que[i] = -99;

rear--;

if (rear == -1)

front = -1;

return;

}

}

printf("n%d not found in queue to delete", data);

}

/* Function to display queue elements */

void display_pqueue()

{

if ((front == -1) && (rear == -1))

{

printf("nQueue is empty");

return;

}

for (; front <= rear; front++)

{

printf(" %d ", pri_que[front]);

}

front = 0;

}

3.) Dequeue or Double Ended Queue using Linked List

What is Dequeue?

The word dequeue is short form of double ended queue. In a dequeue , insertion as well as deletion can be carried out either at the rear end or the front end. The following diagram illustrates a version of the same concept:

Dequeue Represented Using a Circular Array

The source code is available below:

#define MAX 100
#include
#include

void insert(int);
int delStart();
int delEnd();
int queue[MAX];
int rear =0, front =0;
void display();
void insertEnd(int);
void insertStart(int);

int main()
{
char ch , a='y';
int choice, c, token;
printf("Enter your choice for which deque operation you want to perform operation n");
do
{
printf("n1.Input-restricted deque n");
printf("2.output-restricted deque n");
printf("nEnter your choice for the operation : ");
scanf("%d",&c);
switch(c)
{
case 1:
printf("nDo operation in Input-Restricted c dequen");
printf("1.Insert");
printf("n2.Delete from end");
printf("n3.Delete from begning");
printf("n4.show or display");
do
{
printf("nEnter your choice for the operation in c deque: ");
scanf("%d",&choice);
switch(choice)
{
case 1: insertEnd(token);
display();
break;
case 2: token=delEnd();
printf("nThe token deleted is %d",token);
display();
break;
case 3: token=delStart();
printf("nThe token deleted is %d",token);
display();
break;
case 4: display();
break;
default:printf("Wrong choice");
break;
}
printf("nDo you want to continue(y/n) to do operation in input-restricted c deque: ");
ch=getch();
}
while(ch=='y'||ch=='Y');
getch();
break;

case 2 :
printf("nDo operation in Output-Restricted c dequen");
printf("1.Insert at the End");
printf("n2.Insert at the begning");
printf("n3.Delete the element");
printf("n4.show or display");
do
{
printf("nEnter your choice for the operation: ");
scanf("%d",&choice);
switch(choice)
{
case 1: insertEnd(token);
display();
break;
case 2: insertStart(token);
display();
break;
case 3: token=delStart();
printf("nThe token deleted is %d",token);
display();
break;
case 4: display();
break;
default:printf("Wrong choice");
break;
}
printf("nDo you want to continue(y/n):");
ch=getch();
}
while(ch=='y'||ch=='Y');
getch();
break ;
}

printf("nDo you want to continue(y/n):");
ch=getch();
}
while(ch=='y'||ch=='Y');
getch();
}

void display()
{
int i;
printf("nThe queue elements are:");
for(i=rear;i<front;i++)
{
printf("%d  ",queue[i]);
}
}

void insertEnd(int token)
{
char a;
if(front==MAX/2)
{
printf("nQueue fullnyou cant enter more elements at the end of c queue");
return;
}
do
{
printf("nEnter the token to be inserted:");
scanf("%d",&token);
queue[front]=token;
front=front+1;
printf("do you want to continue insertion Y/N");
a=getch();
}
while(a=='y');
}

void insertStart(int token)
{
char a;
if(front==MAX/2)
{
printf("nQueue fullnyou cant enter more elements at the start of queue");
return;
}
do
{
printf("nEnter the token to be inserted:");
scanf("%d",&token);
rear=rear-1;
queue[rear]=token;
printf("do you want to continue insertion Y/N");
a=getch();
}
while(a=='y');
}
int delEnd()
{
int t;
if(front==rear)
{
printf("nQueue empty");
return 0;
}
front=front-1;
t=queue[front+1];
return t;
}
int delStart()
{
int t;
if(front==rear)
{
printf("nQueue empty");
return 0;
}
rear=rear+1;
t=queue[rear-1];
return t;
}

4.) Dequeue or Double Ended Queue using Circular Array

The available source code is different from the previous one which used linked list. This time the code uses the concept for circular array and implements a Dequeue or Double Ended Queue implementation around circular array. The source code is below as per the references.

#include
#include

#define MAX 30

typedef struct dequeue
{
int data[MAX];
int rear,front;
}dequeue;

void initialize(dequeue *p);
int empty(dequeue *p);
int full(dequeue *p);
void enqueueR(dequeue *p,int x);
void enqueueF(dequeue *p,int x);
int dequeueF(dequeue *p);
int dequeueR(dequeue *p);
void print(dequeue *p);

void main()
{
int i,x,op,n;
dequeue q;
initialize(&q);

do
{
printf("n1.Createn2.Insert(rear)n3.Insert(front)n4.Delete(rear)n5.Delete(front)");
printf("n6.Printn7.ExitnnEnter your choice:");
scanf("%d",&op);

switch(op)
{
case 1: printf("nEnter number of elements:");
scanf("%d",&n);
initialize(&q);
printf("nEnter the data:");

for(i=0;i<n;i++) { scanf("%d",&x); if(full(&q)) { printf("nQueue is full!!"); exit(0); } enqueueR(&q,x); } break; case 2: printf("nEnter element to be inserted:"); scanf("%d",&x); if(full(&q)) { printf("nQueue is full!!"); exit(0); } enqueueR(&q,x); break; case 3: printf("nEnter the element to be inserted:"); scanf("%d",&x); if(full(&q)) { printf("nQueue is full!!"); exit(0); } enqueueF(&q,x); break; case 4: if(empty(&q)) { printf("nQueue is empty!!"); exit(0); } x=dequeueR(&q); printf("nElement deleted is %dn",x); break; case 5: if(empty(&q)) { printf("nQueue is empty!!"); exit(0); } x=dequeueF(&q); printf("nElement deleted is %dn",x); break; case 6: print(&q); break; default: break; } }while(op!=7); } void initialize(dequeue *P) { P->rear=-1;
P->front=-1;
}

int empty(dequeue *P)
{
if(P->rear==-1)
return(1);
return(0);
}

int full(dequeue *P)
{
if((P->rear+1)%MAX==P->front)
return(1);
return(0);
}

void enqueueR(dequeue *P,int x)
{
if(empty(P))
{
P->rear=0;
P->front=0;
P->data[0]=x;
}
else
{
P->rear=(P->rear+1)%MAX;
P->data[P->rear]=x;
}
}

void enqueueF(dequeue *P,int x)
{
if(empty(P))
{
P->rear=0;
P->front=0;
P->data[0]=x;
}
else
{
P->front=(P->front-1+MAX)%MAX;
P->data[P->front]=x;
}
}

int dequeueF(dequeue *P)
{
int x;
x=P->data[P->front];
if(P->rear==P->front)   //delete the last element
initialize(P);
else
P->front=(P->front+1)%MAX;
return(x);
}

int dequeueR(dequeue *P)
{
int x;
x=P->data[P->rear];
if(P->rear==P->front)
initialize(P);
else
P->rear=(P->rear-1+MAX)%MAX;
return(x);
}

void print(dequeue *P)
{
if(empty(P))
{
printf("nQueue is empty!!");
exit(0);
}

int i;
i=P->front;
while(i!=P->rear)
{
printf("n%d",P->data[i]);
i=(i+1)%MAX;
}
printf("n%dn",P->data[P->rear]);
}

5.) Priority Queue using Array

I noticed I didn’t updated a priority queue implementation using arrays. This is the reason the source code is here for a ready reference to the readers of the blog. Here is the original version of the source code which is tested against the terms and conditions posted before which should be able to run via a Turbo C compiler. Any other compiler just won’t work because the syllabus itself is outdated, and people (and universities) must realize this fast.

#include
#include
#define size 5

int queue[5][2] = {0};
int top = -1;
int bottom;

void push(int value, int pr)

{

int i,j,k;

if(top < size-1) { if(queue[top][1] > pr)

{

for(i=0;i<top;i++) { if(queue[i][1] > pr)

{

break;

}

}

for(j=top;j>=i;j--)

{

queue[j+1][0] = queue[j][0];

queue[j+1][1] = queue[j][1];

}

top++;

queue[i][0] = value;

queue[i][1] = pr;

}

else

{

top++;

queue[top][0] = value;

queue[top][1] = pr;

}

}

else

{

printf("queue overflow n");

}

}

void pop()

{

int i;

if(queue[0][0] == 0)

{

printf("n The queue is empty  n");

}

else

{

printf("After , dequeue the following value is erased n  %d n", queue[0][0]);

for(i=0;i<top;i++) { queue[i][0] = queue[i+1][0]; queue[i][1] = queue[i+1][1]; } queue[top][0] = 0; queue[top][1] = 0; top--; } } void display() { int i,j; printf("ElementtPriority n"); for(i=size - 1;i>=0;i--)

{

for(j=0;j<2;j++)

{

printf(" %dt",queue[i][j]);

}

printf("n");

}

}

int main()

{

int i,j, ch=0 ,value = 0,pr=0;

while(1)

{

printf("n Please Enter the choice. n");

printf("1 for Enqueue n 2 for Dequeue n 3 for displayn  5 for exit: t n");

scanf("%d",&ch);

switch(ch)

{

case 1:

printf("n Please Enter the number to be inserted: t ");

scanf("%d", &value);

printf("n Please Enter the priority: t ");

scanf("%d", &pr);

push(value,pr);

break;

case 2:

pop();

break;

case 3:

display();

break;

case 5:

exit(0);

default:

printf("You entered wrong choice!");

}
}
}

6.) Circular Queue using Linked List in C Data-structures with Notes

In a standard queue data structure re-buffering problem occurs for each dequeue operation. To solve this problem by joining the front and rear ends of a queue to make the queue as a circular queue
Circular queue is a linear data structure. It follows FIFO principle.

  • In circular queue the last node is connected back to the first node to make a circle.
  • Circular linked list fallow the First In First Out principle
  • Elements are added at the rear end and the elements are deleted at front end of the queue
  • Both the front and the rear pointers points to the beginning of the array.
  • It is also called as “Ring buffer”.
  • Items can inserted and deleted from a queue in O(1) time.

Circular Queue can be created in three ways. They are:

  • · Using single linked list
  • · Using double linked list
  • · Using arrays

Follow is the source code for implementing a Circular Queue in C using Linked List:

#include
#include
#define max 5
int q[max];
int rear = -1, front = -1;
void insert();
void delet();
void display();

void main()
{
char ch;
int choice;
do
{
clrscr();
printf("n1 -> Insert");
printf("n2 -> Delete");
printf("n3 -> Display");
printf("n0 -> Exit");

printf("nnEnter Your Choice -> ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
display();
break;
case 0:
exit();
default:
printf("nnInvalid Choice");
}
printf("nnDo u Want to Continue -> ");
ch = getche();
} while (ch == 'y' || ch == 'Y');
}

void insert()
{
int data;
if ((rear == max - 1 && front == 0) || rear == front - 1)
{
printf("nSorry! Q is Full");
}
else
{
printf("nEnter data You want to insert ->");
scanf("%d", &data);
if (front == -1)
{
front++;
rear++;
}
else if (rear == max - 1)
{
rear = 0;
}
else
{
rear++;
}
q[rear] = data;
printf("nnData inserted successfully");
}
}
void delet()
{
if (front == -1)
{
printf("nnSorry! Q is Empty");
}
else
{
if (front == rear)
{
front = rear = -1;
}
else if (front == max - 1)
{
front = 0;
}
else
{
front++;
}
printf("nElement deleted Successfully");
}
}

void display()
{
int i;
if (front == -1)
{
printf("nnSorry! Q is Empty");
}
else
{
printf("nn:: Queue Elements are ::n");
if (front <= rear)
{
for (i = front; i <= rear; i++)
{
printf("n%d", q[i]);
}
}
else
{
for (i = front; i < max; i++)
{
printf("n%d", q[i]);
}
for (i = 0; i <= rear; i++)
{
printf("n%d", q[i]);
}
}
}
}

7.) Circular Queue Implementation in C using Arrays

Earlier in the post, we talked about implementation circular queue using Linked List. This section talks about the same implementation but using arrays. The source code below could be used for the purposes to demonstrate the concepts:

#include
#include
#include
#define MAXSIZE 5
int cq[MAXSIZE];
int front,rear;
void main()
{
void add(int,int);
void del(int);
int will=1,i,num;
front = -1;
rear = -1;
clrscr();
printf("nProgram for Circular Queue demonstration through array");
while(1)
{
printf("nnMAIN MENUn1.INSERTIONn2.DELETIONn3.EXIT");
printf("nnENTER YOUR CHOICE : ");
scanf("%d",&will);
switch(will)
{
case 1:
printf("nnENTER THE QUEUE ELEMENT : ");
scanf("%d",&num);
add(num,MAXSIZE);
break;
case 2:
del(MAXSIZE);
break;
case 3:
exit(0);
default: printf("nnInvalid Choice . ");
}

} //end of  outer while
}               //end of main
void add(int item,int MAX)
{
//rear++;
//rear= (rear%MAX);
if(front ==(rear+1)%MAX)
{
printf("nnCIRCULAR QUEUE IS OVERFLOW");
}
else
{
if(front==-1)
front=rear=0;
else
rear=(rear+1)%MAX;
cq[rear]=item;
printf("nnRear = %d    Front = %d ",rear,front);
}
}
void del(int MAX)
{
int a;
if(front == -1)
{
printf("nnCIRCULAR QUEUE IS UNDERFLOW");
}
else
{
a=cq[front];
if(front==rear)
front=rear=-1;
else
front = (front+1)%MAX;
printf("nnDELETED ELEMENT FROM QUEUE IS : %d ",a);
printf("nnRear = %d    Front = %d ",rear,front);

}
}

Here is a similar implementation of a queue using array. The source code could be referenced from the below:

#include
#include
#define N 6

int queue[N]={0};
int rear=0,front=0;

void insert(void);
void del(void);
void disp(void);
void cre(void);

void main()
{
int user=0;

clrscr();
while(user!=4)
{
clrscr();
printf("nnnttt THE SIZE OF QUEUE IS %d",N);
printf("nt 1.INSERT");
printf("nt 2.DELETE");
printf("nt 3.DISPLAY");
printf("nt 4.EXIT");
printf("nt 5.CREATE");
scanf("%d",&user);
switch(user)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
disp();
break;
case 4:
printf("nt THANK U");
break;
case 5:
cre();
break;
}
getch();

}
getch();
}

void insert(void)
{
int t;
if(rear<N)
{
printf("nt ENTER A VALUE IN QUEUE");
scanf("%d",&t);
queue[rear]=t;
rear++;
}
else
{
printf("nt Q OVERFLOW!!!!!!!!!!!!!!!");
}
}
void del(void)
{
int i;
printf("nt %d gets deleted.........",queue[front]);
queue[front]=0;
front++;
}
void disp(void)
{
int i;
for(i=front;i<rear;i++)
{
printf("nt %d",queue[i]);
}
}
void cre(void)
{

int t;
printf("nt ENTER A VALUE IN QUEUE");
scanf("%d",&t);
front=0;
queue[front]=t;
rear=front+1;
}

8.) Stack Implementation to perform PUSH and POP Operations

After having gone through the Queue utilities in C and discussing queue with circular based and using Linked List, here is the source code to implement Stack for PUSH and POP operations in C.

#include
#define MAXSIZE 5

struct stack
{
int stk[MAXSIZE];
int top;
};
typedef struct stack STACK;
STACK s;

void push(void);
int  pop(void);
void display(void);

void main ()
{
int choice;
int option = 1;
s.top = -1;

printf ("STACK OPERATIONn");
while (option)
{
printf ("------------------------------------------n");
printf ("      1    -->    PUSH               n");
printf ("      2    -->    POP               n");
printf ("      3    -->    DISPLAY               n");
printf ("      4    -->    EXIT           n");
printf ("------------------------------------------n");

printf ("Enter your choicen");
scanf    ("%d", &choice);
switch (choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
return;
}
fflush (stdin);
printf ("Do you want to continue(Type 0 or 1)?n");
scanf    ("%d", &option);
}
}
/*  Function to add an element to the stack */
void push ()
{
int num;
if (s.top == (MAXSIZE - 1))
{
printf ("Stack is Fulln");
return;
}
else
{
printf ("Enter the element to be pushedn");
scanf ("%d", &num);
s.top = s.top + 1;
s.stk[s.top] = num;
}
return;
}
/*  Function to delete an element from the stack */
int pop ()
{
int num;
if (s.top == - 1)
{
printf ("Stack is Emptyn");
return (s.top);
}
else
{
num = s.stk[s.top];
printf ("poped element is = %dn", s.stk[s.top]);
s.top = s.top - 1;
}
return(num);
}
/*  Function to display the status of the stack */
void display ()
{
int i;
if (s.top == -1)
{
printf ("Stack is emptyn");
return;
}
else
{
printf ("n The status of the stack is n");
for (i = s.top; i >= 0; i--)
{
printf ("%dn", s.stk[i]);
}
}
printf ("n");
}

Here is yet another version of Stack implementation but using recursive method:

#include

#include

struct node

{

int a;

struct node *next;

};

void generate(struct node **);

void display(struct node *);

void stack_reverse(struct node **, struct node **);

void delete(struct node **);

int main()

{

struct node *head = NULL;

generate(&head);

printf("nThe sequence of contents in stackn");

display(head);

printf("nInversing the contents of the stackn");

if (head != NULL)

{

stack_reverse(&head, &(head->next));

}

printf("nThe contents in stack after reversaln");

display(head);

delete(&head);

return 0;

}

void stack_reverse(struct node **head, struct node **head_next)

{

struct node *temp;

if (*head_next != NULL)

{

temp = (*head_next)->next;

(*head_next)->next = (*head);

*head = *head_next;

*head_next = temp;

stack_reverse(head, head_next);

}

}

void display(struct node *head)

{

if (head != NULL)

{

printf("%d  ", head->a);

display(head->next);

}

}

void generate(struct node **head)

{

int num, i;

struct node *temp;

printf("Enter length of list: ");

scanf("%d", &num);

for (i = num; i > 0; i--)

{

temp = (struct node *)malloc(sizeof(struct node));

temp->a = i;

if (*head == NULL)

{

*head = temp;

(*head)->next = NULL;

}

else

{

temp->next = *head;

*head = temp;

}

}

}

void delete(struct node **head)

{

struct node *temp;

while (*head != NULL)

{

temp = *head;

*head = (*head)->next;

free(temp);

}

}

9.) Linear Search Implementation in C

This is an abstract source code of Linear search utility in C. This could be used along in the Turbo C compiler. Others were specifically not tested and not recommended.

#include

int main()
{
int array[100], search, c, n;

printf("Enter the number of elements in arrayn");
scanf("%d",&n);

printf("Enter %d integer(s)n", n);

for (c = 0; c < n; c++)
scanf("%d", &array[c]);

printf("Enter the number to searchn");
scanf("%d", &search);

for (c = 0; c < n; c++)
{
if (array[c] == search)     /* if required element found */
{
printf("%d is present at location %d.n", search, c+1);
break;
}
}
if (c == n)
printf("%d is not present in array.n", search);

return 0;
}

10.) Binary Search using Arrays

The source code below is the implementation of binary search algorithm in C using arrays.

#include
int main(){

int a[10],i,n,m,c=0,l,u,mid;

printf("Enter the size of an array: ");
scanf("%d",&n);

printf("Enter the elements in ascending order: ");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}

printf("Enter the number to be search: ");
scanf("%d",&m);

l=0,u=n-1;
while(l<=u){
mid=(l+u)/2;
if(m==a[mid]){
c=1;
break;
}
else if(m<a[mid]){
u=mid-1;
}
else
l=mid+1;
}
if(c==0)
printf("The number is not found.");
else
printf("The number is found.");

return 0;
}

11.) Insertion Sort Implementation in C using Arrays.

This is insertion sort in C using Arrays. The source code is available below:

#include

int main()
{
int n, array[1000], c, d, t;

printf("Enter number of elementsn");
scanf("%d", &n);

printf("Enter %d integersn", n);

for (c = 0; c < n; c++) {
scanf("%d", &array[c]);
}

for (c = 1 ; c <= n - 1; c++) { d = c; while ( d > 0 && array[d] < array[d-1]) {
t          = array[d];
array[d]   = array[d-1];
array[d-1] = t;

d--;
}
}

printf("Sorted list in ascending order:n");

for (c = 0; c <= n - 1; c++) {
printf("%dn", array[c]);
}

return 0;
}

12.) Merge Sort in C using Arrays

Here is the source code for Merge sort in C using Arrays:

#include
#define MAX 50

void mergeSort(int arr[],int low,int mid,int high);
void partition(int arr[],int low,int high);

int main(){

int merge[MAX],i,n;

printf("Enter the total number of elements: ");
scanf("%d",&n);

printf("Enter the elements which to be sort: ");
for(i=0;i<n;i++){
scanf("%d",&merge[i]);
}

partition(merge,0,n-1);

printf("After merge sorting elements are: ");
for(i=0;i<n;i++){
printf("%d ",merge[i]);
}

return 0;
}

void partition(int arr[],int low,int high){

int mid;

if(low<high){
mid=(low+high)/2;
partition(arr,low,mid);
partition(arr,mid+1,high);
mergeSort(arr,low,mid,high);
}
}

void mergeSort(int arr[],int low,int mid,int high){

int i,m,k,l,temp[MAX];

l=low;
i=low;
m=mid+1;

while((l<=mid)&&(m<=high)){

if(arr[l]<=arr[m]){ temp[i]=arr[l]; l++; } else{ temp[i]=arr[m]; m++; } i++; } if(l>mid){
for(k=m;k<=high;k++){
temp[i]=arr[k];
i++;
}
}
else{
for(k=l;k<=mid;k++){
temp[i]=arr[k];
i++;
}
}

for(k=low;k<=high;k++){
arr[k]=temp[k];
}
}

13.) Quick Sort Implementation in C using Arrays

Below is the raw source code for Quick Sort implementation in C using Arrays:

#include

void quicksort(int [10],int,int);

int main(){
int x[20],size,i;

printf("Enter size of the array: ");
scanf("%d",&size);

printf("Enter %d elements: ",size);
for(i=0;i<size;i++)
scanf("%d",&x[i]);

quicksort(x,0,size-1);

printf("Sorted elements: ");
for(i=0;i<size;i++)
printf(" %d",x[i]);

return 0;
}

void quicksort(int x[10],int first,int last){
int pivot,j,temp,i;

if(first<last){
pivot=first;
i=first;
j=last;

while(i<j){
while(x[i]<=x[pivot]&&i<last) i++; while(x[j]>x[pivot])
j--;
if(i<j){
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}

temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);

}
}

14.) Heap Sort Implementation in C using Arrays

Below is the source code for Heap Sort in C using Arrays:

#include
void main()

{

int heap[10], no, i, j, c, root, temp;

printf("n Enter no of elements :");

scanf("%d", &no);

printf("n Enter the nos : ");

for (i = 0; i < no; i++)

scanf("%d", &heap[i]);

for (i = 1; i < no; i++)

{

c = i;

do

{

root = (c - 1) / 2;

if (heap[root] < heap[c])   /* to create MAX heap array */

{

temp = heap[root];

heap[root] = heap[c];

heap[c] = temp;

}

c = root;

} while (c != 0);

}

printf("Heap array : ");

for (i = 0; i < no; i++) printf("%dt ", heap[i]); for (j = no - 1; j >= 0; j--)

{

temp = heap[0];

heap[0] = heap[j    /* swap max element with rightmost leaf element */

heap[j] = temp;

root = 0;

do

{

c = 2 * root + 1;    /* left node of root element */

if ((heap[c] < heap[c + 1]) && c < j-1)

c++;

if (heap[root]<heap[c] && c<j)    /* again rearrange to max heap array */

{

temp = heap[root];

heap[root] = heap[c];

heap[c] = temp;

}

root = c;

} while (c < j);

}

printf("n The sorted array is : ");

for (i = 0; i < no; i++)

printf("t %d", heap[i]);

printf("n Complexity : n Best case = Avg case = Worst case = O(n logn) n");

}

15.) Bubble Sort Implementation in C using Arrays

Below is the source code for bubble sort in C using Arrays:

#include

int main()
{
int array[100], n, c, d, swap;

printf("Enter number of elementsn");
scanf("%d", &n);

printf("Enter %d integersn", n);

for (c = 0; c < n; c++)
scanf("%d", &array[c]);

for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++) { if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap       = array[d];
array[d]   = array[d+1];
array[d+1] = swap;
}
}
}

printf("Sorted list in ascending order:n");

for ( c = 0 ; c < n ; c++ )
printf("%dn", array[c]);

return 0;
}

5. Operating System – Round Robin CPU Scheduling and Multilevel Queue

Operating System – Round Robin Scheduling

By Shritam Bhowmick
Web Application Penetration Tester
LinkedIn: https://www.linkedin.com/profile/view?id=281014248&trk=nav_responsive_tab_profile
Academia: https://independent.academia.edu/ShritamBhowmick
Facebook: https://www.facebook.com/coded32

Round Robin Scheduling

Previous post discussed the two types of CPU Scheduling, Preemptive and Non-Preemptive CPU Scheduling and was focused with FCFS, SJF and Priority Scheduling techniques. We had discussed in the last post how SJF and Priority Scheduling could be modified to attain preemptive scheduling and why FCFS scheduling cannot be used as preemptive scheduling. This post will go further with another preemptive type of CPU Scheduling which is known as Round Robin CPU Scheduling. Now, as we already know Preemptive Scheduling means forcibly stopping a job from the execution and keep it in the waiting state/blocked state for a period of time until it finishes executing another job execution processing, Round Robin is another instance of Preemptive Scheduling wherein it is possible to stop the job and pick up another job from the READY QUEUE  and start executing it. But there is a difference in the way Round Robin Scheduling in implemented.

In SJF (modified to SRT {Shortest Remaining Time First Scheduling}) and Priority (modified with LONG TERM and SHORT TERM Scheduler), it was possible to attain preemptive CPU Scheduling, and used time (CPU BURST TIME) and priority respectively; this however isn’t the case with Round Robin Scheduling (does not depend on CPU BURST Time of the job, or the priority of the job. All the jobs are treated equally). Round Robin Scheduling, the CPU time is divided into number of quanta. Assume the CPU quanta to be duration = 2 ms (Milli-Second). Now, whenever a job is allocated to the CPU for execution, the job will be executed for a maximum of 2 ms. If the CPU BURST time required for the job execution to be completed is more than 2 ms, after 2 ms the job will be forcibly terminated and pushed back to the READY QUEUE and the new job from the READY QUEUE will be allocated to the CPU for execution and again that this new job will be only until 2 ms since that is the quanta defined in terms of Round Robin CPU Scheduling. In case the job execution requirement for any job is less than 2 ms, the job terminates normally and not forcefully. So after termination of the the job in case the job finishes before 2 ms, the Job might go to the I/O WAITING STATE/BLOCK STATE for the CPU to handle I/O operations or might go to the HALTED STATE/TERMINATED STATE. The CPU can’t stay idle for the remaining time if the jobs has finished before the determined quanta, hence the best efficient applied as per Round Robin implementation is to take a new job and start executing the job from that point of time and hence save time with efficiency. This means, assume if the quanta is 2 ms and the job was finished in 1 ms, a new job from the READY QUEUE is taken by the CPU Scheduler and executed, this new job quanta time starts from the earlier saved time which was 1 ms (2-1 = 1 ms). So for an example, there are 4 jobs in the READY QUEUE to be executed by the CPU :

J1 = 4 ms
J2 = 2 ms
J3 = 3 ms
J4 = 6 ms

Assumption is CPU quanta to be 2 ms. That is 1 quanta = 2 ms. Now for Round Robin CPU Scheduling, the job is taken as FCFS basis, but a limited quanta is available for all the jobs per execution cycle, that is in this case 2 ms (1 quanta). The timeline, these jobs will be served is as follows:

J1    J2    J3    J4

|—–|—–|—–|—–|
2ms 2ms 2ms 2ms

Hence, on the first execution cycle:

J1 = 2 ms completed, remaining: 2 ms of CPU BURST time.
J2 = 2 ms completed, remaining: 0 ms of CPU BURST time.
J3 = 2 ms completed, remaining: 1 ms of CPU BURST time.
J4 = 2 ms completed, remaining: 4 ms of CPU BURST time.

Since, Round Robin CPU Scheduler is taking the jobs in FCFS (First Come First Served) basis, there is no priority or CPU time burst minimum time required job first assigned with the scheduling process. The allocation is done purely on first come, first served basis this way and each execution cycle for each job spends 1 quanta of time that is 2 ms which is in this case. After 1 quanta of time spent on a particular job, the job if remaining CPU BURST remains, get’s to the READY QUEUE and next job is taken. If this next job is processed and only required the exact processing time equal to that of the 1 quanta time, the job can either be pushed to the I/O WAITING STATE to cover I/O operation or might reach the HALTED STATE/TERMINATED STATE which is normal Job execution termination. If the Job has finished before the 1 quanta time, the CPU Scheduler without wasting any time pushes the next job from the READY QUEUE to the ACTIVE STATE and starts executing it until the 1 quanta time period which is being assigned similarly to other jobs. When jobs are pushed to the READY QUEUE, they always are pushed to the back of the READY QUEUE since other jobs which are finished first should be at the first to be taken by the CPU. This means the CPU takes the job from the READY QUEUE head, which again means it is following all the rules of the FCFS basis. It has also to be noted that jobs could be pushed to the READY QUEUE from the I/O WAIT STATE, NEW STATE, or ACTIVE STATE (in-case of forcible termination). Here, we need to look at the process diagram to verify if the jobs could be taken to the READY QUEUE in three ways:

ifwelook

The process state diagram suggests, that indeed the READY QUEUE (READY STATE as per process state diagram) get’s the jobs from all the three directions, that is the active state when forcible termination of job is done by the CPU, the new state when new arrivals of jobs are scheduled to be transferred from the new state to the ready state and the wait or blocked state when the jobs are awaiting I/O operations. Hence, after the first execution cycle, the timeline for the above example would look like this:

J1    J2    J3    J4   J1   J3    J4    J4

|—–|—–|—–|—–|—–|—|——|——|
2ms 2ms 2ms 2ms 2ms 1ms 2ms 2ms (assuming no new jobs have arrived at the READY QUEUE)

Now, after this timeline, all the JOBS will be completed assuming no new job arrivals were in the READY QUEUE from the NEW STATE. Hence all the jobs are treated equally in the Round Robin CPU Scheduling. There must be a timer in the system to remind the CPU what 1 quanta is over for a job, this brings the topic to a new curve. The timer we are talking about here could be programmable as well since in some installations, an administrator might want a small quanta, in other installations, the same administrator might want a higher quanta. SO this timer should be programmable, to interrupt the processor reminding the CPU that 1 quanta is past and a job must be stopped.

Timer (Programmable)

Whenever the CPU Scheduler decides that a new job has to be allocated to the CPU or the CPU allocates a new job for execution, immediately the timer has to be reset. At the end of the time pointer, the timer will generate an interrupt when again the CPU will go to the system program and the responsibility of that system program will be to terminate the current executed job, get a new job from the READY QUEUE, give it to the CPU for execution and re-start the timer. That is what happens when the required CPU BURST time in more than the quanta set in the timer. If the CPU BURST time is less then that of the quanta described in the timer, the process is not executed completely till the timer gives an interrupt. The execution is completed before the timer gives an interrupt but the process for the whole cyclic execution process is not yet completed wholly. So the last statement of every CPU BURST must be a system call. The system call for performing an I/O operation or a system call indicating that the process is complete. Again following the system call, the CPU Scheduler has to fetch a new process (job) from the READY QUEUE, give it to the CPU for execution and at the same time, the timer has to be reset so that the next time quanta starts from that point.

Variations of Round Robin Scheduling

There could be different variations of the Round Robin Scheduling, earlier there were no considerations on the Round Robin Scheduling but there could be Jobs which require more CPU BURST time duration depending if the CPU BURST time is more for I/O Operations (I/O Bound Jobs) or CPU Time (CPU Bound Jobs). The variation will depend on these CPU BURST time duration requirements! The main algorithm used will be Round Robin Scheduling.

To implement these concepts, there is a need to know MULTI-LEVEL QUEUE

MULTI-LEVEL QUEUE

There could be multiple READY QUEUE’s as such the following demonstrating how it would work:

multi-levelqueueThe entire diagram suggests that there could be multiple READY QUEUE which could be taken into consideration depending upon the jobs since the variation of jobs have either I/O operation taking the CPU BURST or CPU time taking the time duration of the CPU Processing. This way, if efficiency has to be maintained such that I/O Operations are given the highest priority, the multiple READY QUEUE would have (for an example) 1 READY QUEUE divided into 3 READY QUEUE, they could be:

  1. Q1
  2. Q2
  3. Q3

Here are the Jobs which would be as per the priority of execution (Round Robin Scheduling Implementation does not use Priority, here we are only giving the job prioritizing the jobs in the READY QUEUE not in the Scheduling Algorithm).

  1. Q1 – Handles the I/O Bound Jobs which require more I/O CPU BURST time duration.
  2. Q2 – Handles the moderate CPU Requirement time duration.
  3. Q3 – Handles the CPU Bound Jobs which require more CPU time duration.

Now the execution of such jobs are in the format as below:

  1. Q1- any jobs which are here must be executed at first preference.
  2. Q2 – only after Q1 is empty, the jobs in Q2 are taken. That is all I/O Bound jobs have to be completed first.
  3. Q3 – only after both Q1 and Q2 are empty, the jobs pending on Low priority that is CPU Bound jobs are taken.

There are disadvantages here. Consider that the jobs could be dynamic in nature which means, a job which is an I/O Bound Job could be changed into requiring CPU time (converts itself into being CPU Bound Job). That way, with the current MULTI-LEVEL QUEUE implementation, the jobs in the Q1 (requiring to be executed first in order) in-spite the jobs being transformed to CPU Bound Job by being of Dynamic Nature has to be in the Q1 and executed in first preference. This is unwanted and there has to be some techniques to resolve this. This is where MULTI-LEVEL FEEDBACK QUEUE is used.

MULTILEVEL FEEDBACK QUEUE

In Multilevel feedback queue, there would be again multiple number of queue’s. Assume there are 3 queues as shown below in the diagram (but with a provision that the jobs could be taken from one queue to another queue).

pushq1toq3Whenever the jobs are pushed into the READY QUEUE, it’s always taken to the QUEUE number 1 which is Q1. The time quanta for all the queue’s are:

  1. Q1 = 2 ms
  2. Q2 = 5 ms
  3. Q3 = 10 ms

Whenever a job is entered the first time, let’s assume the nature of the jobs isn’t known that is whether the Job is I/O Bound Job or a CPU Bound Job (what will be the CPU BURST Duration). In queue number 1, the job is to be put. In queue number 1 (Q1) the jobs will be allocated to the CPU using Round Robin Scheduling, for execution of the job. Once it is allocated to the CPU, it will be executed for a minimum of 2 ms, if one fins that the job (The CPU BURST) is over is finished before 2 ms, the job will be kept in Q1 and would not be moved to Q2. This is because the first it has executed, it has shown that the CPU BURST requirement is less than 2 ms, so it’s predicted that the next CPU BURST time will also be less than 2 ms. If the CPU BURST time of the Job is less than 2 ms, the Job remains in Q1. If the timer trigger has occurred and the job is greater than 2 ms, the job will be pushed to next queue which is Q2 which has the time quanta of more than 2 ms. This will again follow the Round Robin CPU Scheduling technique for the job to be allocated to the CPU for execution. This time the time quanta is 5 ms in Q2. In Q2, if one fins that the CPU time requirement for the job is less than 5 ms, it will remain in Q2 itself. Otherwise if it is greater than 5 ms, it will be pushed to the next queue which is Q3, with time quanta 10 ms. All of these queue will follow the Round Robing CU Scheduling algorithm for CPU jobs allocation for job execution.

So conclusively, the first time the job is pushed to the Q1, if the job CPU BURST duration is grater than the current quanta threshold of that particular queue, it would be pushed to the second queue or else be retained there itself. This depends on the nature of the jobs depending upon the requirement of the jobs for the time duration needed for a complete execution of the job with levels of time quanta divided into the READY QUEUE since the QUEUE itself now has been divided according to the time duration using quanta’s. In a similar fashion, the jobs which could be pushed downwards, should be the job require more I/O Bound Operations, the same should be the procedure, to push back the job upwards that is from Q3 to Q2 and then Q2 to Q1 as per the requirement and the nature of the job (if dynamic!). This means if the job at some point of time remain to be I/O Bound, the job remains in the Q1, if at another point of time duration. the job changes it’s nature from I/O Bound to CPU Bound, it could be pushed downward (lower preference). Similarly, low priority jobs (CPU Bound Jobs) could be pushed upwards by increasing it’s priority as per the dynamic nature of the job (changes from CPU Bound job to I/O Bound Job). Hence Jobs are kept at the queue level according to the nature of the job. These are the variations of the Round Robin Technique.

downtoupq3toq2toq1

The diagram above shows clearly how the jobs are now flexibly aligned in accordance to the time duration required and the dynamic nature of the job. If the jobs are changed from CPU Bound job to the I/O bound, it’d go upwards the direction and if the I/O bound jobs are changed to CPU Bound jobs, it’d move downwards the direction as per the requirement. I/O Bound Jobs have the highest priority since it requires less CPU BURST time duration than the CPU bound Jobs which requires more CPU BURST time duration and hence has a lower priority.

A feedforward is also used, since there is a concept called switch time which is the extra time required by the CPU to change from a queue to another queue. The queue could be switched directly to the appropriate queue without having them to move through the intermediate queue. This means if a job has to be transfer from the Q3 to Q1, it is directly possible but needs switch time and some extra CPU processing. This is demonstrated in the diagram below:

feedforwardWe have concluded over preemptive and non-preemptive CPU Scheduling from the last post and concluded this with Round Robin CPU Scheduling. All the posts which covered process management are:

  1. FCFS (First Come First Served) – Nonpreemptive
  2. SJF (Shortest Job First) – Nonpreemptive
  3. Priority Scheduling – Nonpreemptive
  4. SRT (Shortest Remaining Time First) – Preemptive
  5. Round Robin Scheduling – Preemptive
  6. Multilevel Queue – Preemptive

But all these CPU Scheduling which we have discussed are based on a single CPU. The modern operating systems should not remain satisfied with single processor CPU. There are distributing computing nature which is used in modern operating system be it networked processors among many systems or a single set-up having multiple core processors. We discussed single resource with multiple processes among which the resource is to be shared. What we will see next is a setup wherein there are multiple resource which is to be shared with multiple processes.

DISTRIBUTED COMPUTING

There are models which could go under distributed computing:

  1. WORKSTATION MODEL
  2. PROCESSOR POOL MODEL

Workstation Model could be considered as such every user which has one full fledged computer having it’s own memory, hard-disk, etc but shared on for example a LAN network without which the the computer remains functional. Every user has the processing power.

Processor Pool Model could be considered wherein one can have a high-end server having multiple processors and to the users, there could be high-end terminals (example a graphics terminal), so that the user terminal doesn’t have any processing capabilities. Users does not have processing capabilities, the processing is centralized.

Naturally for these two types of models, the approach taken for CPU sharing must be different. There hence must be two different kind of allocation techniques:

  1. NON-MIGRATORY
  2. MIGRATORY

NON-MIGRATORY to some extend is static in nature. In NON-MIGRATORY CPU allocation, once the process is created, it is decided on which of the processors the process has to be executed; once decided it is fixed and that process is executed on that processor until termination.

MIGRATORY is dynamic wherein the process could be migrated from one processor to another depending upon the requirement. The process itself hence could be terminated in another processor and could be originated or allocated to another processor during initiation of the process.

The next post would cover these aspects of CPU process sharing since this post was dedicated to cover Round Robin Scheduling and Multilevel Queue. Single CPU Management and Process Management hence has been covered in these five posts which could be used for a ready reference:

  1. Operating System Introductory
  2. Operating System  – Importance of an Operating System
  3. Operating System – CPU Management
  4. Operating System – Process State Diagram and Basic CPU Scheduling
  5. Operating System – Round Robin CPU Scheduling and Multilevel Queue

Use the links to keep updated on the Process Management of Operating System, next concurrent processing would be covered. Thank you and I bid good-bye to the followers of the blog. Roger out.