osslip24
Q1
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j+1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void insertionSort(int arr[], int n) {
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
// Move elements of arr[0..i-1] that are greater than key to one position ahead of their current position
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
int main() {
int n;
// Input the number of elements
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
// Input the elements
printf("Enter %d integers:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Create a child process
pid_t pid = fork();
if (pid == -1) {
perror("Fork failed");
exit(EXIT_FAILURE);
}
if (pid > 0) {
// Parent process (bubble sort)
printf("Parent process (PID: %d) sorting using bubble sort.\n", getpid());
bubbleSort(arr, n);
printf("Parent process sorted the array.\n");
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
// Wait for the child process to complete
wait(NULL);
} else if (pid == 0) {
// Child process (insertion sort)
printf("Child process (PID: %d) sorting using insertion sort.\n", getpid());
insertionSort(arr, n);
printf("Child process sorted the array.\n");
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
return 0;
}
/*Write a programto implement the toy shell. It should display the command
prompt “myshell$”. Tokenize the command line and execute the given
command by creating the child process. Additionally it should interpret the
following commands.
count c filename :- To print number of characters in the file.
count w filename :- To print number of words in the file.
count l filename :- To print number of lines in the file. */
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h> // open,read(),close() : file related function
void count(char p1,charfname)
{
int handle,ccnt=0,wcnt=0,lcnt=0;
char ch;
handle=open(fname,O_RDONLY);
if(handle==-1)
{
printf("\n File %s Not Found....",fname);
}
else
{
while(read(handle,&ch,1)) //on failure read() function returns 0
{
if(ch==' '||ch=='\t')
wcnt++;
else if(ch=='\n')
{
lcnt++;
wcnt++;
}
ccnt++;
}
close(handle);
if(strcmp(p1,"C")==0)
{
printf("\nTotal No. of character in file = %d",ccnt);
}
else if(strcmp(p1,"W")==0)
{
printf("\nTotal No. of words in file = %d",wcnt);
}
else if(strcmp(p1,"L")==0)
{
printf("\nTotal No. of Lines in file = %d",lcnt);
}
else
{
printf("\nInvalid Option......\n");
}
}
}
int main()
{
char cmd[80],tok1[10],tok2[10],tok3[10],tok4[10];
int n;
while(1)
{
printf("\nMYSHELL $]");
fgets(cmd,80,stdin);
n=sscanf(cmd,"%s%s%s%s",tok1,tok2,tok3,tok4); //n=no of tokens are formed from given command
switch(n)
{
case 1:
if(fork()==0)
{
execlp(tok1,tok1,NULL); //paramenters- nameOfProcess,parametersOfProcess
}
wait(0);
break;
case 2 :
if(fork()==0)
{
execlp(tok1,tok1,tok2,NULL);
}
wait(0);
break;
case 3:
if(strcmp(tok1,"count")==0)
{
count(tok2,tok3);
}
else
{
if(fork()==0)
{
execlp(tok1,tok1,tok2,tok3,NULL);
}
wait(0);
}
break;
case 4 :
if(fork()==0)
{
execlp(tok1,tok1,tok2,tok3,tok4,NULL);
}
wait(0);
break;
}
}
}