POS Manual
POS Manual
#include <stdio.h>
int mutex=1, full=0,empty=3, x=0;
int main()
{
int n;
void producer1();
void consumer1();
int wait(int);
int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit");
while(1)
{
printf("\nEnter your Choice:");
scanf("%d",&n);
switch(n)
{
case 1: if((mutex==1)&&(empty!=0))
producer1();
else
printf("Butter is full!!");
break;
case 2: if((mutex==1)&&(full!=0))
consumer1();
else
printf("Butter is empty!!");
break;
case 3:
break;
}
}
return 0;
}
int wait(int s)
{
return (--s);
}
int signal(int s)
{
return(++s);
}
void producer1()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nProduer produces the item %d",x);
mutex=signal(mutex);
}
void consumer1()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\nConsumer consumes item %d",x);
x--;
mutex=signal(mutex);
}
Explanation:
1. The code begins with the inclusion of the standard input-output library <stdio.h> and
declares some global variables: mutex, full, empty, and x.
2. The main function initializes variables and enters an infinite loop that presents a menu to
the user for selecting between producer, consumer, or exit options.
3. Inside the loop, the user's choice is taken as input, and the code performs the selected
operation based on the choice using a switch statement.
4. The wait and signal functions are simple semaphore operations. wait decrements the
semaphore value, and signal increments it. These functions are used for synchronization
and mutual exclusion.
5. In the producer1 function, the producer is attempting to produce an item. It first acquires
the mutex semaphore to ensure exclusive access to the shared variables. Then it
updates the semaphores full and empty accordingly, indicating that a new item has been
produced. Finally, it increments the item count x and releases the mutex.
7. Depending on the user's choice, the program either produces an item, consumes an
item, or exits.
Threads:
I) A multithreaded program that determines the summation of a non-negative integer.
#include <stdio.h>
#define MAXSIZE 10
void main()
{
int array [MAXSIZE];
int i, num, negative_sum = 0, positive_sum = 0;
float total = 0.0, average;
printf("Enter the value of N \n");
scanf("%d", &num);
printf("Enter %d numbers(-ve, +ve and zero)\n", num);
for (i = 0; i<num; i++)
{
scanf("%d", &array[i]);
}
printf("Input array elements \n");
for (i = 0; i<num; i++)
{
printf("%+3d\n", array[i]);
}
// summation starts
for (i=0;i <num; i++)
{
if (array[i] < 0)
{
negative_sum = negative_sum + array[i];
}
else if (array[i] > 0)
{
positive_sum = positive_sum +array[i];
}
else if (array[i] == 0)
{
;
}
total = total + array[i];
}
average = total/num;
printf("\n Sum of negative number =%d\n",negative_sum);
printf ("sum of all positive numbers %d\n", positive_sum);
printf ("\n average of all inpute number = %2f\n",average);
}
Explanation:
1. The program starts by including the standard input-output library ( stdio.h) and
defining a constant MAXSIZE as 10 to set the maximum number of elements in
the array.
2. The main function begins, and within it, several variables are declared:
a. array[MAXSIZE]: An integer array to store the input numbers.
b. i: A loop counter.
c. num: The number of elements to be entered by the user.
d. negative_sum and positive_sum: Variables to keep track of the sum of
negative and positive numbers, respectively.
e. total: A variable to accumulate the total sum of all input numbers.
f. average: To store the calculated average.
3. The program prompts the user to input the value of N, which represents the
number of integers they want to input. The user's input is stored in the num
variable.
4. Next, the program prompts the user to input num numbers one by one and stores
them in the array.
5. It then prints the elements of the input array in a formatted manner, showing the
sign of each number with %+3d.
6. The code enters a loop to calculate the sum of negative and positive numbers
and the total sum of all input numbers. It iterates through each element in the
array.
7. Inside the loop, it checks whether each element is negative, positive, or zero and
updates negative_sum, positive_sum, and total accordingly.
8. After the loop, the program calculates the average by dividing the total sum by
the number of elements (num).
9. Finally, it prints the calculated results, including the sum of negative numbers, the
sum of positive numbers, and the average of all input numbers.
II) Write a multithreaded java program that output prime numbers. The program will then
create a separate thread that outputs all the prime number less than or equal to
the number entered by the user.
#include <stdio.h>
int main(void) {
int l, i, temp=0,p,t;
printf("Enter the limit:");
scanf("%d",&l);
int nlist[l];
for(i=2;i<l;i++){
nlist[temp]=i;
temp++;
}
for (i=0; i<temp;i++){
if(nlist[i]!=0){
t=i;
p=nlist[i];
while((t+p)<temp){
t=t+p;
nlist[t]=0;
}
}
}
for(i=0; i<temp;i++){
if(nlist[i]!=0)
printf("%d, ", nlist[i]);
}
}
Explanation:
1. The program starts by including the standard input-output library ( stdio.h) and
defining the main function.
2. Several variables are declared:
a. l: It stores the user-input limit for finding prime numbers.
b. i: A loop counter used for iteration.
c. temp: A variable to keep track of the number of elements in the nlist array.
d. p and t: Temporary variables for processing within the loops.
3. The program prompts the user to enter a limit ( l) up to which they want to find
prime numbers.
4. An integer array nlist of size l is declared to store numbers from 2 to l. It is
initialized in a loop.
5. The code then uses the Sieve of Eratosthenes algorithm to find prime numbers. It
iterates through the nlist array.
6. Inside the loop, it checks if the current element is not marked as 0 ( nlist[i] != 0). If
it's not marked, it's considered a prime number ( p).
7. It then iterates through the array again, starting from the position t, and marks all
multiples of the prime number as 0, effectively eliminating them from the list of
primes.
8. After completing the sieve algorithm, the program goes through the nlist array
once more and prints the prime numbers that are not marked as 0.
9. The program returns 0 to indicate successful execution.
III) The Fibonacci sequence is the series
#include <stdio.h>
int main(void) {
int i, n, t1 = 0, t2 = 1, nextTerm;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fabonacci Series: ");
for(i=1;i<=n;++i){
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}
Explanation:
1. The program starts by including the standard input-output library ( stdio.h) and
defining the main function.
2. Several variables are declared:
a. i: A loop counter used for iteration.
b. n: It stores the user-input number of terms they want in the Fibonacci
series.
c. t1 and t2: Variables to represent the current and next terms in the
Fibonacci series.
d. nextTerm: A variable to calculate the next term in the series.
3. The program prompts the user to enter the number of terms ( n) they want in the
Fibonacci series.
4. It initializes the first two terms of the Fibonacci series ( t1 and t2). The first term is
set to 0, and the second term is set to 1.
5. The program enters a loop to generate and print the Fibonacci series. The loop
runs from 1 to n terms.
6. Inside the loop, it prints the current term (t1) of the Fibonacci series.
7. It then calculates the next term in the series (nextTerm) by adding t1 and t2 and
updates t1 and t2 accordingly. This step is crucial in generating the Fibonacci
sequence, as each term is the sum of the previous two terms.
8. The loop continues until it has printed the specified number of terms ( n) in the
Fibonacci series.
9. Finally, the program returns 0 to indicate successful execution.
Practical 3
Synchronization
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#define NUM_READERS 5
#define NUM_WRITERS 2
#define MAX_ITERATIONS 10
// Reading
printf("Reader %d reads: %d\n", reader_id, shared_resource);
// Writing
shared_resource++;
printf("Writer %d writes: %d\n", writer_id, shared_resource);
int main() {
pthread_t reader_threads[NUM_READERS], writer_threads[NUM_WRITERS];
int reader_args[NUM_READERS], writer_args[NUM_WRITERS];
sem_init(&mutex, 0, 1);
sem_init(&wrt, 0, 1);
// Join threads
for (int i = 0; i < NUM_READERS; i++) {
pthread_join(reader_threads[i], NULL);
}
for (int i = 0; i < NUM_WRITERS; i++) {
pthread_join(writer_threads[i], NULL);
}
sem_destroy(&mutex);
sem_destroy(&wrt);
return 0;
}
Output
Explanation
2. The shared_resource variable represents the resource that both readers and writers are
accessing. The iterations variable keeps track of how many iterations the threads have
performed.
3. We use two semaphores: mutex to control access to reader_count, and wrt to control
access to the shared resource.
4. The reader function simulates a reader thread. It enters a loop where it acquires the
mutex, updates reader_count, and checks if it's the first reader. If it is, it blocks writers by
waiting on the wrt semaphore. It then reads the shared resource, releases the mutex,
simulates reading, and updates the iteration count.
5. The writer function simulates a writer thread. It enters a loop where it acquires the wrt
semaphore, updates the shared resource, releases the wrt semaphore, simulates
writing, and updates the iteration count.
6. In the main function, we initialize the semaphores and create reader and writer threads.
Each thread is assigned a unique ID.
7. After creating the threads, we wait for them to finish using pthread_join.
8. Finally, we destroy the semaphores and return from the main function.
9. The threads continue running until the iterations variable reaches MAX_ITERATIONS. At
that point, each thread calls pthread_exit(NULL), which gracefully terminates the thread.
This ensures that the program does not run indefinitely and provides a controlled
termination after a specified number of iterations.
Practical 4
#include <stdio.h>
currentTime += processes[i].burst;
}
}
int main() {
int n;
Output
Explanation
1. We define a Process structure to represent each process, including its process ID (pid),
arrival time (arrival), and burst time (burst).
3. In the main function, we take user input for the number of processes and details of each
process, including arrival time and burst time.
4. After taking input, we call the fcfsScheduling function to perform FCFS scheduling and
display the waiting time and turnaround time for each process.
Practical 5
#include <stdio.h>
#include <stdbool.h>
printf("Time\tProcess\n");
if (shortestProcessIndex == -1) {
// No eligible process is available; increment time
currentTime++;
} else {
// Execute the selected process for one time unit
processes[shortestProcessIndex].remaining--;
printf("%d\tP%d\n", currentTime, processes[shortestProcessIndex].pid);
if (processes[shortestProcessIndex].remaining == 0) {
// Process has completed
processes[shortestProcessIndex].completed = true;
completedProcesses++;
}
currentTime++;
}
}
}
int main() {
int n;
return 0;
}
Output
In this program:
1. We define a Process structure to represent each process, including its process ID (pid),
arrival time (arrival), burst time (burst), remaining burst time (remaining), and a flag
(completed) to track if the process has completed.
3. In the main function, we take user input for the number of processes and details of each
process, including arrival time and burst time.
5. Compile and run the program, and it will perform preemptive SJF scheduling for the
given set of processes.
Practical 6
C program that implements the Round-Robin (RR) scheduling algorithm with preemptive
behavior. This program takes user input for the number of processes, their arrival times,
burst times, and the time quantum.
#include <stdio.h>
#include <stdbool.h>
printf("Time\tProcess\n");
processes[i].remaining -= execution_time;
printf("%d\tP%d\n", currentTime, processes[i].pid);
if (processes[i].remaining == 0) {
processes[i].completed = true;
completedProcesses++;
}
currentTime += execution_time;
}
}
}
}
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct Process processes[n];
int time_quantum;
printf("Enter the time quantum: ");
scanf("%d", &time_quantum);
return 0;
}
Output
This program is quite similar to the preemptive SJF scheduling program you provided, with
adjustments made to implement the Round-Robin scheduling algorithm. It takes user input for
process details and the time quantum, then simulates the execution of processes using the RR
scheduling algorithm and displays the timeline of process execution.
Practical 7
#include <stdio.h>
#include <stdbool.h>
printf("Time\tProcess\n");
if (highestPriorityIndex == -1) {
// No eligible process is available; increment time
currentTime++;
} else {
// Execute the selected process for one time unit
processes[highestPriorityIndex].remaining--;
printf("%d\tP%d\n", currentTime, processes[highestPriorityIndex].pid);
if (processes[highestPriorityIndex].remaining == 0) {
// Process has completed
processes[highestPriorityIndex].completed = true;
completedProcesses++;
}
currentTime++;
}
}
}
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct Process processes[n];
return 0;
}
Output
This program allows you to enter the number of processes, their arrival times, burst times, and
priorities. It then simulates the execution of processes based on the preemptive Priority
Scheduling algorithm and displays the timeline of process execution.
5. If there's no eligible process available at the current time, we increment the currentTime.
This is because no processes have arrived or all eligible processes have been
completed.
6. If we find an eligible process with the highest priority, we execute this process for one
time unit by decrementing its remaining burst time. We also print the process's ID and
the current time.
7. If the process's remaining burst time becomes 0, it means the process has completed,
so we mark it as completed and increment completedProcesses.
9. In the main function, we take user input for the number of processes, their arrival times,
burst times, and priorities.
11. This code simulates the Priority Scheduling algorithm with a preemptive approach and
allows the user to input the necessary process details.
Practical 8
C program that implements the Shortest Remaining Time First (SRTF) scheduling
algorithm, which is also known as Shortest Job First (SJF) with preemption. This
program takes user input for the number of processes, their arrival times, burst times,
and then simulates the SRTF scheduling algorithm
#include <stdio.h>
#include <stdbool.h>
printf("Time\tProcess\n");
if (shortestProcessIndex == -1) {
// No eligible process is available; increment time
currentTime++;
} else {
// Execute the selected process for one time unit
processes[shortestProcessIndex].remaining--;
printf("%d\tP%d\n", currentTime, processes[shortestProcessIndex].pid);
if (processes[shortestProcessIndex].remaining == 0) {
// Process has completed
processes[shortestProcessIndex].completed = true;
completedProcesses++;
}
currentTime++;
}
}
}
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct Process processes[n];
return 0;
}
Output
1. The code structure is similar to the previous code examples, with the main difference
being the SRTF (Shortest Remaining Time First) scheduling logic.
3. The SRTFScheduling function performs the Shortest Remaining Time First scheduling.
This function takes an array of processes and the number of processes as parameters.
5. Inside the scheduling loop, we search for the process with the shortest remaining burst
time among those that have arrived and are not completed. We also keep track of the
index of this process.
6. If there are no eligible processes available at the current time, we increment the
currentTime. This is because no processes have arrived or all eligible processes have
been completed.
7. If we find an eligible process with the shortest remaining burst time, we execute this
process for one time unit by decrementing its remaining burst time. We also print the
process's ID and the current time.
8. If the process's remaining burst time becomes 0, it means the process has completed,
so we mark it as completed and increment completedProcesses.
10. In the main function, we take user input for the number of processes, their arrival times,
burst times, and execute the SRTF scheduling by calling the SRTFScheduling function.
11. This code simulates the Shortest Remaining Time First (SRTF) scheduling algorithm,
where the process with the shortest remaining burst time is selected for execution at
each step