[go: up one dir, main page]

100% found this document useful (1 vote)
1K views18 pages

Os Ass-1 PDF

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 18

SWE3001-OPERATING SYSTEMS

ASSESSMENT-1

Name:R.DINESH
REG NO:18MIS0065
FACULTY:KUMAR P J
SLOT:G1+TG1
a) Shell Programming

Handling the command line arguments

Code:
for i in "$@"
do
echo Argument: $i
done
echo
echo "Done by:)"
echo 18MIS0065
echo Dinesh R

Output:

String Reversal

Code:
#!/bin/bash

read -p "Enter string:" string

len=${#string}

for (( i=$len-1; i>=0; i-- ))

do

reverse="$reverse${string:$i:1}"
done

echo "$reverse"
echo
echo "Done by :)"
echo 18MIS0065
echo Dinesh R

Output:

If-Else, Nested If Else

Code:

#!/bin/bash

echo "Balance = $ 1000"

echo "Enter the Amount You want to Withdraw."

read a

if [ $a -gt 1000 ]

then

echo "Insufficient Balance!"


else
if [ $a -gt 950 ]

then
echo "Warning! Keep $ 50 in your account to keep it active!"
fi

fi
echo "*****************"
echo "18MIS0065"
echo Dinesh R

Output:

Switch cases in shell

echo “Enter a number”


read num
case $num in
[0-9])
echo “you have entered a single digit number”
;;
[1-9][1-9])
echo “you have entered a two-digit number”
;;
[1-9][1-9][1-9])
echo “you have entered a three-digit number”
;;
*)
echo “your entry does not match any of the conditions”
;;
esac
echo
echo 18MIS0065-R.Dinesh
output:

a) Parent child process creation using fork( ) and exec() system call
Checking the Process Identifier
Assigning new task to child
Providing the path name and program name to exec()
Synchronizing Parent and child process using wait()#include <stdio.h>

#include <unistd.h>

#include <sys/types.h>

#include <errno.h>

#include <stdio.h>

#include <sys/wait.h>

#include <stdlib.h>

int global;

int main()

pid_t child_pid;

int status;

int local = 0;

child_pid = fork();
printf("\n************18MIS0065-Dinesh R**************");

printf("\n");

if (child_pid >= 0) /* fork succeeded */

if (child_pid == 0) /* fork() returns 0 for the child process */

printf("child process!\n");

local++;

global++;

printf("child PID = %d, parent pid = %d\n", getpid(), getppid());

printf("\n child's local = %d, child's global = %d\n",local,global);

char *cmd[] = {"whoami",(char*)0};

return execv("/usr/bin/",cmd);

else /* parent process */

printf("parent process!\n");

printf("parent PID = %d, child pid = %d\n", getpid(), child_pid);

wait(&status); /* wait for child to exit, and store child's exit status */

printf("Child exit code: %d\n", WEXITSTATUS(status));

//The change in local and global variable in child process should not reflect here in paren
printf("\n Parent'z local = %d, parent's global = %d\n",local,global);

printf("Parent says bye!\n");

exit(0); /* parent exits */

}
}

else /* failure */

perror("fork");

exit(0);

Output:

b) Process and Thread Management


Write a program to create a thread and perform the following (Easy)
• Create a thread runner function
• Set the thread attributes
• Join the parent and thread
• Wait for the thread to complete

Code:
#include<stdio.h>
#include<pthread.h>
#include<tgmath.h>
void *factorial(void *p);
void *fibb(void *p);
int fact(int n);
int main(){
printf("\n******18MIS0065-Dinesh R\n");
pthread_t tid1;
pthread_t tid2;
pthread_attr_t attr; // set of thread attributes

//get the default attributes


pthread_attr_init(&attr);
pthread_create(&tid1,&attr,factorial,NULL);
pthread_join(tid1,NULL);
pthread_create(&tid2,&attr,fibb,NULL);
pthread_join(tid2,NULL);

}
int fact(int n){
if(n==0 || n==1)
return 1;
else
return n*fact(n-1);
}
void *factorial(void *p){
int i,num1;
printf("Thread 1 (factorial) : ");
printf("Enter Number: ");
scanf("%d",&num1);
printf("Factorial is: %d\n",fact(num1));
pthread_exit(0);

}
void *fibb(void *p){
int i,num1,fib[1000];
printf("Thread 2 (fibonacci) : ");
printf("\nEnter the Nth index of series: ");
scanf("%d",&num1);
fib[0]=0;
fib[1]=1;
for(i=2;i<num1;i++)
fib[i]=fib[i-1]+fib[i-2];
printf("\nThe %dth term of series is : %d \n",num1,fib[num1-1]);
pthread_exit(0);

Output:

d) Write a program to create a thread to find the factorial of a natural number ‘n’. (Medium)

#include<stdio.h>
#include<pthread.h>
#include<tgmath.h>
void *factorial(void *p);
int fact(int n);
int main(){
pthread_t tid1;
pthread_attr_t attr; // set of thread attributes

//get the default attributes


pthread_attr_init(&attr);
pthread_create(&tid1,&attr,factorial,NULL);
pthread_join(tid1,NULL);

}
int fact(int n){
if(n==0 || n==1)
return 1;
else
return n*fact(n-1);
}
void *factorial(void *p){
int i,num1;
printf("Thread 1 (factorial) : ");
printf("Enter Number: ");
scanf("%d",&num1);
printf("Factorial is: %d\n",fact(num1));
printf("\n*********18MIS0065*******");
printf("\nDinesh R\n");
pthread_exit(0);

Output:

d)Assume that two processes named client and server running in the system. It is required that
these two processes should communicate with each other using shared memory concept. The
server writes alphabets from a..z to the shared memory .the client should read the alphabets from
the shared memory and convert it to A…Z. Write a program to demonstrate the above
mentioned scenario. (Medium)

Client Code:
#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/shm.h>
#include <stdio.h>

#include<stdlib.h>

#define SHMSZ 27

main()

int shmid,i;

key_t key;

char *shm, *s;


printf("\n18MIS0065-R.Dinesh");
printf("\nClient Running\n");

key = 5678;

if ((shmid = shmget(key, SHMSZ, 0666)) < 0) {

perror("shmget");

exit(1);

if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {

perror("shmat");

exit(1);

for (s = shm; *s!=NULL; s++)

putchar(*s);

putchar('\n');

for(i=65;i<=90;i++){

*shm = (char)i;
shm++;

exit(0);

Output:

Server Code:
#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/shm.h>

#include <stdio.h>

#define SHMSZ 27

main()

char c;

int shmid;
key_t key;

char *shm, *s;


printf("\n18MIS0065-R.Dinesh");
printf("\nServer Running\n");
/*
*
* * We'll name our shared memory segment
*
* * "5678".
*
* */

key = 5678;

/*
*
* * Create the segment.
*
* */

if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {

perror("shmget");

exit(1);

/*
*
* * Now we attach the segment to our data space.
*
* */

if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {

perror("shmat");

exit(1);

/*
*
* * Now put some things into the memory for the
*
* * other process to read.
*
* */

s = shm;

for (c = 'a'; c <= 'z'; c++)

*s++ = c;

*s = NULL;

/*
*
* * Finally, we wait until the other process
*
* * changes the first character of our memory
*
* * to '*', indicating that it has read what
*
* * we put there.
*
* */

while (*shm != '*')

sleep(1);

exit(0);

Output:
e)The Collatz conjecture concerns what happens when we take any positive integer n and apply
the following algorithm: n = n/2, if n is even n = 3 × n + 1, if n is odd The conjecture states that
when this algorithm is continually applied, all positive integers will eventually reach 1. For
example, if n = 35, the sequence is 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1.Write a C
program using the fork () system call that generates this sequence in the child process. The
starting number will be provided from the command line. For example, if 8 is passed as a
parameter on the Command line, the child process will output 8, 4, 2, 1. Because the parent and
child processes have their own copies of the data, it will be necessary for the child to output the
sequence. Have the parent invoke the wait () call to wait for the child process to complete before
exiting the program (High).

Code:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
printf(" \n\nWelcome \n");
printf("\n*****18MIS0065-Dinesh R\n");
int n=0; int k=0; pid_t pid; do
{
printf("Please enter a number greater than 0 to run the CollatzConjecture.\n");
scanf("%d", &k); }
while (k <= 0);
pid = fork();
if (pid == 0)
{
printf("Child is working... \n");
printf("%d\n",k); while (k!=1)
{
if (k%2 == 0)
{ k = k/2;
}
else if (k%2 == 1)
{
k = 3 * (k) + 1;
}
printf("%d \n",k);
}
printf("Child process is done.\n");
}
else
{
printf("Parents is waiting on child process...\n");
wait();
printf("Parent process is done.\n");
}
return 0;
}

Output:

g) Write a multithreaded program that calculates various statistical values for a list of numbers.
This program will be passed a series of numbers on the command line and will then create three
separate worker threads. One thread will determine the average of the numbers, the second will
determine the maximum value, and the third will determine the minimum value. For example,
suppose your program is passed the integers 90 81 78 95 79 72 85 , the program will report the
average value as 82. The minimum value as 72. The maximum value as 95. The variables
representing the average, minimum, and maximum values will be stored globally. The worker
threads will set these values, and the parent thread will output the values once the workers have
exited. (High)

Code:
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
int arr[50],n,i;

void *th()
{
int sum=0;
float average;
printf("\n*****18MIS0065-Dinesh R\n");
printf("enter your number :=");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++)
{
sum=sum+arr[i];
}
average=sum/n;
printf("The average value is:%f",average);
}
void *th1()
{

int temp=arr[0];
for(i=1;i<n;i++)
{
if(temp>arr[i])
{
temp=arr[i];
}
}
printf("\nThe Minimum value is:=%d",temp);

}
void *th2()
{

int temp=arr[0];
for(i=1;i<n;i++)
{
if(temp<arr[i])
{
temp=arr[i];
}
}
printf("\nThe Maximum value is:=%d",temp);
}
int main()
{
int n,i;
pthread_t t1;
pthread_t t2;
pthread_t t3;
n=pthread_create(&t1,NULL,&th,NULL);
pthread_join(t1,NULL);
//printf("\n done and my value is %d",n);
n=pthread_create(&t2,NULL,&th1,NULL);
pthread_join(t2,NULL);
//printf("\n done and my value is %d",n);
n=pthread_create(&t3,NULL,&th2,NULL);
pthread_join(t3,NULL);
//printf("\n done and my value is %d",n);

Output:

You might also like