0% found this document useful (0 votes)
8 views14 pages

Os Expt

The document contains a series of C programs and shell scripts demonstrating various system calls and concepts in operating systems, including file operations, process management, memory management, and scheduling algorithms. It covers topics such as opening and reading files, displaying system processes, implementing the producer-consumer problem, and using the Banker's algorithm for resource allocation. Additionally, it includes examples of FIFO page replacement and memory allocation strategies.

Uploaded by

tushartiwari1648
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views14 pages

Os Expt

The document contains a series of C programs and shell scripts demonstrating various system calls and concepts in operating systems, including file operations, process management, memory management, and scheduling algorithms. It covers topics such as opening and reading files, displaying system processes, implementing the producer-consumer problem, and using the Banker's algorithm for resource allocation. Additionally, it includes examples of FIFO page replacement and memory allocation strategies.

Uploaded by

tushartiwari1648
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

SYSTEM CALLS FOR OUTPUT USE

gcc .o open file_name

./operation

1A.OPEN C:

#include<stdio.h>

#include<fcntl.h>

#include<errno.h>

extern int errno;

int main()

int fd = open("os1.txt", O_RDONLY | O_CREAT);

printf("fd = %d \n ", fd);

if(fd == -1)

printf("Error Number %d \n", errno);

perror("Program");

return 0;

1B.READ C:

#include<unistd.h>

int main()

char buff[20];

read(0,buff,10);

write(1,buff,10);

}
1A.WRITE C:

#include<unistd.h>

int main()

write(1,"HELLO BUDDY\n", 12);

1B.CLOSE C:

#include<stdio.h>

#include<fcntl.h>

#include<errno.h>

extern int errno;

int main()

int fd = open("os2.txt", O_RDONLY | O_CREAT);

printf("fd = %d \n ", fd);

if(fd == -1)

printf("Error Number %d \n", errno);

perror("Program");

return 0;

2A.DISPLAYING TOP 10 PROCESSES IN DESENDING ORDER

FOR OUTPUT USE sh os.sh

echo "TOP 10 PROCESSES IN DECENDING ORDER IS :";

ps axl | head -n 10
2B.DISPLAYING PROCESSES WITH HIGHEST MEMOEY USAGE

FOR OUTPUT USE sh mem.sh

echo "DISPLAY PROCESSES WITH HIGHEST MEMORY USAGE:";

ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head:

2C.DISPLAY CURRENT LOGGED USER AND LOGIN NAME

FOR OUTPUT USE sh current.sh

echo"LOGGED IN USERS ARE:-\n";

who -u

echo"NUMBER OF LOGGED IN USERS ARE :-\n";

who -u | wc-

3A.DISPLAY CURRENT SHELL,HOME DIRECTORY,OPERATING SYSTEM

TYPE,CURRENT PATH SETTING AND CURRENT WORKING DIRECTORY

FOR OUTPUT USE w

echo"CURRENT CURRENT HOME DIRECTORY IS:";

whoami

echo"CURRENT OPERATING SYSTEM TYPE IS:";

uname

echo"CURRENT WORKING DIRECTORY IS:";

pwd

3B.DISPLAY OS NAME,RELEASE NUMBER,KERNEL VERSION

FOR OUTPUT USE sh .sh

echo"LOGGED IN USERS ARE:-\n";

who -u

echo"NUMBER OF LOGGED IN USERS ARE :-\n";

who -u | wc-
4.FCFS

FOR OUTPUT USE gcc fcfs.c

#include<stdio.h>

int main()

int n,bt[30],wait_t[30],turn_ar_t[30],av_wt_t=0,avturn_ar_t=0,i,j;

printf("Please enter the total number of processes(maximum 30):"); // the maximum

process that be used to calculate is specified.

scanf("%d",&n);

printf("\nEnter The Process Burst Timen");

for(i=0;i<n;i++)

printf("P[%d]:",i+1);

scanf("%d",&bt[i]);

wait_t[0]=0;

for(i=1;i<n;i++)

wait_t[i]=0;

for(j=0;j<i;j++)

wait_t[i]+=bt[j];

printf("\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time");

for(i=0;i<n;i++)

turn_ar_t[i]=bt[i]+wait_t[i];

av_wt_t+=wait_t[i];

avturn_ar_t+=turn_ar_t[i];
printf("\nP[%d]\t\t%d\t\t\t%d\t\t\t\t%d",i+1,bt[i],wait_t[i],turn_ar_t[i]);

av_wt_t/=i;

avturn_ar_t/=i;

printf("\nAverage Waiting Time:%d",av_wt_t);

printf("\nAverage Turnaround Time:%d",avturn_ar_t);

return 0;

5.CREATE CHILD PROCESS IN LINUX USING FORK SYSTEM CALLS

FOR OUTPUT USE gcc fork2/3.c

./a.out

Program For Fork2

#include <stdio.h>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/wait.h>

#include <unistd.h>

int main(void) {

pid_t pid = fork();

if(pid == 0) {

printf("Child => PPID: %d PID: %d\n", getppid(), getpid());

exit(EXIT_SUCCESS);

else if(pid > 0) {

printf("Parent => PID: %d\n", getpid());

printf("Waiting for child process to finish.\n");

wait(NULL);

printf("Child process finished.\n");


}

else {

printf("Unable to create child process.\n");

return EXIT_SUCCESS;

Program For Fork3

#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>

int main()

// make two process which run same

// program after this instruction

pid_t p = fork();

if(p<0){

perror("fork fail");

exit(1);

printf("Hello world!, process_id(pid) = %d \n",getpid());

return 0;

}
6.PRODUCER CONSUMER

FOR OUTPUT USE gcc filename.c

./a.out

#include <stdio.h>

#include <stdlib.h>

int mutex = 1;

int full = 0;

int empty = 10, data = 0;

void producer()

--mutex;

++full;

--empty;

data++;

printf("\nProducer produces item number: %d\n", data);

++mutex;

void consumer()

--mutex;

--full;

++empty;

printf("\nConsumer consumes item number: %d.\n", data);

data--;

++mutex;

int main()

{
int n, i;

printf("\n1. Enter 1 for Producer"

"\n2. Enter 2 for Consumer"

"\n3. Enter 3 to Exit");

for (i = 1; i > 0; i++)

printf("\nEnter your choice: ");

scanf("%d", &n);

switch (n)

case 1:

if ((mutex == 1) && (empty != 0))

producer();

else

printf("The Buffer is full. New data cannot be produced!");

break;

case 2:

if ((mutex == 1) && (full != 0))

consumer();

else

{
printf("The Buffer is empty! New data cannot be consumed!");

break;

case 3:

exit(0);

break;

7.BANKER’s ALGORITHM

FOR OUTPUT USE gcc banker.c

./a.out

#include <stdio.h>

int main() {

/* array will store at most 5 processes with 3 resources

if your process or resources are greater than 5 and 3, then increase the size of the array */

int p, c, count = 0, i, j, alc[5][3], max[5][3], need[5][3], safe[5], available[3], done[5],


terminate = 0;

printf("Enter the number of processes and resources: ");

scanf("%d %d", &p, &c);

// Validate input size

if (p > 5 || c > 3) {

printf("Error: Maximum processes = 5, Maximum resources = 3. Adjust array size


accordingly.\n");

return 1;

// Input allocation matrix

printf("Enter allocation of resources for all processes (%dx%d matrix):\n", p, c);

for (i = 0; i < p; i++) {


for (j = 0; j < c; j++) {

scanf("%d", &alc[i][j]);

// Input max resource requirement matrix

printf("Enter the max resources required for all processes (%dx%d matrix):\n", p, c);

for (i = 0; i < p; i++) {

for (j = 0; j < c; j++) {

scanf("%d", &max[i][j]);

// Input available resources

printf("Enter the available resources (%d values):\n", c);

for (i = 0; i < c; i++) {

scanf("%d", &available[i]);

// Calculate the need matrix

printf("\nNeed resources matrix:\n");

for (i = 0; i < p; i++) {

for (j = 0; j < c; j++) {

need[i][j] = max[i][j] - alc[i][j];

printf("%d\t", need[i][j]);

printf("\n");

// Initialize done array

for (i = 0; i < p; i++) {

done[i] = 0;
}

return 0;

8.MVT
FOR OUTPUT USE gcc mvt.c

./a.out

#include <stdio.h>

int main() {

int ms, mp[10], i, temp, n = 0;

char ch = 'y';

// Input total memory available

printf("\nEnter the total memory available (in Bytes): ");

scanf("%d", &ms);

temp = ms;

for (i = 0; ch == 'y' || ch == 'Y'; i++, n++) {

printf("\nEnter memory required for process %d (in Bytes): ", i + 1);

scanf("%d", &mp[i]);

if (mp[i] <= temp) {

printf("\nMemory is allocated for Process %d", i + 1);

temp -= mp[i]; // Reduce available memory

} else {

printf("\nMemory is Full, Process %d cannot be allocated.", i + 1);

break;

printf("\nDo you want to continue (y/n)? ");

scanf(" %c", &ch); // Space before %c to ignore newline character

}
// Output memory allocation details

printf("\n\nTotal Memory Available: %d", ms);

printf("\n\nPROCESS\tMEMORY ALLOCATED");

for (i = 0; i < n; i++)

printf("\n%d\t%d", i + 1, mp[i]);

printf("\n\nTotal Memory Allocated: %d", ms - temp);

printf("\nTotal External Fragmentation: %d\n", temp);

return 0;

9.PAGE REPLACEMENT FIFO

FOR OUTPUT USE gcc fifo.c

./a.out

#include<stdio.h>

int main()

int pageFaults = 0;

int frames;

int m, n, s, pages;

printf("\n Enter frame size: ");

scanf("%d",&frames);

printf("\n Enter the no of pages: ");

scanf("%d",&pages);

int incomingStream[pages];

printf("\n Enter the String: ");

for(m=0;m<pages;m++){

scanf("%d",&incomingStream[m]);

printf("Incoming \t frame1 \t frame2 \t frame3");


int temp[frames];

for(m=0;m<frames;m++){

temp[m] = -1;

for(m=0;m<pages;m++){

s = 0;

for(n=0;n<frames;n++){

if(incomingStream[m] == temp[n]){

s++;

pageFaults--;

pageFaults++;

if((pageFaults<=frames) && (s == 0)){

temp[m] = incomingStream[m];

else if(s == 0){

temp[(pageFaults-1)%frames] = incomingStream[m];

printf("\n");

printf("%d\t\t\t",incomingStream[m]);

for(n=0;n<frames;n++){

if(temp[n]!=-1){

printf(" %d\t\t\t",temp[n]);

else{

printf(" - \t\t\t");

}
}

printf("\n Total page Faults: \t%d\n",pageFaults);

printf("\n Total page Hits: \t%d\n",pages-pageFaults);

return 0;

You might also like