CS Practical
CS Practical
roll-no:0407
c.s practical
question 1:Execute various LINUX commands for
(ii) File Management: cat, cp, rm, mv, cmp, comm, diff, find, grep, awk
ANS:Information Maintenance
File Management
Command:
Process Control
ps
ps-aux
killfork()
#include <stdio.h>
#include <unistd.h>
int main() {
if (pid == 0) {
} else {
perror("Fork failed");
return 0;
getpid
#include <stdio.h>
#include <unistd.h>
int main() {
printf("Current process ID is: %d\n", getpid());
return 0;
The kill command sends a signal to a process. By default, it sends the SIGTERM signal,
Example:
sleep
The sleep command pauses the execution for a specified number of seconds.
Example:
Communication:
1. Input-Output Redirection
Example:
Example:
Example:
2. Pipe()
Pipes allow the output of one command to be used as the input for another command.
Example:
This will list all processes, then filter for processes containing the word "python."
Protection Management
1. chmod
Example:
chmod 755 myfile.txt # rwx for owner, rx for group and others
2. chown
Example:
chown user:group myfile.txt
Example:
QUESTION 3.Write a program (using fork() and/or exec() commands) were parent and
child execute
#include <stdio.h>
#include <unistd.h>
int main() {
if (pid == 0) {
// Child process
// Parent process
perror("Fork failed");
return 0;
#include <stdio.h>
#include <unistd.h>
int main() {
if (pid == 0) {
// Child process
// Parent process
} else {
perror("Fork failed");
return 0;
}
(iii) before terminating the parent waits for the child to finish its task
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
if (pid == 0) {
// Child process
// Parent process
} else {
perror("Fork failed");
}
return 0;
QUESTION4. Write a program to report behaviour of Linux kernel including kernel version
CPU type and CPU information.
QUESTION 5: Write a program to report behaviour of Linux kernel including information o
configured memory amount of free and used memory.
#include <stdio.h>
#include <stdlib.h>
void report_memory_info() {
if (file == NULL) {
exit(EXIT_FAILURE);
}
char line[256];
// Read the file line by line and extract the memory details
continue;
continue;
break;
fclose(file);
printf("----------------------------\n");
printf("----------------------------\n");
int main() {
report_memory_info();
return 0;
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
char buffer[BUFFER_SIZE];
if (source_fd < 0) {
return;
if (dest_fd < 0) {
close(source_fd);
return;
if (bytes_written != bytes_read) {
close(source_fd);
close(dest_fd);
return;
if (bytes_read < 0) {
close(source_fd);
close(dest_fd);
if (bytes_read >= 0) {
if (argc != 3) {
return 1;
copy_file(source, destination);
return 0;
#include <stdio.h>
typedef struct {
} Process;
int main() {
int n, i;
Process processes[10];
scanf("%d", &n);
processes[i].processID = i + 1;
printf("Enter arrival time and burst time for process %d: ", i + 1);
int currentTime = 0;
for (i = 0; i < n; i++) {
currentTime = processes[i].arrivalTime;
currentTime = processes[i].completionTime;
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n",
processes[i].processID,
processes[i].arrivalTime,
processes[i].burstTime,
processes[i].completionTime,
processes[i].turnAroundTime,
processes[i].waitingTime);
}
float totalTAT = 0, totalWT = 0;
totalTAT += processes[i].turnAroundTime;
totalWT += processes[i].waitingTime;
return 0;
#include <stdio.h>
typedef struct {
int processID; // Process ID
} Process;
// Function to sort the processes based on burst time (Shortest Job First)
Process temp;
temp = processes[i];
processes[i] = processes[j];
processes[j] = temp;
}
// Function to calculate SJF scheduling
int currentTime = 0;
sortProcessesByBurstTime(processes, n);
processes[i].turnAroundTime = processes[i].completionTime -
processes[i].arrivalTime;
currentTime = processes[i].completionTime;
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n",
processes[i].processID,
processes[i].arrivalTime,
processes[i].burstTime,
processes[i].completionTime,
processes[i].turnAroundTime,
processes[i].waitingTime);
totalTAT += processes[i].turnAroundTime;
totalWT += processes[i].waitingTime;
int main() {
int n;
// Get the number of processes
scanf("%d", &n);
Process processes[n];
// Get the arrival time and burst time for each process
processes[i].processID = i + 1;
printf("Enter arrival time and burst time for process %d: ", i + 1);
sjfScheduling(processes, n);
printProcessDetails(processes, n);
return 0;
}
QUESTION 9: Write a program to implement non-preemptive priority-based scheduling
algorithm
#include <stdio.h>
typedef struct {
} Process;
Process temp;
// Swap if the priority of the current process is greater than the next process
temp = processes[i];
processes[i] = processes[j];
processes[j] = temp;
int currentTime = 0;
sortProcessesByPriority(processes, n);
currentTime = processes[i].arrivalTime;
processes[i].turnAroundTime = processes[i].completionTime -
processes[i].arrivalTime;
currentTime = processes[i].completionTime;
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n",
processes[i].processID,
processes[i].arrivalTime,
processes[i].burstTime,
processes[i].priority,
processes[i].completionTime,
processes[i].turnAroundTime,
processes[i].waitingTime);
totalTAT += processes[i].turnAroundTime;
totalWT += processes[i].waitingTime;
int main() {
int n;
scanf("%d", &n);
Process processes[n];
// Get the arrival time, burst time, and priority for each process
for (int i = 0; i < n; i++) {
processes[i].processID = i + 1;
printf("Enter arrival time, burst time, and priority for process %d: ", i + 1);
priorityScheduling(processes, n);
printProcessDetails(processes, n);
return 0;
#include <stdio.h>
#include <limits.h>
typedef struct {
} Process;
int is_process_available = 0;
while (completed != n) {
// Find the process with the shortest remaining time at the current time
min_remaining_time = processes[i].remaining;
shortest = i;
is_process_available = 1;
if (!is_process_available) {
time++;
continue;
processes[shortest].remaining--;
min_remaining_time = processes[shortest].remaining;
if (processes[shortest].remaining == 0) {
// Process completed
processes[shortest].completion = time + 1;
processes[shortest].turnaround = processes[shortest].completion -
processes[shortest].arrival;
processes[shortest].waiting = processes[shortest].turnaround -
processes[shortest].burst;
completed++;
shortest = -1;
min_remaining_time = INT_MAX;
time++;
is_process_available = 0;
printf("\nProcess\tArrival\tBurst\tCompletion\tTurnaround\tWaiting\n");
total_turnaround += processes[i].turnaround;
total_waiting += processes[i].waiting;
printf("%d\t%d\t%d\t%d\t\t%d\t\t%d\n",
processes[i].pid,
processes[i].arrival,
processes[i].burst,
processes[i].completion,
processes[i].turnaround,
processes[i].waiting);
int main() {
int n;
scanf("%d", &n);
Process processes[n];
processes[i].pid = i + 1;
scanf("%d", &processes[i].arrival);
scanf("%d", &processes[i].burst);
}
srtf(processes, n);
display_process_table(processes, n);
return 0;
QUESTION 11: Write a program to calculate sum of n numbers using Pthreads. A list
of n numbers is divided into two smaller list of equal size, two separate threads are used to
sumthe sub lists.
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
typedef struct {
} ThreadData;
data->sum = 0;
data->sum += data->array[i];
return NULL;
int main() {
int n;
if (n <= 0) {
return 1;
if (!numbers) {
return 1;
scanf("%d", &numbers[i]);
int mid = n / 2;
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
free(numbers);
return 0;
}
QUESTION 12 Write a program to implement first fit best , best fit and worst fit
allocation strategies.
#include <stdio.h>
#define MAX 10
int allocation[n];
allocation[i] = -1;
}
// Iterate over all processes
allocation[i] = j;
blockSize[j] -= processSize[i];
break;
if (allocation[i] != -1) {
} else {
}
}
int allocation[n];
allocation[i] = -1;
bestIdx = j;
}
}
if (bestIdx != -1) {
allocation[i] = bestIdx;
blockSize[bestIdx] -= processSize[i];
if (allocation[i] != -1) {
} else {
allocation[i] = -1;
worstIdx = j;
if (worstIdx != -1) {
allocation[i] = worstIdx;
blockSize[worstIdx] -= processSize[i];
}
if (allocation[i] != -1) {
} else {
int main() {
int m, n;
scanf("%d", &m);
printf("Enter the number of processes: ");
scanf("%d", &n);
scanf("%d", &blockSize[i]);
scanf("%d", &processSize[i]);
return 0;