Os Ass-1 PDF
Os Ass-1 PDF
Os Ass-1 PDF
ASSESSMENT-1
Name:R.DINESH
REG NO:18MIS0065
FACULTY:KUMAR P J
SLOT:G1+TG1
a) Shell Programming
Code:
for i in "$@"
do
echo Argument: $i
done
echo
echo "Done by:)"
echo 18MIS0065
echo Dinesh R
Output:
String Reversal
Code:
#!/bin/bash
len=${#string}
do
reverse="$reverse${string:$i:1}"
done
echo "$reverse"
echo
echo "Done by :)"
echo 18MIS0065
echo Dinesh R
Output:
Code:
#!/bin/bash
read a
if [ $a -gt 1000 ]
then
then
echo "Warning! Keep $ 50 in your account to keep it active!"
fi
fi
echo "*****************"
echo "18MIS0065"
echo Dinesh R
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");
printf("child process!\n");
local++;
global++;
return execv("/usr/bin/",cmd);
printf("parent process!\n");
wait(&status); /* wait for child to exit, and store child's exit 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);
}
}
else /* failure */
perror("fork");
exit(0);
Output:
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
}
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
}
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;
key = 5678;
perror("shmget");
exit(1);
perror("shmat");
exit(1);
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;
key = 5678;
/*
*
* * Create the segment.
*
* */
perror("shmget");
exit(1);
/*
*
* * Now we attach the segment to our data space.
*
* */
perror("shmat");
exit(1);
/*
*
* * Now put some things into the memory for the
*
* * other process to read.
*
* */
s = shm;
*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.
*
* */
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: